Recent

Author Topic: Finding null value of variable types  (Read 656 times)

nikel

  • Full Member
  • ***
  • Posts: 135
Finding null value of variable types
« 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?
« Last Edit: May 06, 2021, 10:01:54 am by nikel »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Finding null value of variable types
« Reply #1 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)
« Last Edit: May 06, 2021, 10:17:48 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3080
Re: Finding null value of variable types
« Reply #2 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

egsuh

  • Hero Member
  • *****
  • Posts: 834
Re: Finding null value of variable types
« Reply #3 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.

nikel

  • Full Member
  • ***
  • Posts: 135
Re: Finding null value of variable types
« Reply #4 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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 3080
Re: Finding null value of variable types
« Reply #5 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018