[fpc-devel] "Default" discussion for SmartPointers etc
hnb.code at gmail.com
Tue Jul 26 14:51:12 CEST 2016
Finally I have a working implementation (not published yet) of Smart
Pointers/ARC objects and Nullable (Nilable?) types. I think is worth to
discuss a little about new "default modifier" (which is strictly related to
mentioned structures). If needed I can correct details.
As you know (or not) all is based on new modifier - "default" for fields.
Overriding operators like "." or "^" is complicated and not obvious in
The "default" modifier is inspired by "default" modifier for indexed
properties. All of my work is (I hope so) natural for Pascal language. All
FPC tests pass fine (phew!). The idea is quiet simple:
"If all fails - try to use default field".
Example implementation of SmartPointers and SmartObjects available at:
https://github.com/maciej-izak/PascalSmartPointers (see OUTPUT on the end
of each of file)
Tests (very good way to see how it works. NOTE: I need to add few other
tests for functions with var/const/out, "for in do" loop and for arrays and
indexed properties - help with additional tests is welcome):
The way how to obtain pointer can be a little confusing for most of Pascal
programmers. Anyway nothing new for Pascal language. In Pascal we have
little known @@ operator to get pointer to variable which handle pointer to
procedure/function. For records with "default field" @ means "get pointer
to default field" and @@ means "get pointer to record".
Any questions and suggestions are welcome!
Side note: as side effect of new feature we can implement very compatible
ARC objects compatible with Delphi without breaking existing code base and
without ugly additions for TObject like:
function __ObjAddRef: Integer; virtual;
function __ObjRelease: Integer; virtual;
property RefCount: Integer read FRefCount;
procedure DisposeOf; // that can become also part of TObject
All can be delegated into record (record with default field works like
proxy, note: each of method/field/property/operator of record has higher
priority than "default field object"). We can omit easily TComponent
problem for ARC compiler like mentioned here (maybe we can add some hidden
field for TObject similar to hfMonitorOffset from Delphi to point proxy
record for destroy notification - maybe as part of TMonitor):
https://plus.google.com/+DalijaPrasnikar/posts/3CUnrZam6zp (see comments)
more info about DisposeOf:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-devel