www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I want delete or change class's member name on compile-time

reply Brian <zoujiaqing gmail.com> writes:
Like:

class A
{
     string b;
     string c;
}

compile-time to:

class A
{
     string _b;
     string c;
}

or:

class A
{
     string c;
}
Jun 08 2018
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/8/18 2:13 PM, Brian wrote:
 Like:
 
 class A
 {
      string b;
      string c;
 }
 
 compile-time to:
 
 class A
 {
      string _b;
      string c;
 }
 
 or:
 
 class A
 {
      string c;
 }
 
Not possible in D. What you are looking for is an AST macro, and D does not have those. However, you can potentially create another type that mimics everything that A does except for one thing using compile-time introspection. But you won't be able to do that with the member functions. -Steve
Jun 08 2018
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 06/08/2018 11:13 AM, Brian wrote:
 Like:

 class A
 {
      string b;
      string c;
 }

 compile-time to:

 class A
 {
      string _b;
      string c;
 }

 or:

 class A
 {
      string c;
 }
If these are your classes you can use static if or version: version = Z; // Can be provided as a compiler switch as well class A { version (X) { string b; } else version (Y) { string _b; } string c; } void main () { } Or with static if: class A { static if (some_compile_time_condition) { string _b; } else static if (...) { // ... } else { // ... } } Ali
Jun 08 2018