Easy way to implement interface properties?

Frustrated c1514843 at drdrb.com
Wed Jan 1 09:08:41 PST 2014


On Wednesday, 1 January 2014 at 14:30:46 UTC, Gary Willoughby 
wrote:
> On Wednesday, 1 January 2014 at 12:09:40 UTC, Jacob Carlborg 
> wrote:
>> On 2014-01-01 01:52, Frustrated wrote:
>>> Is there an easy way to implement properties of an interface 
>>> within a
>>> class instead of having to duplicate almost the exact same 
>>> code with
>>> generic properties?
>>>
>>> interface A
>>> {
>>>    @property int data();
>>>    @property int data(int value);
>>>
>>> }
>>>
>>> class B : A
>>> {
>>>  @property int data() { return m_data; } // read property
>>>  @property int data(int value) { return m_data = value; } // 
>>> write
>>> property
>>> }
>>
>> You can't use an abstract class?
>
> Yes this is the ideal time to use an abstract base class and is 
> what i would do.
>
> The same thing *could* be achieved using mixin templates, with 
> or without scopes but could lead to unmaintainable code. See 
> the examples on mixin templates here: 
> http://nomad.so/2013/07/templates-in-d-explained/


With abstract classes I would still have to implement generic 
code and it would not be much different than using a standard 
class(The inheritance is only about 2 levels deep but many 
interfaces).

I don't see how the mixin method would be unmaintainable since it 
simply implements what hasn't been implemented(it could lead to 
bugs if one forgets to implement stuff but that's easy to 
check(functions could throw exceptions in "retail" build)).

Basically Adam's approach is what I was looking for as I see it 
as the best way unless you have any specific reasons why it 
doesn't work well. (of course the interface implementer isn't 
robust but I'm working on that)



More information about the Digitalmars-d-learn mailing list