Recent

Author Topic: Assigned() ?  (Read 1308 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Assigned() ?
« on: May 15, 2023, 01:52:14 pm »
Hello.

I did try to find the declaration of Assigned( P: Pointer) but without luck.

So the question is:
Code: Pascal  [Select][+][-]
  1. if assigned(Apointer) then

is it equal to this ?:

Code: Pascal  [Select][+][-]
  1. if Apointer <> nil then

And subsidiary question: where is assigned(P: pointer) declared?

Thanks.

Fre;D
« Last Edit: May 15, 2023, 02:01:25 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

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Assigned() ?
« Reply #1 on: May 15, 2023, 01:59:35 pm »
The documentation states:
Code: Pascal  [Select][+][-]
  1. Assigned returns True if P is non-nil and retuns False of P is nil. The main use of Assigned is that Procedural variables, method variables and class-type variables also can be passed to Assigned.

So yes, it is simply a = nil check. I personally like it because it's a bit more verbose without being so much more in length. It just reads more like a sentence as "if the variable is assigned a value then". <> Nil is much more technical and less intuitive.
But in the end, there is no difference.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Assigned() ?
« Reply #2 on: May 15, 2023, 02:02:29 pm »
The documentation states:
Code: Pascal  [Select][+][-]
  1. Assigned returns True if P is non-nil and retuns False of P is nil. The main use of Assigned is that Procedural variables, method variables and class-type variables also can be passed to Assigned.

So yes, it is simply a = nil check. I personally like it because it's a bit more verbose without being so much more in length. It just reads more like a sentence as "if the variable is assigned a value then". <> Nil is much more technical and less intuitive.
But in the end, there is no difference.

OK, perfect, thanks Warfley.

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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Assigned() ?
« Reply #3 on: May 18, 2023, 04:43:56 pm »
There is a subtle, but important difference between Assigned(MethodVar) and MethodVar <> Nil in case of a method variable with the following signature: function: Pointer of object (or returning any type that can be checked against Nil) in mode Delphi: in case of the check against Nil the method pointer will be called, because Delphi (and FPC in modes Delphi and TP) attempts to call procedure and method variables (and function references) when they don't take parameters while FPC (except in modes Delphi and TP) requires you to use empty parenthesis for this. Assigned however will not attempt to call the method variable.
You can also see this when you use a method variable with a result type that can not be checked against Nil as then the compiler will issue a type conversion error.

Zoran

  • Hero Member
  • *****
  • Posts: 1831
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Assigned() ?
« Reply #4 on: May 18, 2023, 08:33:37 pm »
To better understand what PascalDragon means, you can try this:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode delphi}
  4.  
  5. function Fun: Pointer;
  6. begin
  7.   Result := nil;
  8. end;
  9.  
  10. type
  11.   TFun = function: Pointer;
  12.  
  13. var
  14.   B: Boolean;
  15.   F: TFun;
  16. begin
  17.   F := Fun;
  18.  
  19.   B := F <> nil;
  20.   Writeln(B); // prints FALSE
  21.  
  22.   B := Assigned(F);
  23.   Writeln(B); // prints TRUE
  24.  
  25.   ReadLn;
  26. end.
  27.  
  28.  

So, not often will you experience the different behaviour of Assigned(P) and P<>nil, but, when you do, this bug(*) can be very hard to find. So, we should keep this in mind.

(*) -- when I said "bug", I mean semantic error, this is not a bug, but documented behaviour.
« Last Edit: May 18, 2023, 08:38:10 pm by Zoran »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Assigned() ?
« Reply #5 on: May 18, 2023, 10:00:08 pm »
(*) -- when I said "bug", I mean semantic error, this is not a bug, but documented behaviour.

It's a bug in the language's syntax. It doesn't exist in C since the presence of (empty) parentheses is significant, or in Modula-2 where Wirth had realised his mistake. I don't know the situation in ALGOL, i.e. whether it was a problem he should have anticipated.

Having said which, pre-ANSI C was extremely weak when it came to at least warning about possible omission or inclusion of (empty) parentheses when a function without parameters was invoked or referenced.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Assigned() ?
« Reply #6 on: May 19, 2023, 02:53:01 am »
I did try to find the declaration of Assigned( P: Pointer) but without luck.
...
where is assigned(P: pointer) declared?

Assigned() is a compiler intrinsic function.  It is built-in to the compiler itself.  So there is no declaration for it.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Assigned() ?
« Reply #7 on: May 19, 2023, 03:30:44 am »
I did try to find the declaration of Assigned( P: Pointer) but without luck.
...
where is assigned(P: pointer) declared?
Assigned() is a compiler intrinsic function.  It is built-in to the compiler itself.  So there is no declaration for it.

Ha., ok, thanks Remy.

(for info:
https://en.wikipedia.org/wiki/Intrinsic_function
Not sure I understood, but it seems that a intrinsic function is mapping directly to some cpu instructions  ( or something like that  %)). )
« Last Edit: May 19, 2023, 03:37:52 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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Assigned() ?
« Reply #8 on: May 19, 2023, 08:34:56 am »
(for info:
https://en.wikipedia.org/wiki/Intrinsic_function
Not sure I understood, but it seems that a intrinsic function is mapping directly to some cpu instructions  ( or something like that  %)). )

Not really a safe assumption. It's probably better to say that there are a number of things that /look/ like function invocations which the compiler handles as special cases, of which WriteLn() is probably the most notorious.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Assigned() ?
« Reply #9 on: May 21, 2023, 01:11:25 pm »
(for info:
https://en.wikipedia.org/wiki/Intrinsic_function
Not sure I understood, but it seems that a intrinsic function is mapping directly to some cpu instructions  ( or something like that  %)). )

Intrinsic function in the context of FPC means that it's a function of which there is no Pascal implementation in the source of the RTL (e.g. Assigned) or of which only parts exist as implementations in the RTL (e.g. a Writeln exists of mulitple calls to RTL functions) and which is essentially generated by the compiler. It can also mean that it's maps directly to some specific CPU instructions (e.g. the intrinsics for the x86 IN/OUT instructions), but it's not restricted to that.

 

TinyPortal © 2005-2018