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.stoudenm...@gmail.com
   emi...@pitp.ca
   http://itensor.org/miles/
  • enums Russel Winder via Digitalmars-d-learn
    • Re: enums bearophile via Digitalmars-d-learn
      • Re: enums Philippe Sigaud via Digitalmars-d-learn
        • Re: enums H. S. Teoh via Digitalmars-d-learn
          • Re: enums Philippe Sigaud via Digitalmars-d-learn
    • Re: enums monarch_dodra via Digitalmars-d-learn
      • Re: enums Andrej Mitrovic via Digitalmars-d-learn
        • Re: enums Miles Stoudenmire via Digitalmars-d-learn
        • Re: enums bearophile via Digitalmars-d-learn
          • Re: enums Paul D Anderson via Digitalmars-d-learn
            • Re: en... bearophile via Digitalmars-d-learn
            • Re: en... Timon Gehr via Digitalmars-d-learn
            • Re: en... monarch_dodra via Digitalmars-d-learn
              • Re... Chris Nicholson-Sauls via Digitalmars-d-learn
                • ... bearophile via Digitalmars-d-learn
                • ... Chris Nicholson-Sauls via Digitalmars-d-learn
          • Re: enums Philippe Sigaud via Digitalmars-d-learn
    • Re: enums Ali Çehreli via Digitalmars-d-learn

Reply via email to