Getters/setters generator
jmh530 via Digitalmars-d-announce
digitalmars-d-announce at puremagic.com
Tue Jun 13 12:31:28 PDT 2017
On Sunday, 11 December 2016 at 02:17:18 UTC, Mike Parker wrote:
>
> What are properties if not "getters" and "setters"? From the
> original post: "It would generate 2 methods "num": one to set
> num_ and one to get its value."
>
> Two methods named "num". No "get" or "set" in sight.
Sorry to bump, but it also isn't hard to use mixins to write more
generic get/set.
import std.stdio : writeln;
struct Foo
{
private int a;
private int b;
void set(string variable)(int x) @property
{
mixin(variable ~ " = x;");
}
int get(string variable)() @property
{
return mixin(variable);
}
}
void main()
{
Foo foo;
foo.set!("a")(1);
foo.set!("b")(2);
writeln(foo.get!("a"));
writeln(foo.get!("b"));
}
More information about the Digitalmars-d-announce
mailing list