www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enum values without initializer

reply bearophile <bearophileHUGS lycos.com> writes:
Do you know why this is an error?

struct Foo {}
enum Foo f;
void main() {}


I think there's no need to consider it an error (and it can't cause runtime
bugs):
http://d.puremagic.com/issues/show_bug.cgi?id=4049

Bye,
bearophile
Apr 03 2010
next sibling parent reply BCS <none anon.com> writes:
Hello bearophile,

 Do you know why this is an error?
 
 struct Foo {}
 enum Foo f;

The only way to make that legal would be to 1) add a special corner case rule or 2) allow "enum type name;" for all types. #1 is a bad idea as are almost all special case rules #2 is just flat wrong 99.9% of the time. -- ... <IXOYE><
Apr 03 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
BCS :
 #1 is a bad idea as are almost all special case rules

Right, I was not looking for a special case.
 #2 is just flat wrong 99.9% of the time.

I see. Then I will think if it's right to remove that "bug" report or not. ----------------
I have to ask. :) What does that code supposed to mean? Is Foo the base type of
the enum, so that we can create enum values for user types as well?<

In D2 Walter has decided to use the keyword "enum" to represent what in D1 is "const". So that's not an enumeration at all, it's just a constant. With other people I think it's a bad choice to use "enum" to say "const", but I think it's one of small warts of D2 that will not change... That code just means that I want to create a compile-time const value initialized at its default (init). But it's not correct current D2 code, you have to write: struct Foo {} enum Foo f = Foo(); void main() {} If you don't want that doplication you can also write: enum auto f = Foo(); Bye, bearophile
Apr 03 2010
parent reply "Nick Sabalausky" <a a.a> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:hp8jra$1ln5$1 digitalmars.com...
 If you don't want that doplication you can also write:
 enum auto f = Foo();

Can't you do: enum f = Foo(); ?
Apr 03 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Nick Sabalausky:

 If you don't want that doplication you can also write:
 enum auto f = Foo();

Can't you do: enum f = Foo(); ?

In my opinion that's a semantic mess, I don't write that. auto is for automatic local type inference and enum is to ask for a compile time constant. Bye, bearophile
Apr 04 2010
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Nick Sabalausky:
 
 If you don't want that doplication you can also write:
 enum auto f = Foo();

enum f = Foo(); ?

In my opinion that's a semantic mess, I don't write that. auto is for automatic local type inference and enum is to ask for a compile time constant.

No it isn't. 'auto' is a storage class, it has NOTHING to do with type inference. Type inference is triggered when the type is omitted from a declaration. It just turns out that in the majority of cases (variables), the easiest way to do this is to use the default storage class which is used if you don't otherwise specify one: auto. This is why 'const blah = 42;' works: const is used as a storage class and the type is omitted.
Apr 06 2010
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
bearophile wrote:
 Do you know why this is an error?
 
 struct Foo {}
 enum Foo f;
 void main() {}
 
 
 I think there's no need to consider it an error (and it can't cause runtime
bugs):
 http://d.puremagic.com/issues/show_bug.cgi?id=4049
 
 Bye,
 bearophile

I have to ask. :) What does that code supposed to mean? Is Foo the base type of the enum, so that we can create enum values for user types as well? Ali
Apr 03 2010