typeof and block statements

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 12 07:19:42 PDT 2010


On Thu, 12 Aug 2010 09:56:07 -0400, simendsjo <simen.endsjo at pandavre.com>  
wrote:

> On 12.08.2010 15:30, Steven Schveighoffer wrote:
>> On Thu, 12 Aug 2010 09:13:54 -0400, simendsjo
>> <simen.endsjo at 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

First, this compiles for me.  Not sure why it doesn't for you.

Second, stringof is your friend.  Coupled with pragma(msg, ...) It allows  
you to "view" the type of an expression at compile time:

pragma(msg, typeof({return 1;}()).stringof);

Which when compiled on dmd 2.047 linux, prints:

int


>> 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?

Once mandatory parens for non-properties is implemented, the code will be  
changed to r.popFront();

-Steve


More information about the Digitalmars-d-learn mailing list