Annotation programming in my design code ..

Eugene Wissner via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 6 09:48:23 PST 2017


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 at 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.


More information about the Digitalmars-d mailing list