Results 1 to 8 of 8

Thread: Lape functions

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Lape functions

    How come the lape functions are formatted like this?

    Simba Code:
    procedure TRSInterface.setBounds(newBounds: TBox);

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

  2. #2
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    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:
    Simba Code:
    varA: TRSInterface;
    //some other code to initialize varA
    varA.setBounds();

    Within the setBounds body, it could refer to the TRSInterface by calling 'Self'.
    Eg.
    Simba Code:
    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.
    Last edited by riwu; 09-25-2013 at 10:43 PM.

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    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:
    Simba Code:
    varA: TRSInterface;
    //some other code to initialize varA
    varA.setBounds();

    Within the setBounds body, it could refer to the TRSInterface by calling 'Self'.
    Eg.
    Simba Code:
    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

  4. #4
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    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.

  5. #5
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    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)"

  6. #6
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Simba Code:
    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?

  7. #7
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    Simba Code:
    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
    Simba Code:
    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)
    Last edited by riwu; 09-26-2013 at 11:24 AM.

  8. #8
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    An example of how this can be used is this:

    Simba Code:
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •