Get symbols (and/or UDAs) of subclass from superclass
James Blachly
james.blachly at gmail.com
Mon Mar 16 15:55:22 UTC 2020
On 3/15/20 5:28 PM, Simen Kjærås wrote:
...
> Since toStringImpl will always call toStringBase, this could perhaps
> better be modeled with a template mixin:
>
> mixin template DerivedToString() {
> override string toStringImpl() {
> return this.toStringBase();
> }
> }
>
> class Derived2 : Base {
> mixin DerivedToString!();
> }
>
> This way, you can have all the logic of toString in the base class, and
> the only thing a subclass will have to include is one line for the
> mixin. In addition, since toStringImpl is abstract, the implementer of a
> subclass will get a compile-time error if he or she forgets to do either
> the mixin or override toStringImpl themselves.
>
> --
> Simen
Thanks to you and Basile for responses and distinct solutions to
consider. I am working on the mixin approach now, but with a brief
mixinline in each derived subclass, I do not even need to call a
superclass method -- just write the display code once with UDA
filtering. I think this is the most sensible approach, until something
else unexpected comes up :)
More information about the Digitalmars-d-learn
mailing list