digitalmars.D - [dox] enum specs vs reality
- captaindet (18/18) Aug 27 2013 i admit that i am not very good at reading/understanding language defini...
- Andre Artus (26/32) Aug 27 2013 You are right. Given the rules marked with asterisks below your
- Jacob Carlborg (7/11) Aug 28 2013 I haven't looked at this in TPL but the above is a manifest constant.
- captaindet (7/17) Aug 28 2013 don't know what you mean. since they are defined with the enum keyword t...
- Jacob Carlborg (11/18) Aug 28 2013 The above is short for:
- Jacob Carlborg (10/33) Aug 28 2013 That doesn't look entirely correct. Currently the docs read:
- captaindet (12/19) Aug 28 2013 a) so are you saying
- Jacob Carlborg (9/21) Aug 28 2013 Yes, it's sometimes usable to be able to declare dummy types like this.
- Andrej Mitrovic (3/7) Aug 28 2013 Good tip, I used to use structs for this but I wanted something
- H. S. Teoh (5/15) Aug 28 2013 Good idea! I also don't like using structs everywhere just for UDA's.
- Jacob Carlborg (10/17) Aug 28 2013 Actually, I previously misread this. This is working as it should.
- Maxim Fomin (4/5) Aug 28 2013 I suppose this is legal for the same reasons as class A; or
- Jacob Carlborg (6/9) Aug 28 2013 It's useful for UDA's:
i admit that i am not very good at reading/understanding language definition syntax. but yet i think the given enum specs ( http://dlang.org/enum.html ) are not quite in order. they seem to imply that both enum ; enum WhatAmI ; are correct. while the first one throws an error as expected, the second one passes and is partially usable, potentially similar to C's #define OPTION. however, typedef's throwing of an error makes me doubt that this is legal: ---- import std.stdio, std.traits; enum test; // passes but is it really legal? int main(string[] args) { writeln( __traits(compiles, test) ); // true writeln( is( test == enum ) ); // true writeln( isBasicType!(test) ); // true writeln( isSomeFunction!(test) ); // false // writeln( typeof(test).stringof ); // Error: argument test to typeof is not an expression return 0; }
Aug 27 2013
On Tuesday, 27 August 2013 at 23:52:59 UTC, captaindet wrote:i admit that i am not very good at reading/understanding language definition syntax. but yet i think the given enum specs ( http://dlang.org/enum.html ) are not quite in order. they seem to imply that both enum ; enum WhatAmI ;You are right. Given the rules marked with asterisks below your examples would be legal. EnumDeclaration: * enum EnumTag EnumBody * enum EnumBody enum EnumTag : EnumBaseType EnumBody enum : EnumBaseType EnumBody EnumBody: * EmptyEnumBody EnumMembersBody EmptyEnumBody: * ; Going by the description on the linked page EmptyEnumBody should probably be something like this: EmptyEnumBody: EnumMember ; Although going by examples [2] from Andrei's book ("The D Programming Language"), it seems the following is valid: EmptyEnumBody: EnumMembers ; 2. The example from p.69 [TDPL]. It seems to be missing a colon after "enum". enum size_t g_maxDataSize = 100_000_000, g_maxMemory = 1_000_000_000; I hope someone can clear up what is and isn't a valid enum.
Aug 27 2013
On 2013-08-28 04:27, Andre Artus wrote:2. The example from p.69 [TDPL]. It seems to be missing a colon after "enum". enum size_t g_maxDataSize = 100_000_000, g_maxMemory = 1_000_000_000; I hope someone can clear up what is and isn't a valid enum.I haven't looked at this in TPL but the above is a manifest constant. Which basically has nothing to do with enums. It's a way to declare a constant that doesn't have any storage and which address cannot be taken. Basically the same as "#define foo 0" in C. -- /Jacob Carlborg
Aug 28 2013
On 2013-08-28 02:21, Jacob Carlborg wrote:On 2013-08-28 04:27, Andre Artus wrote:don't know what you mean. since they are defined with the enum keyword they have everything to do with enum, especially the official syntax presented here: http://dlang.org/enum.html enum keyword covers both, enumeration constants and manifest constants. the specs cover both in one. moreover, they explain that manifest constants are only syntactic sugar for anonymous enums: enum { A = 2, B = 4 } is the same as enum A = 2; enum B = 4;2. The example from p.69 [TDPL]. It seems to be missing a colon after "enum". enum size_t g_maxDataSize = 100_000_000, g_maxMemory = 1_000_000_000; I hope someone can clear up what is and isn't a valid enum.I haven't looked at this in TPL but the above is a manifest constant. Which basically has nothing to do with enums. It's a way to declare a constant that doesn't have any storage and which address cannot be taken. Basically the same as "#define foo 0" in C.
Aug 28 2013
On 2013-08-28 17:26, captaindet wrote:enum keyword covers both, enumeration constants and manifest constants. the specs cover both in one. moreover, they explain that manifest constants are only syntactic sugar for anonymous enums: enum { A = 2, B = 4 } is the same as enum A = 2; enum B = 4;The above is short for: enum int A = 2; enum int B = 4; They declare manifest constants, not types. It's the same as: immutable int A = 2; But you can't take the address of "A", which you can if it's declared as immutable. -- /Jacob Carlborg
Aug 28 2013
On 2013-08-28 01:53, captaindet wrote:i admit that i am not very good at reading/understanding language definition syntax. but yet i think the given enum specs ( http://dlang.org/enum.html ) are not quite in order. they seem to imply that both enum ; enum WhatAmI ;That doesn't look entirely correct. Currently the docs read: EnumDeclaration: enum EnumBody Should probably be: EnumDeclaration: enum EnumMembersBody:are correct. while the first one throws an error as expected, the second one passes and is partially usable, potentially similar to C's #define OPTION. however, typedef's throwing of an error makes me doubt that this is legal: ---- import std.stdio, std.traits; enum test; // passes but is it really legal? int main(string[] args) { writeln( __traits(compiles, test) ); // true writeln( is( test == enum ) ); // true writeln( isBasicType!(test) ); // true writeln( isSomeFunction!(test) ); // false // writeln( typeof(test).stringof ); // Error: argument test to typeof is not an expression return 0; }The last one will fail since "typeof" expects an expression and not a type. -- /Jacob Carlborg
Aug 28 2013
On 2013-08-28 02:26, Jacob Carlborg wrote:That doesn't look entirely correct. Currently the docs read: EnumDeclaration: enum EnumBody Should probably be: EnumDeclaration: enum EnumMembersBody:agreed.The last one will fail since "typeof" expects an expression and not a type.a) so are you saying enum WhatAmI; is legal? (just asking because i don't know) b) what typeof expects/tolerates seems to be a bit of a minefield by itself. enum test = true; writeln( typeof(test).stringof ); //prints: bool enum wtf; writeln( typeof(wtf).stringof ); //Error: argument wtf to typeof is not an expression /det
Aug 28 2013
On 2013-08-28 17:34, captaindet wrote:Yes, it's sometimes usable to be able to declare dummy types like this. Especially now when we have UAD's (User Defined Attribute): enum foo; foo bar ();The last one will fail since "typeof" expects an expression and not a type.a) so are you saying enum WhatAmI; is legal? (just asking because i don't know)b) what typeof expects/tolerates seems to be a bit of a minefield by itself. enum test = true; writeln( typeof(test).stringof ); //prints: bool enum wtf; writeln( typeof(wtf).stringof ); //Error: argument wtf to typeof is not an expressionThat seems strange. Perhaps worth a bugzilla report: http://d.puremagic.com/issues/ -- /Jacob Carlborg
Aug 28 2013
On 8/28/13, Jacob Carlborg <doob me.com> wrote:Yes, it's sometimes usable to be able to declare dummy types like this. Especially now when we have UAD's (User Defined Attribute): enum foo; foo bar ();Good tip, I used to use structs for this but I wanted something non-instantiable.
Aug 28 2013
On Wed, Aug 28, 2013 at 09:12:44PM +0200, Andrej Mitrovic wrote:On 8/28/13, Jacob Carlborg <doob me.com> wrote:Good idea! I also don't like using structs everywhere just for UDA's. T -- MSDOS = MicroSoft's Denial Of ServiceYes, it's sometimes usable to be able to declare dummy types like this. Especially now when we have UAD's (User Defined Attribute): enum foo; foo bar ();Good tip, I used to use structs for this but I wanted something non-instantiable.
Aug 28 2013
On 2013-08-28 17:34, captaindet wrote:b) what typeof expects/tolerates seems to be a bit of a minefield by itself. enum test = true; writeln( typeof(test).stringof ); //prints: bool enum wtf; writeln( typeof(wtf).stringof ); //Error: argument wtf to typeof is not an expressionActually, I previously misread this. This is working as it should. The first declares a manifest constant: enum test = true Is short for: enum bool test = ture; "bool" is the type, "test" is the name. The second declares a new type, where "wtf" is the name of the type. -- /Jacob Carlborg
Aug 28 2013
On Tuesday, 27 August 2013 at 23:52:59 UTC, captaindet wrote:enum WhatAmI ;I suppose this is legal for the same reasons as class A; or struct S; is legal - forward declaration (although in case of enums it is pretty useless).
Aug 28 2013
On 2013-08-28 19:31, Maxim Fomin wrote:I suppose this is legal for the same reasons as class A; or struct S; is legal - forward declaration (although in case of enums it is pretty useless).It's useful for UDA's: enum foo; foo bar (); -- /Jacob Carlborg
Aug 28 2013