Rvalue references - The resolution

Walter Bright newshound2 at digitalmars.com
Sat May 4 17:47:00 PDT 2013


On 5/4/2013 5:02 PM, deadalnix wrote:
> On Saturday, 4 May 2013 at 23:30:01 UTC, Walter Bright wrote:
>> On 5/4/2013 3:50 PM, deadalnix wrote:
>>> Require isn't the right word, or you hav to explain yourself much more.
>>
>> You need an explicit annotation if a ref parameter is returned by ref by that
>> function. This is what Rust's annotations do.
>>
>> Consider:
>>
>>     ref T foob(ref U u) { return u.t; }
>>
>>     ref T bar() { U u; return foob(u); }
>>
>> The compiler cannot know that the ref return of foob is referring to local u
>> (as opposed to, say, a ref to a global) unless it is annotated to say so. Rust
>> is no different.
>
> This code sample won't require any annotation in Rust.

If the compiler accepts that code, it will crash at runtime. If it doesn't 
accept that code, then it will also disallow legitimate code like:

      ref T foob(ref U u) { static T t; return t; }

      ref T bar() { U u; return foob(u); }

> And it illustrate
> wonderfully what I'm saying : most people in the discussion (and it has been
> shown now that this includes you) were unaware of how does Rust solve the problem.
>
> I don't think excluding a solution that isn't understood is the smartest thing
> to do.

I suggest you enumerate the cases with a Rust-like system and show us how it 
solves the problem without annotations. Note that Rust has pretty much zero real 
world usage - it's one thing to say needing to use annotations is 'rare' and 
another to know it based on typical usage patterns of the language.

For example, if the default is "assume the ref return refers to the ref 
parameter", then some containers would require the annotation and some would 
not. This is not very viable when doing generic coding, unless you are willing 
to provide two copies of each such function - one with the annotations and the 
other without.

Note also that if you have A calls B calls C, the annotation on C doesn't 
propagate up to B, again leading to a situation where you're forced to make two 
versions of the functions.

(I say doesn't propagate because in a language that supports separate 
compilation, all the compiler knows about a function is its signature.)



More information about the Digitalmars-d mailing list