Lazarus

Programming => General => Topic started by: nikel on May 06, 2021, 09:54:04 am

Title: Finding null value of variable types
Post by: nikel on May 06, 2021, 09:54:04 am
Hello, I'm trying to find null values of variable types in order to terminate them. I was searching and finally this seemed worked:

Code: Pascal  [Select][+][-]
  1. Num:=not -1; // Outputs 0
  2. WriteLn(Num.ToString) // Should give error

On ActionScript I used to use something like:

Code: [Select]
Num=NaN; // Not a number
Mc=MovieClip;

Finally I stuck at this. I can't learn null value of TStringList (also tried StringList:=TStringList;)
Code: Pascal  [Select][+][-]
  1. StringList:=TStringList.Create;
  2. StringList.LoadFromFile('Yeni Metin Belgesi.txt', TEncoding.UTF8);
  3. WriteLn(Default(TStringList).ToString); // Gives error External:SIGSEGV
  4.  
  5. ReadLn;

And this sets an integer to 0 but doesn't terminate:
Code: Pascal  [Select][+][-]
  1. Num:=Default(Integer);

Where can I learn null value of variable types?
Title: Re: Finding null value of variable types
Post by: lucamar on May 06, 2021, 10:13:59 am
Few types have a "null" value in Pascal;  besides pointers as such basically only those that are internally a pointer, such as LongStrings, arrays, records, (old-style) objects, classes ... In all those cases comparing/setting to Nil is enough, or (for strings) to ''.

Furthermore, floating point vars (Single, Doubles, etc.) can be set/compared to NaN.

For all other types (Integer, Char, ...) you can set them to some "default" value which you could consider null, but they don't have one per se since any value they may have does, in fact, represent something.

Note also that there some caveats if you want to "terminate" variables of the first kind: arrays must have its length set to 0 (with SetLength()), records and objects must be Disposed (or its memory freed somehow), classes Freed, etc.

So in your examples, Num (which I suppose is an integer) can't be "terminated", while StringList (a class object) is terminated with StringList.Free (or FreeAndNil(StringList) for a testable state)
Title: Re: Finding null value of variable types
Post by: MarkMLl on May 06, 2021, 10:21:51 am
I echo what @Lucamar says, with the extension that an empty string is = '' (use of which is, I'm told, more efficient than checking for Length() = 0), an assigned-but-empty stringlist has Count = 0, and an unassigned object is nil... that's specifically why there's the Assigned() function.

As related issues, sometimes you need to consider the maximum and minimum possible values of (numeric) types, and in principle you might need to consider the identity of a type+operator combination.

The poor support for reserved "this variable contains nothing" values isn't an FPC problem, it's implicit in the PC architecture and in fact in most architectures with the exception of the few that have had out-of-word tags.

MarkMLl
Title: Re: Finding null value of variable types
Post by: egsuh on May 06, 2021, 10:28:35 am
I think there are no null values in pascal, in any type of variables. Well, PChar might --- but not sure --- I haven't used it.
With your example  num := not -1,

-1 is $FFFFFFFF.  and "not -1" does bit operation so that the result is $00000000, which is zero, not null.
Title: Re: Finding null value of variable types
Post by: nikel on May 06, 2021, 10:36:14 am
Thanks for the replies. So I'm going to use initial value instead of null while initializing and terminating most of them.
Title: Re: Finding null value of variable types
Post by: MarkMLl on May 06, 2021, 10:44:55 am
Thanks for the replies. So I'm going to use initial value instead of null while initializing and terminating most of them.

That's wise, but also look carefully at your program logic: I think it would be reasonable to class relying on null values in with relying on assertions, i.e. it's OK during development and testing but by the time you have production code most of that should be very much an exceptional condition.

MarkMLl
TinyPortal © 2005-2018