Understanding isInfinite(Range)

Andrej Mitrovic andrej.mitrovich at test.com
Mon Sep 6 12:24:34 PDT 2010


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.

Philippe Sigaud Wrote:

> On Mon, Sep 6, 2010 at 18:47, Pelle <pelle.mansson at gmail.com> wrote:
> 
> > On 09/04/2010 02:11 PM, Simen kjaeraas wrote:
> >
> >> Is there a way you could write an isStatic(expr) template? Using
> >>>
> >>
> >> 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 );
> >> }
> >>
> >
> > enum s = "Hello";
> >
> > assert (isStatic!s);
> >
> > Gonna need more work than that.
> >
> 
> Why? That's exactly the behavior we want, or so it seems to me.
> 
> <div class="gmail_quote">On Mon, Sep 6, 2010 at 18:47, Pelle <span dir="ltr">&lt;<a href="mailto:pelle.mansson at gmail.com">pelle.mansson at gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
> <div><div></div><div class="h5">On 09/04/2010 02:11 PM, Simen kjaeraas wrote:<br>
> <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
> 
> Is there a way you could write an isStatic(expr) template? Using<br></blockquote>
> <br>
> template isStatic( alias T ) {<br>
> enum isStatic = is( char[1+T] );<br>
> }<br>
> <br>
> unittest {<br>
> int n = 3;<br>
> assert( !isStatic!n );<br>
> assert( isStatic!1 );<br>
> enum r = 5;<br>
> assert( isStatic!r );<br>
> }<br>
> </blockquote>
> <br></div></div>
> enum s = &quot;Hello&quot;;<br>
> <br>
> assert (isStatic!s);<br>
> <br>
> Gonna need more work than that.<br>
> </blockquote></div><br>Why? That's exactly the behavior we want, or so it seems to me.<br><br><br>
> 



More information about the Digitalmars-d-learn mailing list