typeof and block statements

Steven Schveighoffer schveiguy at yahoo.com
Thu Aug 12 07:39:17 PDT 2010


On Thu, 12 Aug 2010 10:29:41 -0400, simendsjo <simen.endsjo at pandavre.com>  
wrote:

> On 12.08.2010 16:19, Steven Schveighoffer wrote:
>> On Thu, 12 Aug 2010 09:56:07 -0400, simendsjo
>> <simen.endsjo at pandavre.com> wrote:
>>
>>> 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
>>
>
> Tested with dmd 2.048 on win7.
>
> pragma(msg, "module:"~typeof({return 1;}()).stringof);
>
> void main()
> {
> 	pragma(msg, "main:"~typeof({return 1;}()).stringof);
> 	//typeof({return 1;}()) a; // found 'a' when expecting ';' following  
> statement
> }
>
> prints
> module:int
> main:int
>

Ahh.  It fails on 2.047 also if you put the statement inside main.  If you  
declare it outside main, it works.

Looks like a bug.  Please file http://d.puremagic.com/issues/enter_bug.cgi

-Steve


More information about the Digitalmars-d-learn mailing list