Results 1 to 10 of 10

Thread: Three questions - setting result at beginning, overloading commands, classes/objects

  1. #1
    Join Date
    Oct 2015
    Location
    Nowhere
    Posts
    134
    Mentioned
    2 Post(s)
    Quoted
    64 Post(s)

    Question Three questions - setting result at beginning, overloading commands, classes/objects

    1. Is it necessary to set the result of a function at the beginning? I remember seeing it done when I first started programming and have been doing it on autopilot ever since, but it really seems unnecessary in most situations.

    Simba Code:
    function waitForX(x:Object):Boolean;
    begin
      Result := false;
     
      while(not(find(x)))do {Just an example, I don't made infinite loops like this}
        wait(250);

      Result := true;
    end;

    2. Is it possible to overload certain commands such as break()? I'd like to have something like:

    Simba Code:
    while(not(findX()))do
    begin
    if(time > 5000)then
       break(y, -1, 'took too long');
    end;

    Instead of:

    Simba Code:
    while(not(findX()))do
    begin
      if(time > 5000)then
      begin
        searchObj := y;
        log(-1, 'took too long');
        break();
      end;
    end;

    3. Is it possible to create objects/classes? I saw a thread where someone did do it, but I was unable to do it myself.

  2. #2
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    1. No, but it feels safer.
    2. Try it?
    3. Not true OOP Objects, no. You can make records that have namespaced functions but it's not the same thing.

  3. #3
    Join Date
    Oct 2015
    Location
    Nowhere
    Posts
    134
    Mentioned
    2 Post(s)
    Quoted
    64 Post(s)

    Default

    Quote Originally Posted by tls View Post
    1. No, but it feels safer.
    2. Try it?
    3. Not true OOP Objects, no. You can make records that have namespaced functions but it's not the same thing.
    I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask

    Currently doing record/namespace. Shame we can't do it

    Thanks!

  4. #4
    Join Date
    Dec 2007
    Posts
    289
    Mentioned
    4 Post(s)
    Quoted
    86 Post(s)

    Default

    Quote Originally Posted by skollstagg View Post
    I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask
    I don't think you can overload a keyword (despite it being implemented as a procedure).

  5. #5
    Join Date
    Dec 2010
    Posts
    483
    Mentioned
    30 Post(s)
    Quoted
    328 Post(s)

    Default

    Quote Originally Posted by skollstagg View Post
    I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask

    Currently doing record/namespace. Shame we can't do it

    Thanks!
    Yes, it is a shame. The lack of inheritance, polymorphism, and all around OOP awesomeness actually makes me hate Simba on levels you don't even understand sometimes.

    You can't overload the break keyword. At most, you could make an alias for it, and I don't know if that is even allowed in Lape. You could however write your own function that will do what you want as well as break...

    Lastly, please keep in mind the exit() keyword. When used within a function, it acts as a return, and is much nicer on the eyes than "result". ie. exit(true); exit(false); exit(42); etc

  6. #6
    Join Date
    Oct 2015
    Location
    Nowhere
    Posts
    134
    Mentioned
    2 Post(s)
    Quoted
    64 Post(s)

    Default

    Quote Originally Posted by the bank View Post
    Yes, it is a shame. The lack of inheritance, polymorphism, and all around OOP awesomeness actually makes me hate Simba on levels you don't even understand sometimes.

    You can't overload the break keyword. At most, you could make an alias for it, and I don't know if that is even allowed in Lape. You could however write your own function that will do what you want as well as break...

    Lastly, please keep in mind the exit() keyword. When used within a function, it acts as a return, and is much nicer on the eyes than "result". ie. exit(true); exit(false); exit(42); etc
    Normally that wouldn't be an infinite loop and I'd have exits within, but would it be good practice to always use exit regardless?

    Could you also tell me how I can create a function that will act as a break? I couldn't think of a way before and didn't really know what to google..

  7. #7
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by skollstagg View Post
    but would it be good practice to always use exit regardless?
    Exit is nice when, and really only when it enhances the readability of the code, eg a lot functions do checks at the "start" which will return if the arguments doesn't match some criteria. This is when Exit should be used.
    Using Exit in the middle (of a larger function) can in cases degrade the readability.
    Using Exit at the end of a function just to use it would be pointless. And avoiding using the "Result" variable would be rather pointless as well, since it's already there, and you plan on returning something anyway.

    In other words, usage of "Exit" depends, there are no clear cut answers, other than to use your head.

    pascal Code:
    function sum(x:Array of Int32): Int64;
    var i:Int32;
    begin
      for i:=0 to High(x) do
        Result += x[i];
    end;
    Here you see that the Exit keyword simply isn't needed, and the result-variable does the trick. It would be bad practice to use Exit here as it's simply not needed..

    Quote Originally Posted by skollstagg View Post
    Could you also tell me how I can create a function that will act as a break? I couldn't think of a way before and didn't really know what to google..
    This is not doable. Break will break out of a loop, you can have a procedure or a function do this. Break is builtin to the language simply because this functionality can't be created/duplicated on the scriptside.

    What you can do is:
    pascal Code:
    while(not(FindX()))do
    begin
      if (time > 5000) then
      begin
        DoStuff(y,-1,'took too long');
        Break;
      end;
    end;
    Where DoStuff does whatever you need to do with "y" and also calls the "log"-method. Just like you wanted to do by overloading "break".

    Also modifying break would only make your code less readable, shorter isn't always better. This is a clear case where it's NOT. Same goes for the example I wrote above, it's less readable then the original code.


    As for your first post, setting result at the beginning isn't needed unless default return value should not be the same as the value lape inits your variable to.
    Booleans default to False, so for your example it isn't needed in lape.

    For other languages, including Delphi and FPC you need to initialize every variable before you use em, else you would end up with unexpected outcome, as these languages do not initialize your variables to some default.
    Last edited by slacky; 02-17-2016 at 08:54 PM.
    !No priv. messages please

  8. #8
    Join Date
    Dec 2010
    Posts
    483
    Mentioned
    30 Post(s)
    Quoted
    328 Post(s)

    Default

    Quote Originally Posted by slacky View Post
    In other words, usage of "Exit" depends, there are no clear cut answers, other than to use your head.

    pascal Code:
    function sum(x:Array of Int32): Int64;
    var i:Int32;
    begin
      for i:=0 to High(x) do
        Result += x[i];
    end;
    Here you see that the Exit keyword simply isn't needed, and the result-variable does the trick. It would be bad practice to use Exit here as it's simply not needed..
    Correct me if I'm wrong, but in your example exit wouldn't only be unneeded but infact wouldn't allow the function to work properly at all. Exit would act as a return and exit the function during the first iteration. Assuming my understanding is correct.

  9. #9
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Quote Originally Posted by the bank View Post
    Correct me if I'm wrong, but in your example exit wouldn't only be unneeded but infact wouldn't allow the function to work properly at all. Exit would act as a return and exit the function during the first iteration. Assuming my understanding is correct.
    The only way to use exit in that code would be to use it like "return" in other languages, which would be pointless since we have that lovely "Result" variable:

    pascal Code:
    function sum(x:Array of Int32): Int64;
    var i:Int32;
    begin
      for i:=0 to High(x) do
        Result += x[i];
      Exit; //or Exit(Result);
    end;
    Point was that the above Exit is totally pointless in Pascal.

    Even if you have tiny function like:
    pascal Code:
    function squared(x:Int32): Int32;
    begin
      Result := x*x;
    end;
    You should not replace Result with Exit here, there is no reason to use Exit, it's simply not ment for this purpose, Exit is and should be used when you need to return earlier then the functions builtin return-point.

    There is in fact a person (maybe a few) on this forum that does stuff like that: "Exit(Result)" and "Exit(x*x)" (for absolutely no reason at the end of a function), a long with a bunch of other questionable stuff. like using pointers everywhere... and it's really not a pretty view.
    hint: oglib
    Last edited by slacky; 02-17-2016 at 10:38 PM.
    !No priv. messages please

  10. #10
    Join Date
    Oct 2015
    Location
    Nowhere
    Posts
    134
    Mentioned
    2 Post(s)
    Quoted
    64 Post(s)

    Default

    Excellent that's how I currently use exit/result. Been trying to stop myself setting result at the beginning bit it's like a habit at this point

    Simba Code:
    function TInterface.openWindow(window:TWindow):Boolean;
    var
      t:Longword;
    begin
      if(window.isOpen())then
        exit(true);

      markTime(t);
      while(true)do
      begin
        if(timeFromMark(t) > 40000)then
          logTerminate('TInterface.openWindow: Fail: ' + window.s);

        if(window.openKey > -1)then
          typeKey(window.openKey)
        else
          mouseBox(window.b, 1, 7);

        if(waitFor(@window.isOpen, 5000))then
          exit(true);
      end;
    end;

    Simba Code:
    function TWindow.isOpen():Boolean;
    begin
      result := findObj(self.obj);
    end

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
  •