digitalmars.D.learn - enums
- Russel Winder via Digitalmars-d-learn (16/16) May 30 2014 I think I have no idea what D enums are about.
- monarch_dodra (16/28) May 30 2014 The keyword "enum" stems from the enum hack in C++, where you use:
- Andrej Mitrovic via Digitalmars-d-learn (12/39) May 31 2014 This has been asked so many times, is this info not on the website? We
- Miles Stoudenmire via Digitalmars-d-learn (14/58) May 31 2014 In contrast to those two examples where immutable can be used at compile
- bearophile (5/9) May 31 2014 By default use enum if you define a compile-time-known value,
- Paul D Anderson (7/16) May 31 2014 'enum' as a manifest constant keyword has been an unpopular
- bearophile (4/6) May 31 2014 I agree, I too asked for a better name.
- Timon Gehr (4/22) May 31 2014 'const' is just as badly named, but for some reason this fact doesn't
- monarch_dodra (2/5) May 31 2014 I find enum makes sense.
- Chris Nicholson-Sauls (2/7) May 31 2014 Good... I was starting to fear I was the only one.
- bearophile (7/8) May 31 2014 In general you can't fix the names in a language because you
- Chris Nicholson-Sauls (9/17) Jun 01 2014 In a perfect world, sure, we'd have a different name for it; I'm
- Philippe Sigaud via Digitalmars-d-learn (6/10) Jun 01 2014 Now that we have immutable's that can be used inside templates, why
- bearophile (7/10) May 30 2014 In D enum can be used to define manifest constants. This means
- Philippe Sigaud via Digitalmars-d-learn (9/13) May 30 2014 Because they do not exist as 'variables' or symbol in the generated
- H. S. Teoh via Digitalmars-d-learn (9/16) May 30 2014 One has to admit, though, that the choice of 'enum' as keyword is a bit
- Philippe Sigaud via Digitalmars-d-learn (3/3) May 30 2014 A good use of 'static immutable', sadly not voted into the language.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/8) May 30 2014 As others have already said, p0 is a manifest constant. Interestingly,
- Philippe Sigaud via Digitalmars-d-learn (6/13) May 30 2014 I guess that, in an ideal world, immutable variables could be use at
- Steven Schveighoffer (7/25) May 30 2014 , =
- Philippe Sigaud via Digitalmars-d-learn (5/7) May 30 2014 Oh wow.
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (30/30) May 30 2014 Some explanation how the seemingly different concepts "enumerated
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… -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
May 30 2014
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 30 2014
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 viaDigitalmars-d-learn wrote:known at compile time".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 =3D 0.0045; Now I would have written: immutable double p0 =3D 0.0045; or at the very worst: const double p0 =3D 0.0045; For me, enum means create an enumerated type. Thus "enum double" to define a single value is just a contradiction. Enlightenment required=E2=80=A6The keyword "enum" stems from the enum hack in C++, where you use: enum {foo =3D 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 isCompared to an immutable instance: * The immutable instance creates an actual reference-able object in yourbinary. 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. Inany 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
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 fo=rtop-quoting and over-quoting. On Friday, May 30, 2014, monarch_dodra via Digitalmars-d-learn < digitalmars-d-learn puremagic.com> wrote:rOn Friday, 30 May 2014 at 15:30:15 UTC, Russel Winder viaDigitalmars-d-learn wrote:is known at compile time".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 =3D 0.0045; Now I would have written: immutable double p0 =3D 0.0045; or at the very worst: const double p0 =3D 0.0045; For me, enum means create an enumerated type. Thus "enum double" to define a single value is just a contradiction. Enlightenment required=E2=80=A6The keyword "enum" stems from the enum hack in C++, where you use: enum {foo =3D 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 thatCompared to an immutable instance: * The immutable instance creates an actual reference-able object in you=binary. The enum will not exist outside of the compilation (think of it a=sa higher order macro)e* 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 b=useable as a template parameter, or static if constraint.--=20 -=3DMiles Stoudenmire=3D- miles.stoudenmire gmail.com emiles pitp.ca http://itensor.org/miles/
May 31 2014
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
On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote:Miles Stoudenmire:'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. ;)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
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
On 05/31/2014 11:21 PM, Paul D Anderson wrote:On Saturday, 31 May 2014 at 20:14:59 UTC, bearophile wrote:'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'.Miles Stoudenmire:'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. ;)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
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 WalterI find enum makes sense.
May 31 2014
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:Good... I was starting to fear I was the only one.'enum' as a manifest constant keyword has been an unpopular decision from its introduction. "Everybody" agrees that it should be changed. Everybody but WalterI find enum makes sense.
May 31 2014
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
May 31 2014
On Saturday, 31 May 2014 at 22:45:32 UTC, bearophile wrote:Chris Nicholson-Sauls:In a perfect world, sure, we'd have a different name for it; I'm not saying I love it, just that it "makes sense" in my head. I've sometimes thought 'alias' could have worked as well, especially now with the a=b syntax. alias DEFAULT_PORT = 11020; But, then we exchange one set of brow raisers for another set. In the absence of #define, there probably is no achievable ideal. :)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
Jun 01 2014
On Sat, May 31, 2014 at 10:14 PM, bearophile via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Now that we have immutable's that can be used inside templates, why use an enum? Because it's 'inlined' in the code? (I know the reason not to use it for an array or associative array, I experimented that fully in my own code. Ouch!)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 (...)
Jun 01 2014
Russel Winder:For me, enum means create an enumerated type. Thus "enum double" to define a single value is just a contradiction. Enlightenment required…In D enum can be used to define manifest constants. This means constants known at compile time. In practice for a double there isn't a lot of difference. In general you can't take the address of a manifest constant, unlike immutables. Bye, bearophile
May 30 2014
In D enum can be used to define manifest constants. This means constants known at compile time. In practice for a double there isn't a lot of difference. In general you can't take the address of a manifest constant, unlike immutables.Because they do not exist as 'variables' or symbol in the generated code. They are directly replaced by their computed value. so : - enum : manifest constant, can be used as a template argument, as a static if operand or generally as a compile-time value. Indeed, a way to force compile-time function evaluation is to ask for an enum: enum result = foo(...); // foo(...) will be evaluated during compilation. - immutable : it's a real variable: you can initialize it (once) at runtime, and take its address.
May 30 2014
On Fri, May 30, 2014 at 07:17:15PM +0200, Philippe Sigaud via Digitalmars-d-learn wrote:One has to admit, though, that the choice of 'enum' as keyword is a bit unfortunate, since most newbies (and some not-so-newbies) understand it as "enumeration" rather than "manifest constant". It makes sense in retrospect, but is quite counterintuitive for first-timers. T -- I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike DresserIn D enum can be used to define manifest constants. This means constants known at compile time. In practice for a double there isn't a lot of difference. In general you can't take the address of a manifest constant, unlike immutables.Because they do not exist as 'variables' or symbol in the generated code. They are directly replaced by their computed value.
May 30 2014
A good use of 'static immutable', sadly not voted into the language. :-) But you're right, and I remember being tripped by this.
May 30 2014
On 05/30/2014 08:30 AM, Russel Winder via Digitalmars-d-learn wrote:enum double p0 = 0.0045;As others have already said, p0 is a manifest constant. Interestingly, it can be thought of like a C macro, being pasted inside source code. Avoid enums for arrays and associative arrays as they are hidden performance sinks. The constant gets regenerated at runtime every time it is used in an expression. Ali
May 30 2014
On Fri, May 30, 2014 at 7:28 PM, Ali Çehreli <digitalmars-d-learn puremagic.com> wrote:On 05/30/2014 08:30 AM, Russel Winder via Digitalmars-d-learn wrote:I guess that, in an ideal world, immutable variables could be use at compile-time (template arguments, etc). We could then ditch 'enums-as-manifest-constants'.The best of both worlds. But what we have here is already quite good!enum double p0 = 0.0045;As others have already said, p0 is a manifest constant. Interestingly, it can be thought of like a C macro, being pasted inside source code. Avoid enums for arrays and associative arrays as they are hidden performance sinks. The constant gets regenerated at runtime every time it is used in an expression.
May 30 2014
On Fri, 30 May 2014 13:34:38 -0400, Philippe Sigaud via = Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Fri, May 30, 2014 at 7:28 PM, Ali =C3=87ehreli <digitalmars-d-learn puremagic.com> wrote:, =On 05/30/2014 08:30 AM, Russel Winder via Digitalmars-d-learn wrote:enum double p0 =3D 0.0045;As others have already said, p0 is a manifest constant. Interestingly=it can be thought of like a C macro, being pasted inside source code. Avoid enums for arrays and associative arrays as they are hidden ==performance sinks. The constant gets regenerated at runtime every time it is used=You can as long as the value is known at compile time: http://dpaste.dzfl.pl/5a710bd80ab0 -Stevein an expression.I guess that, in an ideal world, immutable variables could be use at compile-time (template arguments, etc). We could then ditch 'enums-as-manifest-constants'.The best of both worlds.
May 30 2014
On Fri, May 30, 2014 at 7:56 PM, Steven Schveighoffer wrote:You can as long as the value is known at compile time: http://dpaste.dzfl.pl/5a710bd80ab0Oh wow. And that works for static if also: http://dpaste.dzfl.pl/f87321a47834 Man. That opens some new possibilities.
May 30 2014
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 30 2014