Is it possible to get rid of cluttering up code with unnecessary functions when writing a class?

SomeClass1 = class SomeValue : Record z:integer; end; end; SomeClass2 = Class SomeRec : SomeClass1; end; SomeClass3 Private MyClass : SomeClass2; Public Property SomeVal : integer read MyClass.SomeRec.SomeVal.z;//<<<Тут ошибка End; 

Is it necessary in SomeClass3 to make an additional function for reading SomeVal.z? or you can eat this tricky design right in the description of Property SomeVal

 SomeClass3 Private Function ReadSomeVal:integer; MyClass : SomeClass2; Public Property SomeVal : integer read ReadSomeVal; End; implementation Function SomeClass3.ReadSomeVal; Begin Result:=MyClass.SomeRec.SomeVal.z; end; 
  • Yes, sure. So it is more correct from all points of view. > Result: = MyClass.SomeRec.SomeVal.z; Isn't it scary that suddenly it will turn out that MyClass = nil , and the call to the service will end with AV? - Nofate
  • No, it's not scary) SomeClass3 is a Child for MyClass and serves to abbreviate variable names from MyClass .. Made to simplify scripting (using TPSScript) so that the script can be converted from the form wnd (wName) .SomeVal instead of MyClass (LinkMyClass) .SomeClass.SomeClass.SomeVal .Val; - Vladimir Klykov
  • It is honest to say the question of correctness is the last thing that interests me when building such a bicycle;) the very existence of SomeClass3 is already wrong) - Vladimir Klykov
  • It is sad. Let's answer, I will accept his chtoli.) - Vladimir Klykov

3 answers 3

Delphi, in any case, expects Field or method after read , so no one will put an expression in there.

  • Not yet closed, is it possible to do so? Function SomeFunc (s: String): boolean; Property SomeProp: boolean read SomeFunc ('1'); - Vladimir Klykov
  • no, because Field or method is expected, i.e. identifier of the method or field, and not a calculated expression (which is a function call). No one is surprised that you cannot write this: Property SomeProp: integer read 5 + 3; - Nofate
  • Well, I suppose I am indignant ... and I would love to write as shown above) - Vladimir Klykov
  • one
    So forknit the FreePascal Compiler and rewrite it as needed) - Nofate

Yes, sure. If you do not want to write a lot of geters in the class (although it is not clear why this is an autogenous), then you can probably make some universal getter from an index for example. And either the hard code, or through the implement. But why?

 PropertiesInterface = interface function GetIntegerValueByName(name:string):integer; function GetValueContainerByName(name:string):PropertiesInterface ; end SomeClass1 = class(PropertiesInterface) SomeValue : Record z:integer; end; function GetIntegerValueByName(name:string):integer; function GetValueContainerByName(name:string):PropertiesInterface ; end; SomeClass2 = Class(PropertiesInterface) SomeRec : SomeClass1; function GetIntegerValueByName(name:string):integer; function GetValueContainerByName(name:string):PropertiesInterface ; end; SomeClass3 Private MyClass : SomeClass2; Public Property SomeIntVal[name: string] : integer read GetIntVal End; implementation //name VarName|VarName| Function GetIntVal(name: string):integer; var varList: array of string; i: integer; current_var: PropertiesInterface; Begin varList = name.Strip("|");//не помню есть ли такое в дельфи, но суть ясна? current_var = MyClass as PropertiesInterface; for i := 0 to Length(varList)-1 do begin current_var = current_var.GetValueContainerByName(varList[i]); if current_var = nil then raise ENotFoundExeption; end; if current_var <> nil then result = current_var.GetIntegerValueByName(varList[Length(varList)-1]) else raise ENotFoundExeption; end; 
  • Not an option. the condition is described clearly) how to do it differently, I already know (I’m doing it in another place), but I wanted to describe this class as short and beautiful as possible - Vladimir Klykov
  • C #? :-) public int SomeVal {get {return MyClass.some.other.vals;}} Unfortunately in object pascale so nez. Although it may be for the better, it disciplines more. You can put the implementation of these getters into another file, of course. I do not quite understand why it is necessary in the class description to see where it takes the data from? In my opinion, the right way is to correctly name the property, well, and the setters getters can be taken out under the directive {$ I}. - Chad
  • one
    Yes, I also wondered if the compiler would not eat an anonymous function, so to say in-place. But no, not eaten. It is for the better: anonymous functions in the dolphin are very cumbersome. - Nofate
  • Yes, and it will be contrary to ideology. Suppose our implementation is in the dll and in the interface we suddenly somehow stuck such an expression - in the end, or the linker with the compiler will go crazy, or the programmers who wrote the implementation when they sprinkled on the claims that their code does not work as it should. And, I am sure, they will then find that magician who corrected the interface part of the class, and if it doesn’t reach 105, then 116 will definitely be :-) - Chad
  • Oh, oh, everything is described above as I have already said somewhere that it was done for convenient processing of scripts on a pascal typewriter .. well, it’s not for me to know the class hierarchy for me, it’s enough for me to climb, I just didn’t want to fence a bunch of functions of heterosexuals. because Pascal machine itself pulls the clashes on which they poke a finger and there was an attempt to make such a bike, well, since it’s not destiny I will write a bunch of heterosexuals ... - Vladimir Klykov

I'm sorry. By topic, I do this:

 TMyClassAbstract = class private function GetX: Integer; procedure SetX(Value: Integer); // ... public property X: Integer read GetX write SetX; // ... и все другие свойства end; TMyClass = class (TMyClassAbstract) // .. 

i.e. I hide all the properties in the abstract class

  • Not about that - Nofate
  • I'm sorry. On the topic, I do this: TMyClassAbstract = class private function GetX: Integer; procedure SetX (Value: Integer); // ... public property X: Integer read GeX write SetX; // ... and all other properties end; TMyClass = class (TMyClassAbstract) i.e. I hide all properties in the abstract class - ADR
  • I do not need to hide them and do not believe the Private section and all that is visible in it only when called from the component \ child. I wanted to get rid of the description of the f-th, because I do not consider it useful as a function code like begin result: = someclass.val; end; - Vladimir Klykov
  • >> Private section and all that is visible in it only when calling from component \ descendant No. What is visible in it only within the module. But we are not going to directly access the field when there is a property? (and if so that is protected) Get rid of the function code, of course, is not possible. I can only advise using the abbreviation: Ctrl + Spase + propf (properties for accessing the field), Ctrl + Spase + propgs (via the Get / Set function), Ctrl + Spase + propro (Read Only), etc. (they are in Delphi XE2 and higher) for automatic code generation - ADR