SRL Archive - OSR Help

Lape functions


rj

2013-09-25 18:32:43

How come the lape functions are formatted like this?

procedure TRSInterface.setBounds(newBounds: TBox);

How do you get the dot in there? What record is it pointing to I don't understand

riwu

2013-09-25 18:41:29

It's a method specifically for the TRSInterface type. (it's like equivalent to a public static method for static class in java)
So you can do like:

varA: TRSInterface;
//some other code to initialize varA
varA.setBounds();

Within the setBounds body, it could refer to the TRSInterface by calling 'Self'.
Eg.

procedure TBox.printX1;
begin
writeln(Self.X1); //prints x1 of the box
writeln(X1); //does the same thing as ^
end;

begin
IntToBox(1, 2, 3, 4).printX1;
end.

rj

2013-09-25 18:44:18

From: riwu
It's a method specifically for the TRSInterface type. (it's like equivalent to a public static method for static class in java)
So you can do like:

varA: TRSInterface;
//some other code to initialize varA
varA.setBounds();

Within the setBounds body, it could refer to the TRSInterface by calling 'Self'.
Eg.

procedure TBox.printX1;
begin
writeln(Self.X1); //prints x1 of the box
writeln(X1); //does the same thing as ^
end;

begin
IntToBox(1, 2, 3, 4).printX1;
end.


but the dot isn't referring to anything inside of the record I don't get it :s

riwu

2013-09-25 18:47:39

From: Officer Barbrady
but the dot isn't referring to anything inside of the record I don't get it :s

Yeah we are not accessing any member of the record, we are creating a new method for the record.
Records in lape bear several similarities to classes, in that not only u can have variables, but also methods declared specifically for the record.

Gu3st...

2013-09-25 19:11:26

The function you posted (TRSInterface.setBounds(blah)) in the first post is a function that refers to the actual type "TRSInterface" and not a variable.
Lape adds the function to the type record so that when you assign that type record to a variable, say "someVariable : TRSInterface" you can then call "someVariable.setBounds(blah)"

rj

2013-09-25 19:36:08

type color = record
test:string;
end;


function color.showColor:string;
begin
result := self.test;
end;



begin


end.

wee ok got it


That method makes things faster right?

riwu

2013-09-26 07:22:03

From: Officer Barbrady
type color = record
test:string;
end;


function color.showColor:string;
begin
result := self.test;
end;



begin


end.

wee ok got it


That method makes things faster right?

Performance wise, there's no significant difference if you access the record field directly instead. Though if you create a getter and setter method for each field you could imitate object-oriented programming, though not quite the case since you can't declare the fields as private.

The main purpose of type methods is just to make methods more readily understood and the code more readable.
For instance, you will need to remember to use GetTPABounds() to get the bound of a TPA, and GetATPABounds() to get the bound of ATPA.
If the developer create type methods for both of them to return the bounds, you can simply do

TPA.getBounds(); //gets the bound of TPA
ATPA.getBounds(); //gets the bound of ATPA
So in essence, you just need to remember to call .getBounds to get the bound of whichever type :)

As all non-primitives are stored as references (pointers to the memory addresses) in lape, this feature is extremely useful as we can just make .equals methods for all the types (rather than a standard-alone function for every type, which is much harder to use and uglier as well)

Gu3st...

2013-09-26 12:43:37

An example of how this can be used is this:

type
TSomeType = record
weHaveSomeVariables : array [0..3] of Integer;
end;

function TSomeType.toBox(): TBox;
begin
result := intToBox(self.weHaveSomeVariables[0], self.weHaveSomeVariables[1],
self.weHaveSomeVariables[2], self.weHaveSomeVariables[3]);
end;

function TBox.searchForColor(whatColor : Integer): Boolean;
begin
// if (CODE GOES HERE) then
result := True;
end;

var
Weehaw : TSomeType;

begin
Weehaw.weHaveSomeVariables := [2, 3, 4, 5];
if Weehaw.toBox().searchForColor(0) then
writeLn('Found the color 0 in the box created from Weehaw');
end.

Which means you can create incredibly long lines that do the same job as 20 lines of code do without taking away readability.