[fpc-devel] "Default" discussion for SmartPointers etc

Maciej Izak hnb.code at gmail.com
Thu Jul 28 13:51:04 CEST 2016


2016-07-28 13:30 GMT+02:00 Michael Van Canneyt <michael at freepascal.org>:
>
> Assume a is null before the call to test1:
>
>   test1(a);
>

That won't compile.


>
> 1. What happens if this procedure is empty, i.e. no write is performed ?
>    (or it is performed conditionally)
>    On return, is a null or not ?
>

You are not able to call test1 in presented form (you will get error:
Incompatible types).


> 2. Assume your variable is null before the call to Test1.
>    What is the value on entry into Test1 ? i.e. what happens if
>
>    begin
>      if X=0 then
>        x:=1
>      else
>        X:=23;
>    end
>

That won't compile too. The correct code:

=== code begin ===

  if x.HasValue and (x^ = 0) then
    x := 1
  else
    x := 23;

=== code end ===

or

=== code begin ===
  if x.GetValueOrDefault = 0 then
    x := 1
  else
    x := 23;
=== code end ===

3. How do you detect an actual write ?
>
> 4. What happens if Test1 is an external C procedure ?


It depend on context. If we have:

x := 1; // is detected in class operator Implicit(A: T): TNullable<T>;

when you use

x^ := 1; // you are operating directly on Instance field

in that case if x.HasValue is false you will get EAccessViolation

-- 
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160728/0f55b058/attachment.html>


More information about the fpc-devel mailing list