Results 1 to 10 of 10

Thread: Null value in Simba?

  1. #1
    Join Date
    Mar 2014
    Posts
    205
    Mentioned
    4 Post(s)
    Quoted
    116 Post(s)

    Default Null value in Simba?

    Does Simba support a null value? I'm assuming so, since IsVarNull exists, but I can't figure out what it is for assigning to a variable.

  2. #2
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    IsVarNull = IsVariantNull, It's only used to see if a variant has been assigned or not.

    So no

  3. #3
    Join Date
    Mar 2014
    Posts
    205
    Mentioned
    4 Post(s)
    Quoted
    116 Post(s)

    Default

    Quote Originally Posted by Olly View Post
    IsVarNull = IsVariantNull, It's only used to see if a variant has been assigned or not.

    So no
    Well that's misleading!

    How would you differentiate between a valid result from a function and a non-valid result, then? In other languages I've always set the result to null (or whatever the negative value is) at the beginning of the function and then only modified the result if I have a valid result within the function. I can then use the non-valid result in an if..then statement when calling the function to tell if it's returned a valid result or not to continue on with my code.

    Say, for example, I want to have a function return a TPA with the closest result to my character. So I search for a TPA, break it into an ATPA with a 30 pixel-ish size, sort it from mid point, retrieve the first entry into that array and spit it out as a result. I then want to check in the procedure calling this function that it returned a TPA, as opposed to nothing, before continuing.

  4. #4
    Join Date
    Jun 2014
    Location
    Lithuania
    Posts
    475
    Mentioned
    27 Post(s)
    Quoted
    200 Post(s)

    Default

    Quote Originally Posted by adc View Post
    Well that's misleading!

    How would you differentiate between a valid result from a function and a non-valid result, then? In other languages I've always set the result to null (or whatever the negative value is) at the beginning of the function and then only modified the result if I have a valid result within the function. I can then use the non-valid result in an if..then statement when calling the function to tell if it's returned a valid result or not to continue on with my code.

    Say, for example, I want to have a function return a TPA with the closest result to my character. So I search for a TPA, break it into an ATPA with a 30 pixel-ish size, sort it from mid point, retrieve the first entry into that array and spit it out as a result. I then want to check in the procedure calling this function that it returned a TPA, as opposed to nothing, before continuing.

    Simba Code:
    length(tpa)
    If >0 then exist.

  5. #5
    Join Date
    Mar 2014
    Posts
    205
    Mentioned
    4 Post(s)
    Quoted
    116 Post(s)

    Default

    Quote Originally Posted by cosmasjdz View Post
    Simba Code:
    length(tpa)
    If >0 then exist.
    Ah, I see. Thanks very much.

  6. #6
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    I'm so glad Pascal doesn't fool around with null like Java does. It's unnecessary and there's never a situation where contending with null is beneficial to a programmer.
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  7. #7
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by adc View Post
    Does Simba support a null value? I'm assuming so, since IsVarNull exists, but I can't figure out what it is for assigning to a variable.

    Simba Code:
    function doSomething: TPointArray;
    begin
      Result := TPointArray(nil);
    end;

    function somethingElse(Size: Integer): Pointer;
    begin
      if (Size <> 0) then
        exit(AllocMem(100));

      Result := nil;
    end;


    begin
      writeln(doSomething);
    end.


    Quote Originally Posted by KeepBotting View Post
    I'm so glad Pascal doesn't fool around with null like Java does. It's unnecessary and there's never a situation where contending with null is beneficial to a programmer.

    I'm going to disagree with that. Linked-Lists, Binary Trees, etc.. use null all the time and its quite beneficial to know whether or not the next link points to a valid location.. Plugins need it. TMufasaBitmap returns nil on create upon failure.
    I am Ggzz..
    Hackintosher

  8. #8
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    Could try:
    Simba Code:
    if @anyArray<>nil then

    Or:
    Simba Code:
    function returnNothing():glTexture;
    begin end;

    function glTexture.isNull():boolean;
    var
      vTexture:glTexture;
    begin
      exit(compareMem(@self,@vTexture,sizeOf(glTexture)));
    end;[

    writeLN(returnNothing.isNull());
    Last edited by Obscurity; 08-28-2015 at 02:16 PM.




    Skype: obscuritySRL@outlook.com

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

    Default

    null/nil is so useful. Zero values aren't enough information, and with lape I'm not sure they are guaranteed.

  10. #10
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by adc View Post
    How would you differentiate between a valid result from a function and a non-valid result, then? In other languages I've always set the result to null (or whatever the negative value is) at the beginning of the function and then only modified the result if I have a valid result within the function

    Option 1: Use a special value as an indicator

    If your data has obvious good and bad values, you can choose a value to indicate an unknown or error response. This is very common but it does tend towards bad coding. Use of constants is recommended.

    If a function returns an integer but it's always >=0, the programmer might define some constants with -1, -2 to indicate errors/etc.

    Maybe your function returns a TBox of [-1,-1,-1,-1] to indicate it could not calculate a proper answer.

    Option 2: return a record

    Your function can return something like:

    Simba Code:
    record =
      status: boolean;
      response: double;
    end;

    Option 3: var parameters

    If it fits, your function could be set up like:

    Simba Code:
    function doStuff(var d: double): boolean;

    Quote Originally Posted by Brandon View Post
    Simba Code:
    function doSomething: TPointArray;
    begin
      Result := TPointArray(nil);
    end;

    begin
      writeln(doSomething);
    end.
    This gets a cast error if the type is a record containing other records.

    Quote Originally Posted by Obscurity View Post
    Simba Code:
    function returnNothing():glTexture;
    begin end;

    function glTexture.isNull():boolean;
    var
      vTexture:glTexture;
    begin
      exit(compareMem(@self,@vTexture,sizeOf(glTexture)));
    end;[

    writeLN(returnNothing.isNull());
    This works but it's really isDefault() not isNull(). If the default state is a valid option, this doesn't help the caller know if the result is ok or not.

    You could provide a glTexture.INVALID() that returns a known value that cannot be otherwise OK. And something like glTexture.isValid() or such to check it.

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
  •