Easy way to implement interface properties?

Adam D. Ruppe destructionator at gmail.com
Tue Dec 31 17:55:17 PST 2013


On Wednesday, 1 January 2014 at 01:33:04 UTC, Frustrated wrote:
> But your template mixin is still duplicating generic code that 
> should be easily handled automatically. (Generic properties are 
> just wrappers around private fields that always have the same 
> code (just return or set the field))

Oh yeah, that can be done too.

Here's an example:
http://arsdnet.net/dcode/autoimpl.d

The mixin template is implemented by a helper function, which 
loops over the interface methods and builds a code string for it. 
It doesn't handle complex cases, like a setter without a getter, 
but it is a start.

The pragma(msg) in there shows you the generated code when it 
compiles, which can help debugging or just show you what's going 
on, of course you can remove that when you're happy with it.


More information about the Digitalmars-d-learn mailing list