www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Defining constant values in struct

reply "tcak" <tcak gmail.com> writes:
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.

Hence, I defined string as const to make it belong to struct 
itself instead of instances, but it comes with 'need `this` for 
...' error. This indicates that the string doesn't belong to 
struct itself actually.

Because there is nothing like namespace in D, I used a sub-struct.

[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.
Jun 16 2015
next sibling parent reply "jklp" <jklp nowhere.fr> writes:
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.

 Hence, I defined string as const to make it belong to struct 
 itself instead of instances, but it comes with 'need `this` for 
 ...' error. This indicates that the string doesn't belong to 
 struct itself actually.

 Because there is nothing like namespace in D, I used a 
 sub-struct.

 [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.
Do i miss a detail in your requirement ? --- struct TableSchema{ const string TABLE = "users"; struct FieldTypes{ static const string ID = "BIGINT"; } const string CREATESQL = "... id " ~ FieldTypes.ID ~ "..."; } --- because this works.
Jun 16 2015
parent "tcak" <tcak gmail.com> writes:
On Tuesday, 16 June 2015 at 21:38:22 UTC, jklp wrote:
 On Tuesday, 16 June 2015 at 21:17:37 UTC, tcak wrote:
 [...]
Do i miss a detail in your requirement ? --- struct TableSchema{ const string TABLE = "users"; struct FieldTypes{ static const string ID = "BIGINT"; } const string CREATESQL = "... id " ~ FieldTypes.ID ~ "..."; } --- because this works.
Hmm, I never defined it as `static const` before. Tried with `static shared` only. Yes, it works like this.
Jun 16 2015
prev sibling next sibling parent "anonymous" <anonymous example.com> writes:
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.
Jun 16 2015
prev sibling next sibling parent "Alex Parrill" <initrd.gz gmail.com> writes:
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.
String literals are merged by the compiler, much like in C and Java. import std.stdio; enum mystring = "hello world!"; void main() { string str1 = mystring; string str2 = mystring; writeln(str1.ptr == str2.ptr); // prints true }
Jun 16 2015
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 6/17/2015 6:17 AM, 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.
The string is not repeated again and again in the *executable*. The compiler is smart enough to recognize the same string is used in multiple places. It will be placed in the data segment only once. It's no different than using a string literal.
Jun 16 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 17 Jun 2015 09:49:56 +0900, Mike Parker wrote:

 On 6/17/2015 6:17 AM, 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.
The string is not repeated again and again in the *executable*. The compiler is smart enough to recognize the same string is used in multiple places. It will be placed in the data segment only once. It's no different than using a string literal.
but only to some extent[1]. [1] https://issues.dlang.org/show_bug.cgi?id=3D14459=
Jun 17 2015