Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 16, 2015 Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
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. |
June 16, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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.
|
June 16, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to jklp | 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.
|
June 16, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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. |
June 17, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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
}
|
June 17, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | 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.
|
June 17, 2015 Re: Defining constant values in struct | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker Attachments: | 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=14459 |
Copyright © 1999-2021 by the D Language Foundation