May 30, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | Some explanation how the seemingly different concepts "enumerated value" and "manifest constant" are actually related: Let's start with the "classical" enums as they're known from C/C++. They are used to create lists of symbolic names: enum Color { YELLOW, PINK, BLUE }; Internally, each of its members has an integer value by which it is represented. In the above example, YELLOW is 0, PINK is 1 and BLUE is 2. They are also implicitly convertible to their values. Whenever YELLOW, PINK or BLUE is used in the program, the compiler will treat it as if it were a literal 0, 1 or 2 (mostly; in some cases it is still possible to know that you're dealing with YELLOW instead of 0 by using introspection). Normally, the compiler will assign values to the members starting from 0, but it's also possible to specify them explicitly: enum SIPrefixes { KILO = 1_000, MEGA = 1_000_000 }; An extension (and generalization) of this concept is to allow base types other than integers: enum WeekDays : string { MON = "Monday", TUE = "Tuesday" }; They, too, are implicitly convertible to their base types. And analogously to the Color enum, in this case the members are treated as if they were the string literals "Monday", or "Tuesday". From here, it's only a short distance to manifest constants. We can get there by making the braces optional when there is only one member and the enum type has no name, and allowing the base type to be automatically deduced: enum ManifestArrayLiteral = [1, 2, 3]; (I believe, internally DMD does treat the two concepts differently, but IMO this is a nice way to understanding how they work.) |
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra Attachments:
| This has been asked so many times, is this info not on the website? We should have an article on the site explaining this in depth. OT: Sorry for top-quoting and over-quoting. On Friday, May 30, 2014, monarch_dodra via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > On Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder via Digitalmars-d-learn wrote: >> >> I think I have no idea what D enums are about. >> >> Bearophile's example of some code in an email on another thread uses: >> >> enum double p0 = 0.0045; >> >> Now I would have written: >> >> immutable double p0 = 0.0045; >> >> or at the very worst: >> >> const double p0 = 0.0045; >> >> For me, enum means create an enumerated type. Thus "enum double" to define a single value is just a contradiction. >> >> Enlightenment required… > > The keyword "enum" stems from the enum hack in C++, where you use: enum {foo = 100}; //Or similar > > As a way to declare a manifest constant known at compile time. > > D simply "hijacked" the "enum" keyword to mean "manifest constant that is known at compile time". > > Compared to an immutable instance: > * The immutable instance creates an actual reference-able object in your binary. The enum will not exist outside of the compilation (think of it as a higher order macro) > * immutable represents a value, which *may* be initialized at runtime. In any case, more often than not (I have observed), the compiler will refuse to use the immutable's value as compile-time known, and it won't be useable as a template parameter, or static if constraint. > |
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Attachments:
| In contrast to those two examples where immutable can be used at compile time, what are some other cases where it is necessary to use enum instead of immutable? On 31 May 2014 09:33, Andrej Mitrovic via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > This has been asked so many times, is this info not on the website? We should have an article on the site explaining this in depth. OT: Sorry for top-quoting and over-quoting. > > > On Friday, May 30, 2014, monarch_dodra via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > > On Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder via > Digitalmars-d-learn wrote: > >> > >> I think I have no idea what D enums are about. > >> > >> Bearophile's example of some code in an email on another thread uses: > >> > >> enum double p0 = 0.0045; > >> > >> Now I would have written: > >> > >> immutable double p0 = 0.0045; > >> > >> or at the very worst: > >> > >> const double p0 = 0.0045; > >> > >> For me, enum means create an enumerated type. Thus "enum double" to define a single value is just a contradiction. > >> > >> Enlightenment required… > > > > The keyword "enum" stems from the enum hack in C++, where you use: enum {foo = 100}; //Or similar > > > > As a way to declare a manifest constant known at compile time. > > > > D simply "hijacked" the "enum" keyword to mean "manifest constant that > is known at compile time". > > > > Compared to an immutable instance: > > * The immutable instance creates an actual reference-able object in your > binary. The enum will not exist outside of the compilation (think of it as > a higher order macro) > > * immutable represents a value, which *may* be initialized at runtime. > In any case, more often than not (I have observed), the compiler will refuse to use the immutable's value as compile-time known, and it won't be useable as a template parameter, or static if constraint. > > > -- -=Miles Stoudenmire=- miles.stoudenmire@gmail.com emiles@pitp.ca http://itensor.org/miles/ |
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Miles Stoudenmire | Miles Stoudenmire:
> In contrast to those two examples where immutable can be used at compile
> time, what are some other cases where it is necessary to use enum instead of immutable?
By default use enum if you define a compile-time-known value, unless it's composed data like an array, etc.
Bye,
bearophile
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote:
> Miles Stoudenmire:
>
>> In contrast to those two examples where immutable can be used at compile
>> time, what are some other cases where it is necessary to use enum instead of immutable?
>
> By default use enum if you define a compile-time-known value, unless it's composed data like an array, etc.
>
> Bye,
> bearophile
'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but Walter -- at DConf2014 Walter said (again) that using "enum" was okay because people get used to it!
The only reason given is that re-using a keyword is supposed to be easier than introducing a new one. That is manifestly false. ;)
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul D Anderson | Paul D Anderson:
> 'enum' as a manifest constant keyword has been an unpopular decision from its introduction.
I agree, I too asked for a better name.
Bye,
bearophile
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul D Anderson | On 05/31/2014 11:21 PM, Paul D Anderson wrote:
> On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote:
>> Miles Stoudenmire:
>>
>>> In contrast to those two examples where immutable can be used at compile
>>> time, what are some other cases where it is necessary to use enum
>>> instead of immutable?
>>
>> By default use enum if you define a compile-time-known value, unless
>> it's composed data like an array, etc.
>>
>> Bye,
>> bearophile
>
> 'enum' as a manifest constant keyword has been an unpopular decision
> from its introduction. "Everybody" agrees that it should be changed.
> Everybody but Walter -- at DConf2014 Walter said (again) that using
> "enum" was okay because people get used to it!
>
> The only reason given is that re-using a keyword is supposed to be
> easier than introducing a new one. That is manifestly false. ;)
'const' is just as badly named, but for some reason this fact doesn't get nearly as much attention. IMO 'enum' should be 'const' and 'const' should be 'readonly'.
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul D Anderson | On Saturday, 31 May 2014 at 21:21:59 UTC, Paul D Anderson wrote:
> 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but Walter
I find enum makes sense.
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Saturday, 31 May 2014 at 22:13:35 UTC, monarch_dodra wrote:
> On Saturday, 31 May 2014 at 21:21:59 UTC, Paul D Anderson wrote:
>> 'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but Walter
>
> I find enum makes sense.
Good... I was starting to fear I was the only one.
|
May 31, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | Chris Nicholson-Sauls:
> Good... I was starting to fear I was the only one.
In general you can't fix the names in a language because you always find someone that likes the ones present :) I think "enum" is a bad name for the purpose of defining manifest constants, but I don't think this will change.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation