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.
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.
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.
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
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.
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
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.
null/nil is so useful. Zero values aren't enough information, and with lape I'm not sure they are guaranteed.
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;
This gets a cast error if the type is a record containing other records.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)