[fpc-devel] "Default" discussion for SmartPointers etc
Michael Van Canneyt
michael at freepascal.org
Thu Jul 28 16:37:37 CEST 2016
On Thu, 28 Jul 2016, Maciej Izak wrote:
> 2016-07-28 14:59 GMT+02:00 Michael Van Canneyt <michael at freepascal.org>:
>
>> Because with the exception of the ^ operator, I see no need for any special
>> constructions to achieve a "nullable type", except maybe some implicit
>> constructor/destructor ?
>>
>
> procedure with var parameter is special case, we need somehow to perform
> backward compatibility and strong typing is the must. In the fact
> TNullable<Integer> is proxy type to field of ^Integer type. ^ operator
> exist to make life easier in comparison to C# implementation.
>
> "default field" is not necessary for nullable types but is extremely useful
> addition, more options and optimization for end user.
Can you please explain this, because as far as I can see from your
explanation, all that it does is make 2 assignment operators and maybe a
typecast operator unnecessary ?
(in which case it falls under the category syntactic sugar, like "for ..
in", which is fine for me)
Are there additional benefits I have missed ?
> We can exclude
> completely possibility of usage ^ operator for nullable types (^ for
> nullable type is just my invention). In that case calling functions like
> test1 will be impossible. Just pure copy of Nullable types from C# -,- . So
> you are right - ascetic version of nullable type need only management
> operators (aka implicit constructor/destructor). Presented nullable type is
> not "pure" copy of C# implementation, presented above implementation has
> Pascal spirit and optimizations impossible to achieve in other languages.
Well, I think this addition is not necessary, maybe even dangerous.
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 :-)
So, currently balance for me, if I understood everything correctly:
[+] Implicit constructor/destructor.
Good for simulating 'managed' types.
[+] Default field (even if it is only syntactic sugar)
[-] ^ dereference magic: a no-no :)
Michael.
More information about the fpc-devel
mailing list