digitalmars.D.learn - Defining constant values in struct
- tcak (22/22) Jun 16 2015 As far as I known, when I define a string with enum and it is
- jklp (12/36) Jun 16 2015 Do i miss a detail in your requirement ?
- tcak (3/16) Jun 16 2015 Hmm, I never defined it as `static const` before. Tried with
- anonymous (18/36) Jun 16 2015 I'm not sure how true that is. For example, this prints the same
- Alex Parrill (11/16) Jun 16 2015 String literals are merged by the compiler, much like in C and
- Mike Parker (5/9) Jun 16 2015 The string is not repeated again and again in the *executable*. The
- ketmar (3/13) Jun 17 2015 but only to some extent[1].
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
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
On Tuesday, 16 June 2015 at 21:38:22 UTC, jklp wrote:On Tuesday, 16 June 2015 at 21:17:37 UTC, tcak wrote:Hmm, I never defined it as `static const` before. Tried with `static shared` only. Yes, it works like this.[...]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
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
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
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
On Wed, 17 Jun 2015 09:49:56 +0900, Mike Parker wrote:On 6/17/2015 6:17 AM, tcak wrote:but only to some extent[1]. [1] https://issues.dlang.org/show_bug.cgi?id=3D14459=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 17 2015









"tcak" <tcak gmail.com> 