Recent

Author Topic: What is the goal of assigned() ?  (Read 6802 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #45 on: April 07, 2021, 09:38:33 pm »
@Fred vS
Quote
But the problem remains because if V was not initialized, the result of  VarIsNull(V)  will be false (because initialized with random):
By default, a variant is unassigned or empty. Unassigned is the a lowest state in the variant hierarchy.  A null variant is already an assigned one, but has the value null.

OK, perfect, for me all is perfectly clear now.

So the conclusion should be: could it be possible to envisage a new function ?:

Code: Pascal  [Select][+][-]
  1.  isinitialized(myvariable)


And I know already the answer:

Quote
No, you should take care to initialize you local variable by your self.

Many thanks to everybody, it was a very instructive topic.

Fre;D


I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #46 on: April 07, 2021, 09:53:23 pm »
Quote
So the conclusion should be: could it be possible to envisage a new function ?:  isinitialized(myvariable)
You don't have to envision it, it's already implemented:
Code: Pascal  [Select][+][-]
  1. uses variants;
  2.  
  3. var
  4.   V: Variant;
  5. begin
  6.   if V = Variants.Unassigned then
  7.     //...
  8. end;

The code from your previous post, should look like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   V: Variant;
  3. begin
  4.   if (V = Variants.Unassigned) or (V = Variants.Null) then //if VarIsEmpty(V) or VarIsNull(V) then
  5.     Caption := 'V is nothing'
  6.   else
  7.     Caption := 'V is something';
  8. end;  

Quote
No, you should take care to initialize you local variable by your self.
I agree with this one. Please try the following code with and without Sum := 0
Code: Pascal  [Select][+][-]
  1. var
  2.   Sum, I: Integer;
  3. begin
  4.   Sum := 0;
  5.   for I := 1 to 10 do
  6.      Sum := Sum + I;
  7.   ShowMessage(Sum.ToString);
  8. end;
Is there any difference between the two results?
« Last Edit: April 07, 2021, 10:01:59 pm by GetMem »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #47 on: April 07, 2021, 10:21:44 pm »
Well like in Javascript.
Seems that does not work for classes?

Well in a class we can implement it ourselves?

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #48 on: April 07, 2021, 10:25:18 pm »
Hello GetMem.

Finally, I get THE answer of my original first post, first question:

Quote
So what must be used to know if something was not assigned by something ( even not nil )?

Your answer is exactly what I asked for "common local" variables ( not variants ) but it seems to exist only for variants:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.  V: Variant;
  4. begin
  5.   if (V = Variants.Unassigned) then
  6.      Caption := 'V is nothing'
  7.    else
  8.      Caption := 'V is something';
  9. end;      

It works perfectly as wanted: TForm1.caption =  'V is nothing'

Quote
Please try the following code with and without Sum := 0
Is there any difference between the two results?

This, with or without  Sum := 0, gives same result: caption = 55;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   Sum, I: Integer;
  4. begin
  5.   Sum := 0;   // with or without I get 55
  6.   for I := 1 to 10 do
  7.      Sum := Sum + I;
  8.   Caption := Sum.ToString;
  9. end;

Fre;D
« Last Edit: April 07, 2021, 10:56:56 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #49 on: April 07, 2021, 11:34:29 pm »
Hello everybody.

Now that I am a local variable (included variant) Guru, this are my conclusions:

The names used for properties are not always logical.

For example: Variants.Unassigned will check if the variant was initialized.
Imho, Variants.Unassigned should be renamed Variants.Uinitialized.

And for the famous assigned(), I would rename it into notnil().

[EDIT] And even if I agree that it is a good practice to always initialize local variables, there are cases when not all the variables will be used and to share memory only initialize it when needed.

Something for all variables like initialized(thevar) that will check, like variants do with Variants.Unassigned , if  the variable was initialized or no should be useful.

Voila, this to annoy my colleagues the gurus.

Thanks to everybody, it was a beautiful travel.

Fre;D
« Last Edit: April 08, 2021, 12:04:25 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #50 on: April 08, 2021, 02:00:29 am »
I suppose you can add an extension and name it as you want?

In JavaScript is called undefined and null, so every language puts the name the devs wants, there is no standard  :D

But... Is not the same as nil, so I prefer null instead.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: What is the goal of assigned() ?
« Reply #51 on: April 08, 2021, 02:35:25 am »
Hello GetMem.

Finally, I get THE answer of my original first post, first question:

Quote
So what must be used to know if something was not assigned by something ( even not nil )?

Your answer is exactly what I asked for "common local" variables ( not variants ) but it seems to exist only for variants:
...

Yes... but it not solves for your original snippet?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. abitmap : TBGRABitmap;
  4. begin
  5. abitmap.free;
  6. end;

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #52 on: April 08, 2021, 03:30:04 am »
Hello GetMem.

Finally, I get THE answer of my original first post, first question:

Quote
So what must be used to know if something was not assigned by something ( even not nil )?

Your answer is exactly what I asked for "common local" variables ( not variants ) but it seems to exist only for variants:
...

Yes... but it not solves for your original snippet?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. abitmap : TBGRABitmap;
  4. begin
  5. abitmap.free;
  6. end;

Yes, no.

It does not solve the thing, but now I know that with variant there is a trick (variant.unassigned) and for the other variables, the only solution is to initialize the var like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. abitmap : TBGRABitmap = nil;  // here initialize
  4. begin
  5. abitmap.free;
  6. end;
« Last Edit: April 08, 2021, 03:37:04 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #53 on: April 08, 2021, 06:55:51 am »
@Fred vS
Quote
This, with or without  Sum := 0, gives same result: caption = 55;
Please see attached image with Lazarus Trunk/FPC 3.2.0. So I always initialize everything.

@Fred vS, @lainz
Many of your arguments are valid. Indeed it seems strange that you have to initialize some variables others are initialized by default, but this is how pascal works. In my opinion every language has it's own peculiarity, for example I found  "=", "==", "===" in javascript hilarious.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: What is the goal of assigned() ?
« Reply #54 on: April 08, 2021, 09:07:25 am »
We don't have nullable types in FPC?

With FPC 3.2.1 and newer:

Code: Pascal  [Select][+][-]
  1. program tnull;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Nullable;
  7.  
  8. procedure Test;
  9. var
  10.   n: specialize TNullable<LongInt>;
  11. begin
  12.   Writeln(n.HasValue);
  13.   n := 42;
  14.   Writeln(n.HasValue);
  15.   Writeln(LongInt(n));
  16.   n.Clear;
  17.   Writeln(n.HasValue);
  18. end;
  19.  
  20. begin
  21.   Test;
  22. end.

Output:

Code: [Select]
FALSE
TRUE
42
FALSE

But the problem remains because if V was not initialized, the result of  VarIsNull(V)  will be false (because initialized with random):

Not quite right. Like e.g. AnsiString and dynamic arrays a Variant is a managed type. Thus it's initialized by the compiler/RTL to a default value which (as you noticed) is not Null, but Unassigned. You can test for it with VarIsEmpty which will return True for such a Variant.

How can I test if a variable has a valid instance of an object if the opposite of nil is not always assigned?
In my code, I can follow my rule of always initialising to nil, but how can I test that if I'm handed something from a black box of someone else's code?

You can't, because to decide whether some variable (especially pointer types like classes, dynamic arrays and such) is a valid, correct "instance" of its type is essentially uncomputable at least in languages that don't run on some kind of VM (e.g. Java, .Net, JavaScript) and thus have more control over things.

So the conclusion should be: could it be possible to envisage a new function ?:

Code: Pascal  [Select][+][-]
  1.  isinitialized(myvariable)


Such a function only makes sense for local variables (due to the above point of computability) and only inside the same function and then you don't need this anymore, because you're in control of your own code, thus you know whether you initialized it or not.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #55 on: April 08, 2021, 11:15:16 am »
@Fred vS
Quote
This, with or without  Sum := 0, gives same result: caption = 55;
Please see attached image with Lazarus Trunk/FPC 3.2.0. So I always initialize everything.

Huh, how did you get this?

With the code I gave, on Linux 64 bit and fpc 3.2.0, I get the same result, with or without  Sum := 0, and it is  55.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #56 on: April 08, 2021, 11:29:04 am »
Quote
Huh, how did you get this?
With the code I gave, on Linux 64 bit and fpc 3.2.0, I get the same result, with or without  Sum := 0, and it is  55.
After some more research, I found out the difference is bitness.
1. With Lazarus trunk/FPC 3.2.0 (32 bit) I get the strange result
2. With Lazarus trunk/FPC 3.2.0 (64 bit) works OK as in your case

PS: I'm on windows 64 bit.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: What is the goal of assigned() ?
« Reply #57 on: April 08, 2021, 11:38:07 am »
Quote
Huh, how did you get this?
With the code I gave, on Linux 64 bit and fpc 3.2.0, I get the same result, with or without  Sum := 0, and it is  55.
After some more research, I found out the difference is bitness.
1. With Lazarus trunk/FPC 3.2.0 (32 bit) I get the strange result
2. With Lazarus trunk/FPC 3.2.0 (64 bit) works OK as in your case

PS: I'm on windows 64 bit.

Hum...  :(

Many thanks GetMem for your light.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

balazsszekely

  • Guest
Re: What is the goal of assigned() ?
« Reply #58 on: April 08, 2021, 12:06:42 pm »
Quote
Many thanks GetMem for your light.
You're welcome. :)

 

TinyPortal © 2005-2018