On Friday, February 09, 2018 15:48:42 Andrea Fontana via Digitalmars-d 
wrote:
> On Friday, 9 February 2018 at 15:35:38 UTC, Mike Parker wrote:
> > On Friday, 9 February 2018 at 15:27:18 UTC, Andrea Fontana
> >
> > wrote:
> >>> If you need to take the address of a constant, use immutable
> >>> or const (doesn't really matter, but I prefer immutable). If
> >>> you don't need the address, use enum.
> >>
> >> Why not static immutable?
> >
> > For global scope? static has no effect there as far as I know.
> > Looking at the ASM output on run.dlang.io, I see no difference
> > between the two:
> >
> > static immutable foo = 10;
> > immutable bar = 20;
> >
> > LAT group
> > ;File = onlineapp.d
> >
> >     public  immutable(int) onlineapp.foo
> >     public  immutable(int) onlineapp.bar
> >
> > ...
> >
> >                 mov EDI,0Ah
> >
> >         call      @safe void
> >
> > std.stdio.writeln!(immutable(int)).writeln(immutable(int))@PLT32
> >
> >         mov EDI,014h
> >         call      @safe void
> >
> > std.stdio.writeln!(immutable(int)).writeln(immutable(int))@PLT32
>
> If I'm right on classes/structs static immutable != immutable. So
> if you need to replace enum in those cases too, static immutable
> works fine everywhere. Right?

static immutable works fine everywhere, but it's pointless to have the
static at the module level. It's a no-op there. But for most types, enum is
better, because it doesn't result in there being an object in the binary,
whereas static immutable would. The cases where you'd want to avoid enums
are when you really want there to be an address (e.g. for arrays other than
strings, you don't want to use enums, because for them, a new dynamic array
gets alloced every time you use the enum), but something like int, an enum
is definitely better.

- Jonathan M Davis

Reply via email to