enum - auto

Simen Kjærås simen.kjaras at gmail.com
Mon Nov 14 02:33:26 PST 2011


On Mon, 14 Nov 2011 11:14:02 +0100, so <so at so.so> wrote:

> Auto and immutable.
> Can we please put an end to this issue?
> I remember a few discussions, never a complete answer, but many  
> contradicting answers.
>
> 1. Is enum (its current usage) still a temporary hack to a technical  
> limitation on immutable?

Yes and no. Enum was chosen due to limitations in the linker, which made it
impossible to weed out unused constants. The linker is free to remove  
unused
constants, but DMD's linker will not do so. Perhaps in the future, it will.


> 2. Can or Will immutable replace enum? (Not that i want such a change)

Enum is now an established part of the language, and even if such were  
possible,
it is not going to happen.


> 3. Isn't enum a higher order immutable, which is guarantied to be a  
> compile time constant?

One could think of it as such, yes. That hardly gives much advantage over
immutable, though. The main feature of enum is that it takes up no space  
in the
result binary.


> Another issue, auto.
> I was thinking that it works like C++0x auto.
> Now i realize they are not that alike. Can anyone show me the D  
> equivalent of the code below?
>
> struct test {
>      int a;
> };
>
> void main() {
>      test tmp;
>      auto& a = tmp.a;
> }

That would be a reference to an int, right? No such thing in D, except for
function parameters. What you would do instead is get a pointer:

struct test {
     int a;
}

void main() {
     test tmp;
     auto a = &tmp.a;
}


More information about the Digitalmars-d mailing list