www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - real simple manifest constant question probably regret asking...

reply WhatMeForget <kheaser gmail.com> writes:
One of my D books says: "an enum declared without any braces is 
called a manifest constant." The example shows,

enum string author = "Mike Parker";

Is this equivalent to
const string author = "Mike Parker";
or
immutable string author = "Mike Parker";

I guess what I'm asking is does enum give you some advantages 
over say non-enum constants?

Thanks.
Mar 15
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 16/03/2017 4:27 PM, WhatMeForget wrote:
 One of my D books says: "an enum declared without any braces is called a
 manifest constant." The example shows,

 enum string author = "Mike Parker";

 Is this equivalent to
 const string author = "Mike Parker";
 or
 immutable string author = "Mike Parker";

 I guess what I'm asking is does enum give you some advantages over say
 non-enum constants?

 Thanks.
No. An enum is an enum, that variation of the syntax just has a special name that's all. Keep in mind the typed aspect doesn't have an affect other than forcing the value to it. enum Foo : string { Author = "Mike Parker" }
Mar 15
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
WhatMeForget wrote:

 One of my D books says: "an enum declared without any braces is called a 
 manifest constant." The example shows,

 enum string author = "Mike Parker";

 Is this equivalent to
 const string author = "Mike Parker";
 or
 immutable string author = "Mike Parker";

 I guess what I'm asking is does enum give you some advantages over say 
 non-enum constants?

 Thanks.
"enum constants" are so-called "inline constants". i'll try to explain it. imagine that you have a constant: int[2] carr = [ 42, 69 ]; this array will be placed in read-only data segment, and each time you refer to it, the same array will be used. i.e. assert(carr.ptr is carr.ptr); will pass. now let's try enum int[2] carr = [ 42, 69 ]; this time, assert(carr.ptr is carr.ptr); will fail. why? 'cause when you are using "enum constant", it will be created in-place. i.e. in the second case you'll get *two* arrays with identical content. wether you want that effect or not is completely up to you. usually, it is better to declare numeric constants as enums (so they won't take any storage at all), and array constants as `immutable`s. as for strings, it depends of your system, compiler and linker. usually, linkers "deduplicating" strings (i.e. merging identical strings into one), but if your string is heavily used, it may still be better to declare it as non-enum.
Mar 15
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/15/2017 08:27 PM, WhatMeForget wrote:
 One of my D books says: "an enum declared without any braces is called a
 manifest constant." The example shows,

 enum string author = "Mike Parker";

 Is this equivalent to
 const string author = "Mike Parker";
 or
 immutable string author = "Mike Parker";

 I guess what I'm asking is does enum give you some advantages over say
 non-enum constants?
The guideline should be, "prefer enum except when the type is an array except if it's a string." :) You can think of enum as a text replacement similar to C macros. Whenever you see 'author' in code, it would be as if it was replaced with its value: writeln(author); writeln("Mike Parker"); // Same as above The limitation is that just like you cannot take the address of e.g. 42, you can't take the address of an enum: // All lines below are compilation errors: "not an lvalue" writeln(&42); writeln(&"Mike Parker"); writeln(&author); const static and immutable have the advantage of being initialized at runtime. This one reads the name from a file: import std.stdio; immutable string author; string makeAuthor() { import std.file; auto content = read("author_name_file"); return cast(string)content; } shared static this() { author = makeAuthor(); } void main() { writeln(author); } Ali
Mar 15