Defining constant values in struct
jklp via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 16 14:38:21 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.
>
> 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.
More information about the Digitalmars-d-learn
mailing list