DIP77 - Fix unsafe RC pass by 'ref'

Martin Nowak via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 10 14:20:10 PDT 2015


On 04/10/2015 12:29 PM, Walter Bright wrote:
>>
>> So someone passes an RCO via ref to avoid the inc/dec, and because
>> that imposes
>> safety issues we turn it into some sort of pass by value under the hood,
>> defeating the purpose, and provide an opt-out via @system opAssign.
> 
> Or you could pass it by const ref (which is what Rust essentially does).

Maybe I'm missing something, but this proposal seems to make `ref RCO`
fairly useless, because it creates a copy anyhow.

>> Wouldn't it more straightforward to make pass-by-ref unsafe (@system)
>> for RCOs?
> 
> That's what we have now. It's not good enough.

Assigning a RefCounted is marked @system, pass-by-ref is @safe.
What's missing, you want to be able to use RefCounted in @safe code? Why
not pass it by value then? That would pin the object and you could elide
the additional inc/dec just like you propose to elide the temporary copies.
It's more efficient to pass a smart pointer like RefCounted by-value anyhow.


More information about the Digitalmars-d mailing list