On Friday, 6 January 2017 at 17:48:23 UTC, Eugene Wissner wrote:
On Friday, 6 January 2017 at 17:44:13 UTC, Brian wrote:
I would like to design features, how should I do?
coding:
class User
{
@GenerateProperty
int id;
@GenerateProperty
string name;
}
struct GenerateProperty
{
this(string propertyName)
{
propertyName = propertyName
}
string propertyName;
string getGenerateCode()
{
return "@property int " ~ propertyName ~ "() { return __" ~
propertyName ~ "; }\n@property int " ~ propertyName ~ "(int
value) { return __" ~ propertyName ~ " = value; }";
}
}
result code:
class User
{
@property int id() { return __id; }
@property int id(int value) { return __id = value; }
@property string name() { return __name; }
@property string name(int value) { return __name = value; }
private
{
int __id;
string __name;
}
}
Look into https://github.com/funkwerk/accessors for the
implementation.
You should have a mixin which iterates through all class
members and generates the properties.
Can write this code to impl it?
import accessors;
import std.stdio;
class Base
{
mixin(GenerateFieldAccessors);
}
class Person : Base
{
@Read @Write
private uint age_;
@ConstRead
private string name_;
this(in string name, in uint age = 0)
{
this.name_ = name;
this.age_ = age;
}
}
void main()
{
auto person = new Person("Saul Kripke");
person.age = 57;
writeln(person.name, ": ", person.age);
}