how to achieve C's Token Pasting (##) Operator to generate variable name in D?

mw mingwu at gmail.com
Thu Jun 4 18:09:35 UTC 2020


On Sunday, 31 May 2020 at 06:28:11 UTC, mw wrote:
> This is better, ... but it breaks std.traits:
>
> void main() {
>   auto fields = FieldNameTuple!(Point);
>   writeln(fields);
> }
>
> $ ./b
> varvarvar
>
> And normally, we cannot define 2 fields with different types:
>
> class P {
>   int x;
>   double x;  // b.d(45): Error: variable b.P.x conflicts with 
> variable b.P.x at b.d(44)
> }
>
> With the above template we somehow tricked the compiler to be 
> able to do this?
>
> Is this a loop-hole we should file a bug?


This is related to D's silent crept-in multiple inheritance 
problem, a solution via language improvement is here:

https://forum.dlang.org/post/lsnhqdoyatkzbzqbsrbb@forum.dlang.org


I'd imagine something like this:

----------------------------------------------------------------------
class Person : NameI, AddrI {
   mixin NameT!Person rename equals as name_equals;
   mixin AddrT!Person rename equals as addr_equals;

   bool equals(Person other) {
     return this.name_equals(other) &&
            this.addr_equlas(other);
   }
}
----------------------------------------------------------------------



More information about the Digitalmars-d-learn mailing list