bug in typeof or wrong enum specs?
Jacob Carlborg
doob at me.com
Wed Aug 28 23:44:16 PDT 2013
On 2013-08-29 01:28, captaindet wrote:
> a recent discussion (
> http://forum.dlang.org/thread/kvje4r$1tff$1@digitalmars.com ) about the
> official enum dox ( http://dlang.org/enum.html ) was not conclusive whether
>
> enum IDENTIFIER;
>
> is officially allowed/supported. jacob pointed out that it has an
> important use case in that it can serve as UDA. as UDAs are fairly new,
> this cannot be the reason why this syntax was allowed in the first place
> though, *if* it is allowed.
No, but now it's a useful use case.
> also, it might be used in meta stuff similar
> to "#define IDENTIFIER" in C - playing with this idea i run into this
> issue...
>
> while much code behaves with such an empty enum declaration,
>
> writeln( __traits(compiles, IDENTIFIER) ); // true
> writeln( is( IDENTIFIER == enum ) ); // true
>
> typeof() is not happy at all (DMD 2.063.2):
>
> writeln( typeof(IDENTIFIER).stringof );
> // Error: argument IDENTIFIER to typeof is not an expression
>
> typeof() expects an expression and a bare identifier is a
> "PrimaryExpression" ( http://dlang.org/expression.html#PrimaryExpression
> ) and hence a valid argument.
>
> either the empty enum declaration is not allowed (and should be removed
> from the dox and throw a compilation error) or there is a bug in typeof().
No, I previously misread your post.
enum foo;
The above declares a type, with the name "foo".
enum bar = 1;
Declares a manifest constant, short for:
enum int bar = 1;
The type is "int", the name of the constant is "bar".
typeof(bar); // ok, "bar" is not a type
typeof(int); // error, "int" is a type
typeof(foo); // error, "foo" is a type
Although I do think that typeof(type) should work and just return "type".
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list