On 12.08.2010 15:30, Steven Schveighoffer wrote:
On Thu, 12 Aug 2010 09:13:54 -0400, simendsjo
<simen.end...@pandavre.com> wrote:

The spec doesn't mention anything about block statements in typeof
declarations.

//typeof({1}) a; // found } expecting ;
//typeof({1}()) b; // same as a
typeof(1) c; // int

{...} is a function literal, a lambda function if you will.

I thought parameterless delegates were written () {}..

Your lambda function contains a syntax error, the single line in it does
not end in a semicolon.

Of course, if you made it:

{1;}

I think it might fail anyways, because 1; is not a statement.

Why doesn't this work then?
typeof({return 1;}()) a; // found 'a' when expecting ';' following statement


I'm asking because isInputRange from std.range the idom from the b test:

template isInputRange(R)
{
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
}()));
}

is(typeof(...)) is sort of a hack to determine if something compiles or
not. If it does, then there will be a type associated with the
expression, if not, then there will be no type. There is also a
__traits(compiles, ...) which I think really should be used for this
purpose, but the isInputRange may predate that idiom.

Essentially, the isInputRange bool is true if the function literal that
contains those four statements compiles. What it translates to is, Does
R support the functions necessary for input ranges.

I see a syntax error there, r.popFront is not a property, so it should
look like this:

r.popFront();

It only works right now because mandatory () for non-properties is not
implemented in the compiler yet.

So once this is implemented, this template is always false?



Also... The unittest contains
static assert(isInputRange!(int[]));
static assert(isInputRange!(char[]));

But arrays doesn't include these methods.. I don't understand a thing :(

Arrays support "tacking on" extra methods to it. Essentially, for arrays
(and arrays only), the compiler will translate this:

arr.foo()

to this:

foo(arr)

See http://www.digitalmars.com/d/2.0/arrays.html#func-as-property

Thanks!

Reply via email to