Rant after trying Rust a bit

Bruno Queiroga via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 24 16:19:40 PDT 2015


On Friday, 24 July 2015 at 04:42:59 UTC, Walter Bright wrote:
> On 7/23/2015 3:12 PM, Dicebot wrote:
>> On Thursday, 23 July 2015 at 22:10:11 UTC, H. S. Teoh wrote:
>>> OK, I jumped into the middle of this discussion so probably 
>>> I'm speaking
>>> totally out of context...
>>
>> This is exactly one major advantage of Rust traits I have been 
>> trying to
>> explain, thanks for putting it up in much more understandable 
>> way :)
>
> Consider the following:
>
>     int foo(T: hasPrefix)(T t) {
>        t.prefix();    // ok
>        bar(t);        // error, hasColor was not specified for T
>     }
>
>     void bar(T: hasColor)(T t) {
>        t.color();
>     }
>
> Now consider a deeply nested chain of function calls like this. 
> At the bottom, one adds a call to 'color', and now every 
> function in the chain has to add 'hasColor' even though it has 
> nothing to do with the logic in that function. This is the pit 
> that Exception Specifications fell into.
>
> I can see these possibilities:
>
> 1. Require adding the constraint annotations all the way up the 
> call tree. I believe that this will not only become highly 
> annoying, it might make generic code impractical to write 
> (consider if bar was passed as an alias).
>

Could not the compiler just issue a warning of implicit use of 
properties/functions like the C's implicit function declaration 
warning? And some form of "cast" (or test) could be done to make 
the use explicit:

int foo(T: hasPrefix)(T t) {
    t.prefix();    // ok
    t.color();     // Compiler warning: implicit.
    bar(t);        // Compiler warning: implicit.
}

void bar(T: hasColor)(T t) {
    t.color();
    t.prefix();    // Compiler warning: implicit.
}

-------------

int foo(T: hasPrefix)(T t) {
    t.prefix();                  // ok
    (cast(hasColor) t).color();  // ok: explicit.
    bar(cast(hasColor) t);       // ok: explicit.
}

void bar(T: hasColor)(T t) {
    t.color();
}

This seems enough to avoid bugs.

Note: sorry for the bad english writing.

Best regards,
Bruno Queiroga.



More information about the Digitalmars-d mailing list