Understanding isInfinite(Range)

Simen kjaeraas simen.kjaras at gmail.com
Sat Sep 4 05:11:12 PDT 2010


Peter Alexander <peter.alexander.au at gmail.com> wrote:

> == Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
>> On Fri, 03 Sep 2010 11:12:29 -0400, Andrej Mitrovic
>> <andrej.mitrovich at test.com> wrote:
>> > What does char[1 + Range.empty] do? It looks rather cryptic..
>> char[1+Range.empty] is a type.  If Range.empty is a compile-time  
>> constant,
>> then this type is valid, otherwise it's not valid (the is expression
>> results to true if the argument is a valid type).
>> If it's valid, then Range.empty never changes.  If it never changes and
>> it's always false, then it's infinite.
>> -Steve
>
> That's really ugly code :-(
>
> Is there a way you could write an isStatic(expr) template? Using  
> something like that would make the
> code a hell of a lot more readable. At the moment, the code itself does  
> a very poor job of conveying
> what it's trying to accomplish.
>
> These SFINAE-like tricks should be black-boxed as much as possible, or  
> (at the very least)
> commented so that people know what's going on.

template isStatic( alias T ) {
     enum isStatic = is( char[1+T] );
}

unittest {
     int n = 3;
     assert( !isStatic!n );
     assert( isStatic!1 );
     enum r = 5;
     assert( isStatic!r );
}
-- 
Simen


More information about the Digitalmars-d-learn mailing list