Defining constant values in struct

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 16 14:46:33 PDT 2015


On Tuesday, 16 June 2015 at 21:17:37 UTC, tcak wrote:
> As far as I known, when I define a string with enum and it is 
> used at different parts of code, that string is repeated again 
> and again in executable file instead of passing a pointer to 
> string. So, using enum with string doesn't seem like a good 
> idea.
>

I'm not sure how true that is. For example, this prints the same 
address twice:
----
import std.stdio;
enum e = "foo";
void main()
{
     auto a = e;
     auto b = e;
     writeln(a.ptr);
     writeln(b.ptr);
}
----

In contrast, it prints two different addresses when e is defined 
as `[1, 2, 3]` instead.

[...]
>
> [code]
> struct TableSchema{
> 	const string TABLE = "users";
>
> 	struct FieldTypes{
> 		const string ID = "BIGINT";
> 	}
>
> 	const string CREATESQL = "... id " ~ FieldTypes.ID ~ "...";
> }
> [/code]
>
>
> But compiler doesn't allow me to access FieldTypes.ID. It says 
> that it needs `this`. I tried with `static shared`, used 
> `class` instead of `struct` etc. But couldn't have come up with 
> a nice solution.

`static immutable` is where it's at.


More information about the Digitalmars-d-learn mailing list