www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - enhancing enums

reply hehe45 <a3161739 uggsrock.com> writes:
In c++ it is valid syntax to have trailing commas at the and of enum
definitions:
enum {a, b, c, };
This would be a useful addition to D too.

The enum class syntax from c++0x should also adopted by D, this would allow
named enums which are not automatically encased by a namespace:

enum EnumName {A, B, C};       ---> A, B and C are global constants
enum class EnumName {A, B, C};---> A, B and C are in the EnumName namespace
Dec 08 2009
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
hehe45:

 In c++ it is valid syntax to have trailing commas at the and of enum
definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
I think that's already possible in D1.
 The enum class syntax from c++0x should also adopted by D, this would allow
named enums which are not automatically encased by a namespace:
 
 enum EnumName {A, B, C};       ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName namespace
Global constants are a little dangerous, I don't like that. Bye, bearophile
Dec 08 2009
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:hfmlkl$1kil$1 digitalmars.com...
 hehe45:

 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
I think that's already possible in D1.
That's right. And I think it's also allowed for array literals. Very handly feature. Although I had no idea that C++ also allowed it.
Dec 08 2009
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 12/08/2009 06:35 PM, Nick Sabalausky wrote:
 "bearophile"<bearophileHUGS lycos.com>  wrote in message
 news:hfmlkl$1kil$1 digitalmars.com...
 hehe45:

 In c++ it is valid syntax to have trailing commas at the and of enum
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
I think that's already possible in D1.
That's right. And I think it's also allowed for array literals. Very handly feature. Although I had no idea that C++ also allowed it.
it's only allowed for array initializers
Dec 08 2009
parent "Nick Sabalausky" <a a.a> writes:
"Ellery Newcomer" <ellery-newcomer utulsa.edu> wrote in message 
news:hfmssk$2n2d$1 digitalmars.com...
 On 12/08/2009 06:35 PM, Nick Sabalausky wrote:
 "bearophile"<bearophileHUGS lycos.com>  wrote in message
 news:hfmlkl$1kil$1 digitalmars.com...
 hehe45:

 In c++ it is valid syntax to have trailing commas at the and of enum
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
I think that's already possible in D1.
That's right. And I think it's also allowed for array literals. Very handly feature. Although I had no idea that C++ also allowed it.
it's only allowed for array initializers
Oh yea, that's right. A real strange inconsistency. I hope that gets fixed for D2.
Dec 08 2009
prev sibling parent "Phil Deets" <pjdeets2 gmail.com> writes:
On Tue, 08 Dec 2009 17:57:25 -0500, bearophile <bearophileHUGS lycos.com>  
wrote:

 hehe45:

 In c++ it is valid syntax to have trailing commas at the and of enum  
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
I think that's already possible in D1.
and in D2.
Dec 08 2009
prev sibling next sibling parent reply Bill Baxter <wbaxter gmail.com> writes:
On Tue, Dec 8, 2009 at 2:17 PM, hehe45 <a3161739 uggsrock.com> wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum defi=
nitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would all=
ow named enums which are not automatically encased by a namespace:
 enum EnumName {A, B, C}; =A0 =A0 =A0 ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName namespa=
ce

That's basically what it is now:

enum { A,B,C }  --> A,B,C global
enum EnumName {A,B,C} --> EnumName.A, EnumName.B and EnumName.C

The thing lacking from D is C++'s "using namespace EnumName" to bring
A,B,C into the current namespace.  Closest thing is the much reviled
"with" which I think works with an enum name, but I could be wrong.
Not very useful either way, though, because usually you don't want a
new scope.

--bb
Dec 08 2009
parent reply "Nick Sabalausky" <a a.a> writes:
"Bill Baxter" <wbaxter gmail.com> wrote in message 
news:mailman.581.1260313623.20261.digitalmars-d puremagic.com...
On Tue, Dec 8, 2009 at 2:17 PM, hehe45 <a3161739 uggsrock.com> wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are >not automatically encased by a namespace:

 enum EnumName {A, B, C}; ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
That's basically what it is now: enum { A,B,C } --> A,B,C global
The difference though is that doesn't make A, B and C part of a new type. Personally, I'm fine with that though (well, except for the whole "let's pretend manifest constants are an 'enum'" absurdity), because I *hate* enums that pollute the namespace with their members.
Dec 08 2009
parent reply Mike Parker <aldacron gmail.com> writes:
Nick Sabalausky wrote:
 "Bill Baxter" <wbaxter gmail.com> wrote in message 
 news:mailman.581.1260313623.20261.digitalmars-d puremagic.com...
 On Tue, Dec 8, 2009 at 2:17 PM, hehe45 <a3161739 uggsrock.com> wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are >not automatically encased by a namespace:

 enum EnumName {A, B, C}; ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
That's basically what it is now: enum { A,B,C } --> A,B,C global
The difference though is that doesn't make A, B and C part of a new type.
Which is a very good thing, from my perspective. I cut down the size of the Derelict libraries quite a bit when I converted all the named enums and other constants into anonymous enums, thereby eliminating all the TypeInfo objects. In that particular case, TypeInfo is just useless bloat, IMO.
 Personally, I'm fine with that though (well, except for the whole "let's 
 pretend manifest constants are an 'enum'" absurdity), because I *hate* enums 
 that pollute the namespace with their members.
 
 
 
Dec 08 2009
parent reply "Nick Sabalausky" <a a.a> writes:
"Mike Parker" <aldacron gmail.com> wrote in message 
news:hfmvkn$nvv$1 digitalmars.com...
 Nick Sabalausky wrote:
 "Bill Baxter" <wbaxter gmail.com> wrote in message 
 news:mailman.581.1260313623.20261.digitalmars-d puremagic.com...
 On Tue, Dec 8, 2009 at 2:17 PM, hehe45 <a3161739 uggsrock.com> wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are >not automatically encased by a namespace:

 enum EnumName {A, B, C}; ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
That's basically what it is now: enum { A,B,C } --> A,B,C global
The difference though is that doesn't make A, B and C part of a new type.
Which is a very good thing, from my perspective. I cut down the size of the Derelict libraries quite a bit when I converted all the named enums and other constants into anonymous enums, thereby eliminating all the TypeInfo objects. In that particular case, TypeInfo is just useless bloat, IMO.
I would think that could be optimized away. Maybe it would be a linker optimization though.
Dec 08 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
Nick Sabalausky wrote:
 "Mike Parker" <aldacron gmail.com> wrote in message 
 news:hfmvkn$nvv$1 digitalmars.com...
 Nick Sabalausky wrote:
 "Bill Baxter" <wbaxter gmail.com> wrote in message 
 news:mailman.581.1260313623.20261.digitalmars-d puremagic.com...
 On Tue, Dec 8, 2009 at 2:17 PM, hehe45 <a3161739 uggsrock.com> wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are >not automatically encased by a namespace:

 enum EnumName {A, B, C}; ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
That's basically what it is now: enum { A,B,C } --> A,B,C global
The difference though is that doesn't make A, B and C part of a new type.
Which is a very good thing, from my perspective. I cut down the size of the Derelict libraries quite a bit when I converted all the named enums and other constants into anonymous enums, thereby eliminating all the TypeInfo objects. In that particular case, TypeInfo is just useless bloat, IMO.
I would think that could be optimized away. Maybe it would be a linker optimization though.
If i remember right there is some code in phobos that relies on typeinfos to do safe string formatting and integer to identifier conversion for enums. Still I agree that there should be a way to turn off typeinfo generation for most types (except classes of course, that would screw up dynamic casts). One way would be to have a switch like --minimal-typeinfo that reduces all TypeInfo instances to a mere int that is an index into a primitives array [byte, short, int, long, ubyte, ushort, uint, ulong, float, double, real, ...] to still enable the routines that relies on typeinfos to work. That way even if you have a thousands enum's, their combined typeinfo data is 4 bytes long in the executable.
Dec 08 2009
prev sibling next sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 08-12-2009 o 23:17:55 hehe45 <a3161739 uggsrock.com> napisa=B3(a):

 In c++ it is valid syntax to have trailing commas at the and of enum  =
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
Please no. I've done some C++ but the "need semicolon after brace?" is = always something I need to think twice about.
 The enum class syntax from c++0x should also adopted by D, this would =
=
 allow named enums which are not automatically encased by a namespace:

 enum EnumName {A, B, C};       ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName  =
 namespace
D enums are already like that. Use with(EnumName) to get a temporary = relief from prefixing constants with EnumName all the time. Tomek
Dec 08 2009
parent "Nick Sabalausky" <a a.a> writes:
"Tomek Sowiński" <just ask.me> wrote in message 
news:op.u4m6tf2eot0hzo las-miodowy...
Dnia 08-12-2009 o 23:17:55 hehe45 <a3161739 uggsrock.com> napisał(a):

 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
Please no. I've done some C++ but the "need semicolon after brace?" is always something I need to think twice about.
I don't think he was talking about the semicolon, just the comma between the "c" and the "}".
Dec 08 2009
prev sibling parent reply Jeremie Pelletier <jeremiep gmail.com> writes:
hehe45 wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum
definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.
 
 The enum class syntax from c++0x should also adopted by D, this would allow
named enums which are not automatically encased by a namespace:
 
 enum EnumName {A, B, C};       ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName namespace
I don't think that's necessary, you could simply do: typedef int EnumName; enum : EnumName {A, B, C} And your global scope is happily polluted!
Dec 08 2009
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Jeremie Pelletier wrote:
 hehe45 wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are not automatically encased by a namespace:

 enum EnumName {A, B, C};       ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
I don't think that's necessary, you could simply do: typedef int EnumName; enum : EnumName {A, B, C} And your global scope is happily polluted!
Not any more. typedef will be removed from D2. -Lars
Dec 09 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
Lars T. Kyllingstad wrote:
 Jeremie Pelletier wrote:
 hehe45 wrote:
 In c++ it is valid syntax to have trailing commas at the and of enum 
 definitions:
 enum {a, b, c, };
 This would be a useful addition to D too.

 The enum class syntax from c++0x should also adopted by D, this would 
 allow named enums which are not automatically encased by a namespace:

 enum EnumName {A, B, C};       ---> A, B and C are global constants
 enum class EnumName {A, B, C};---> A, B and C are in the EnumName 
 namespace
I don't think that's necessary, you could simply do: typedef int EnumName; enum : EnumName {A, B, C} And your global scope is happily polluted!
Not any more. typedef will be removed from D2. -Lars
Ah hmm, I missed that thread. That was about the biggest use I had for typedefs, but this case can use aliases just as well.
Dec 09 2009