[Dlang-study] [lifetime] Few root decisions to take on RC classes
Andrei Alexandrescu
andrei at erdani.com
Mon Nov 2 07:46:56 PST 2015
On 11/02/2015 10:38 AM, Martin Nowak wrote:
> On 11/02/2015 01:20 AM, Andrei Alexandrescu wrote:
>> class Widget
>> static Widget g;
>> void fun()
>> {
>> g = this;
>> }
>> }
>>
>> If we make the entire Widget reference-counted, the compiler knows how
>> to make the code work. If we go the @noescape route, this code and
>> others like it will be statically disallowed. So we end up with
>> something more restrictive (or more complicated if we decide to allow
>> certain escape patterns).
>
> Why are we trying so hard to make bad code work? Sharing the ownership
> of a class from within a method can be very surprising and can always be
> replaced by sharing with the function (passing as argument)/assigning on
> the callsite.
The example is chosen to be simple, not good or representative. In real
code shared ownership is both frequent and desirable.
> Even more important the @rc scheme doesn't seem to solve a much more
> common use case.
>
> class Widget
> {
> void fun()
> {
> escapingReferenceToFoo(field);
> }
> Foo field;
> }
Is Foo a class type or not?
Andrei
More information about the Dlang-study
mailing list