Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 30, 2014 enums | ||||
---|---|---|---|---|
| ||||
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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > 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 Re: enums | ||||
---|---|---|---|---|
| ||||
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 Re: enums | ||||
---|---|---|---|---|
| ||||
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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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 Ç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. You can as long as the value is known at compile time: http://dpaste.dzfl.pl/5a710bd80ab0 -Steve |
May 30, 2014 Re: enums | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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. |
Copyright © 1999-2021 by the D Language Foundation