www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - enums

reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
next sibling parent Andrej Mitrovic via Digitalmars-d-learn writes:
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 =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=A6
The 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 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
prev sibling parent reply Miles Stoudenmire via Digitalmars-d-learn writes:
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=
r
 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 =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=A6
The 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
is known at compile time".
 Compared to an immutable instance:
 * The immutable instance creates an actual reference-able object in you=
r
 binary. The enum will not exist outside of the compilation (think of it a=
s
 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 b=
e
 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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply "Paul D Anderson" <claude.rains msn.com> writes:
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
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
parent reply "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
On Saturday, 31 May 2014 at 22:45:32 UTC, bearophile wrote:
 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
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. :)
Jun 01 2014
prev sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
On Sat, May 31, 2014 at 10:14 PM, bearophile via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 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 (...)
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!)
Jun 01 2014
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
 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
prev sibling next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, May 30, 2014 at 07:17:15PM +0200, Philippe Sigaud via
Digitalmars-d-learn wrote:
 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.
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 Dresser
May 30 2014
prev sibling parent Philippe Sigaud via Digitalmars-d-learn writes:
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
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply Philippe Sigaud via Digitalmars-d-learn writes:
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:

          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.
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!
May 30 2014
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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=
=
 in 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.
You can as long as the value is known at compile time: http://dpaste.dzfl.pl/5a710bd80ab0 -Steve
May 30 2014
parent Philippe Sigaud via Digitalmars-d-learn writes:
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/5a710bd80ab0
Oh wow. And that works for static if also: http://dpaste.dzfl.pl/f87321a47834 Man. That opens some new possibilities.
May 30 2014
prev sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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