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.

Reply via email to