Scale advocacy

Ali Çehreli acehreli at yahoo.com
Sun Jul 3 13:26:49 PDT 2011


On Sun, 03 Jul 2011 20:07:43 +0000, Ali Çehreli wrote:

> import std.range;
> 
> // ...
> 
> ElementType!Range front_or_null(Range)(Range range) {
>     return range.empty ? null : range.front;
> }
> 
> The template constraint has proven to be problematic though. I will open
> a separate thread about that.

I've figured it out: I've been using "== null" instead of "is null". This 
fails:

import std.range;

ElementType!Range front_or_null(Range)(Range range)
    if (__traits(compiles,
                 { bool result = ElementType!Range.init == null; } ))
{
    return range.empty ? null : range.front;
}

class C
{}

void main()
{
    C[] c;
    front_or_null(c);
}

Replacing "==" with "is" works:

                 { bool result = ElementType!Range.init is null; } ))

I would have liked this simpler syntax to work:

    if (__traits(compiles, { ElementType!Range.init is null; } ))

but it fails without really telling why. Only if I move that expression 
into a code context that I can understand what the problem is. Now trying 
to compile this:

    C.init is null;

produces this error:

Error: is has no effect in expression (null is cast(C)null)

I am grateful for the warning but it was puzzling for a while why the 
simple template constraint was not working. I guess it was a good 
decision to suppress the compiler errors in __traits(compiles). I wonder 
whether __traits(compiles) can be made less strict by eliminating the 
need for the silly 'bool result' in the delegate above.

But this is not a pressing issue at all. :)

Ali


More information about the Digitalmars-d-learn mailing list