Is there a way to use a property similar to a behavior?

I have the following formula

X := X + F*(1-i div n); 

Where

 X, F, i, n: integer; 

The code I'm using is

 F := 7; // the initial speed with no friction. n := 10; // the animation number of steps. Hn := n * 2 ; X := 0; // first Pos i := 1; J := 1; while J < Hn do begin X := X + F * (1 - i div n); if X > Xmax then X := 0; <-- line (1). if i >= n then Dec(i) else Inc(i); Inc(J); end; 

If it were possible, I would like to use this, but without the implementation of the class / record (not inside the implementation / method of the class / record). Inaccurate syntax, only the same principle, instead of directly assigning X, SetX is then the result is assigned to X.

 X: integer write SetX; // This is not a correct delphi syntax. I added it to explain the behavior I want. function SetX(aValue: integer): integer; const Xmax: SomeIntegerValue; begin if aValue > Xmax then result := 0 else result := aValue; end; 

Therefore, I could omit Line (1). If possible, all lines after the formula would be omitted, and the while loop would look like this:

 while J < Hn do // J will be incremented each time the loop wants to read it. begin X := X + F * (1 - i div n); end; 

Is it possible to use a property similar to a behavior?

Note. . I am looking for a way to change the purpose and ways of reading a variable, as you do in a record / class property.

-2
source share
3 answers

Is it possible to use a property similar to an approach outside the class / record?

No, getters and seters properties can only be implemented in records and classes.

+2
source

You can use a local function like

 procedure YourProcedure; var X: Integer; LJ: Integer; function J: Integer; begin Inc(LJ); Result := LJ; end; procedure SetX(const AValue: Integer); const Xmax: SomeIntegerValue; begin if aValue > Xmax then X := 0 else X := aValue; end; //... begin while J < Hn do // J will be incremented each time the loop wants to read it. begin SetX(X + F * (1 - i div n)); end end. 
0
source

I found a way to do what I wanted. I know that operator overloading := impossible, however, forcing the compiler to perform the same behavior as the operator with overloading is possible.

Overloading did not allow me to control LSA (left side argument). but he completely controlled the implicit conversion of any TType (in my case it is integer ) to TXinteger . So all I had to do was make sure that each statement would result in a TType , which would cause the compiler to implicitly convert it to TXinteger .

Forcing the compiler to use my implicit statement every time it wants to assign something to a TXinteger , then I control the assignment. So I overloaded the operator := .

an example is given that eliminates the possibility of Line (1).

 program Project4; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TXinteger = record X: integer; class operator Add(a, b: TXinteger): integer; class operator Add(a: TXinteger; b:integer): integer; class operator Add(a: integer; b:TXinteger): integer; class operator Implicit(a: Integer): TXinteger; class operator Implicit(a: TXinteger): Integer; end; // Example implementation of Add class operator TXinteger.Add(a, b: TXinteger): integer; begin result := aX + bX; end;(**) class operator TXinteger.Add(a: TXinteger; b:integer): integer; begin result := aX + b; end; class operator TXinteger.Add(a: integer; b:TXinteger): integer; begin result := a + bX; end; class operator TXinteger.Implicit(a: Integer): TXinteger; const Xmax: integer = 10; begin if a > Xmax then result.X := 0 else result.X := a; end; class operator TXinteger.Implicit(a: TXinteger): Integer; begin result := aX; end; var X: TXinteger; Hn, F, i,J, n: integer; begin try F := 7; n := 10; Hn := n * 2 ; X := 0; i := 1; J := 1; while J < Hn do begin X := X + F * (1 - i div n); // Line (1) is gone now. if i >= n then Dec(i) else Inc(i); Inc(J); end; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. 

Note: for this case it makes no sense to do all this simply to omit one line of code. I would like to share this because it gives an idea of ​​how to overload the := operator.

What I wanted was:

  • Change the reading method of X:Integer (value read from x variable storage).
  • Change the way X:Integer .

Overloading all the operators that use the value of X, I completed the first. And, forcing the compiler, as explained above, I completed the second.

Thank you all for your help.

0
source

Source: https://habr.com/ru/post/1494574/


All Articles