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

Sven Barth pascaldragon at googlemail.com
Thu Jul 28 11:42:44 CEST 2016

Am 27.07.2016 23:27 schrieb "Jonas Maebe" <jonas.maebe at elis.ugent.be>:
> Before continuing, I think it would be a good idea to look at what the
desired concept is exactly (transparent proxy objects/references, thin
wrappers that allow to specify some default behaviour, nullable types, ...
?) already exists in other programming language (not C++ -- everything is
just meta-programmed using templates there) and how things work there at
least from a specification standpoint.

The main features that Maciej wants to achieve - at least as far as I can
see it - is ARC without changing TObject (basically the approach that one
would choose in C++) and Nullable types.
For the later I've found that C# and also Oxygene implement this, however
they don't do this based on the type, but the variable.

=== code begin ===

  x: nullable Integer;

=== code end ===

Pointer types (in Oxygene these are only object references, in FPC that
would also include the managed strings and dynamic arrays) are inherently
nullable ("nullable Pointer" or "nullable TObject" wouldn't be any
different from regular "Pointer" and "TObject") this also introducing "non
nullable", though I don't think we'd need this for now.
I haven't checked in detail, but there is probably a check for Nil plus an
exception if a Nil-nullable is assigned to a non-nullable variable
(something like "as" basically).

So we /could/ solve the Nullable topic by using the approach Oxygene has
chosen, maybe only with an exception of var-parameters (as internally it
would probably be represented by a automatically generated container

For the reference counting topic there aren't many alternatives than the
C++ approach (after all CLR and JVM support GC or have inherent ARC) and
while they do use templates for that they also rely on the extensive
operator overloading capabilities of C++, something that we don't have in

The main need would be an easy way to call the methods of the reference
counted instance while hiding the "gory" details.

Something like this would already be possible with only the management

=== code begin ===

procedure Something;
  o: TRef<TObject>;
  o := TStringList.Create;

=== code end ===

Now the desire is merely to get rid of the "Obj.".

That's where Maciej's idea for the "default" keyword came from, cause that
allows something similar for array properties.

If we implement the Nullable I mentioned above we could decouple the two
features and for example restrict "default" to fields to a structured type
(or better yet a property to such a type, as "var" support shouldn't be
included anyway).

What Maciej's approach would have allowed is for some ingenious developer
to fond a creative (ab)use for the feature.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160728/07f599f0/attachment.html>

More information about the fpc-devel mailing list