According to the spec (in declaration.html), the argument to typeof() must be an expression. This is not currently enforced by DMD. In fact, it can be any type, except for builtin types which fail with a misleading error message. alias int foo; typeof(foo) a; // ok typeof(int) b; // FAILS: found ')' when expecting '.' following 'int' typeof(typeof(foo)) c; // ok, even though the argument is obviously a type. I think that the compiler's behaviour generally makes more sense than the spec (and I'm finding it to be useful behaviour). Enforcing the rule in the spec would mean the compiler would need to determine if 'foo' is a type name before issuing a syntax error --> violation of independence of syntax and semantic passes. So I suggest that the spec be changed to allow typeof(Type) to be legal, and to adjust the compiler to accept built-in types. eg, make typeof(int) synonymous with int.
According to the spec, typeof(typeof(foo)) shouldn't compile. As you've probably noticed already, the syntax is Typeof: typeof ( Expression ) and this is parseable only as a type and not as an expression. I can see that there might be some generic programming use in defining typeof(X) == X if X is already a type. The only problem is that it isn't documented. But if what's in the brackets is already not parseable as an Expression, there's no benefit to writing it this way. As such, it's probably reasonable to disallow it as it would mean yet another ambiguity in the grammar.
I agree with Stewart, neither should compile.
So the resolution would be that: typeof(X) would generate an error late in the semantic pass if X turns out to be a type, rather than an expression?
Yes.
One thing worries me about this... in D1.0, a common metaprogramming idiom is "is(typeof(xxx))" For example, this compiles in D1.0: ------ static assert(!is(typeof(nonexistent))); ------ Lots of code (including BLADE, for example) will fail if that stops working. Please make sure that doesn't get broken in fixing this bug!
How do you work that out?
Changed the description of this bug in view of the discussion above. The fear I mentioned above is probably irrational.
Fixed DMD1.032
*** Bug 1777 has been marked as a duplicate of this bug. ***
Fixed dmd 1.032 and 2.016
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル