The bizarre world of typeof()

Don nospam at nospam.com
Mon Oct 26 04:02:20 PDT 2009


Ary Borenszweig wrote:
> Don wrote:
>> I'm trying to make sense of the rules for 'typeof'. It's difficult 
>> because DMD's behaviour is so different to the spec. Here's four 
>> simple cases.
>>
>> // This doesn't compile on D1.
>> //alias typeof(int*int) Alias1;
> 
> Not valid: typeof accepts an expression and "int*int" is not a valid 
> expression.

Agreed.
> 
>>
>> // This compiles in D1, but not in D2.
>> alias int Int;
>> alias typeof(Int*Int) Alias2;
> 
> Almost same as above: Int resolves to a type and "type*type" is not a 
> valid expression.

Agreed.

>>
>> // Yet this DOES compile on D2 !
>> typeof(T*U) foo(T, U)(T x, U y) { return x*y; }
>> alias typeof(foo(Int, Int)) Alias3;
> 
> Of course, because this doesn't translate to "Int*Int", this translates 
> to some variables x and y of type "Int" and "Int" respectively for which 
> you can do "x*y".
How does it get from Int to an instance of type Int?
The first issue is typeof( T * U ). T and U are not variables, they are 
types.






More information about the Digitalmars-d mailing list