How add class or struct member after construction?

Jacob Carlborg doob at me.com
Sun Nov 8 16:10:03 UTC 2020


On 2020-11-05 23:48, Marcone wrote:
> How add class or struct member after construction? Is it possible in D? 
> How?

It depends on what needs you have. You can declare a free function that 
takes the class/struct as the first parameter and call it like a method [1]:

class Foo
{
     int a;
}

void printA(Foo foo)
{
     writeln(foo.a);
}

foo.printA();
printA(foo);

The two above lines are exactly the same.

[1] https://dlang.org/spec/function.html#pseudo-member

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list