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

Maciej Izak hnb.code at gmail.com
Thu Jul 28 17:49:26 CEST 2016


2016-07-28 16:37 GMT+02:00 Michael Van Canneyt <michael at freepascal.org>:
>
> Are there additional benefits I have missed ?
>
>
Using nilable (I think nilable is better than nullable) records is terrible
without default field:

=== code begin ===

var
  n: TNilable<TRec>;
  x: TRec;
begin
  {

   ... let say that n is assigned somewhere, assume that n.HasValue = true

  }
  // to change any field in nilable n  you need to...
  x := n.Value;
  x.foo1 := 10;
  x.foo2 := 'abc';
  n := x;
end.

=== code end ===

instead of:

=== code begin ===

var
  n: TNilable<TRec>;
begin
  {

   ... let say that n is assigned somewhere, assume that n.HasValue = true

  }
  x.foo1 := 10;
  x.foo2 := 'abc';
end.

=== code end ===


> I can live with the 'ascetic' version :-)
>
> Since the following:
>
> Procedure SomeTest(Var X : Integer);
>
> begin
>   X:=1; end;
>
> Var
>   A : TNullable<Integer>;
>
> begin
>   SomeTest(A^);
> end.
>
> Will lead to a crash on a null value, I don't think this is a very good
> idea to add.
> We should protect users from crashes, not expose them to it :-)
>

that will change nothing, just look below. Just harder to usage pure
non-pascalish clone of C# struct (finally C# has pointers as
nonstandard/unsafe type so feature with direct dereference is disabled by
design).

=== code begin ===

procedure SomeTest(x : Integer);
begin
end;

Var
  A : TNullable<Integer>;
begin
  SomeTest(A.Value); // AV error here!
end.

=== code end ====

A^ is shortcut for A.Value but has advantage = direct dereference to
Instance.

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


More information about the fpc-devel mailing list