On Tuesday, 13 June 2017 at 19:31:28 UTC, jmh530 wrote:
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"));
}

I suppose the errors will be more cryptic, since you don't check if the string referers to an existing member.

You provide only get/set that return by value. So you may need to generate getters/setters for const values, for ref values, for const ref values... And it would result in much more code for every object.

Reply via email to