[fpc-pascal] Smart Pointers
Maciej Izak
hnb.code at gmail.com
Tue May 10 20:34:30 CEST 2016
2016-05-10 11:26 GMT+02:00 Anthony Walter <sysrpl at gmail.com>:
> Hey I'm all for your improvements. Good work, keep it up, and I actually
> appreciate it. If you want, I can write up an overview of your improvements
> at getlazarus.org and provide precompiled versions for testings/use on
> all platforms (win, mac, linux, raspbian)..
>
glad to hear that ^^. I think is good idea to wait for smart pointers. I
will let you know in PM.
Which probably isn't something you or anyone else has implemented yet
> because of the generic factor. Actually support for type helpers for any
> generic types (classes, records, interfaces, arrays) would be great,
> especially when combined with the work you've done on record initialization
> and finalization.
>
With my private "not committed yet" version of compiler I can compile code
like that (I think the good name for below construction is "specialized
helper" or "forced helper" or just "record with default field"):
{ be aware that code below is experimental }
type
TManagedArray<T> = record
Instance: TArray<T> default;
function Add(A: T): Integer;
end;
function TManagedArray<T>.Add(A: T): Integer;
begin
Result := Length(Instance);
SetLength(Instance, Succ(Result));
Instance[Result] := A;
end;
procedure foo(A: TArray<Integer>);
begin
WriteLn(a[0]); // 10
end;
var
a: TManagedArray<Integer>;
pa: ^TArray<Integer>;
i: Integer;
begin
a.Add(10);
i := a[0]; // compiler magic, equivalent of: i := a.Instance[0];
foo(a); // compiler magic, equivalent of: foo(a.Instance);
pa := @a; // compiler magic, equivalent of: pa := @a.Instance;
WriteLn(pa^[0], ' = ', i); // 10 = 10
end.
--
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160510/fc6d6b0a/attachment.html>
More information about the fpc-pascal
mailing list