Understanding isInfinite(Range)

Mafi mafi at example.org
Mon Sep 6 12:40:21 PDT 2010


Am 06.09.2010 21:24, schrieb Andrej Mitrovic:
> Apparently I can't post to D.learn from gmail without waiting for a review? What the..?
>
> Anyway, I've posted this:
>
> On a related note, I always wanted to make a template to replace the
> dreaded is(typeof('delegate literal'())); calls.
>
> For example, instead of this:
>
> enum bool isInputRange = is(typeof(
> {
>     R r;             // can define a range object
>     if (r.empty) {}  // can test for empty
>     r.popFront;          // can invoke next
>     auto h = r.front; // can get the front of the range
> }()));
>
> We'd have a much cleaner call like so:
>
> enum bool isInputRange = validate!(
> {
>     R r;             // can define a range object
>     if (r.empty) {}  // can test for empty
>     r.popFront;          // can invoke next
>     auto h = r.front; // can get the front of the range
> });
>
> But I haven't found a way to do it properly. If I call validate on a
> type R range which doesn't feature the empty() method, then no matter
> what the definition of validate is the compiler will error out because
> it sees the call to r.empty() in the function literal, and 'r' doesn't
> have an empty method.
>

You could just let your template get a string. Then
validate!q{....}
should work. It looks almost exactly the same but has a 'q'.

Maybe you could even create an template overload which gets delegate 
which has always failing static assert with message "you forget the 'q'" 
but I don't know if template initialization comes before semantic 
analisys so I'm not sure this will work.

Mafi




More information about the Digitalmars-d-learn mailing list