Polymorphic recursive class
Jack Applegame via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jul 13 08:46:41 PDT 2015
On Monday, 13 July 2015 at 13:51:51 UTC, Dmitry Olshansky wrote:
> You can do it no problem, it just requires to manually
> implement boxing and make `right` a template that casts to the
> right type. The reason is that D doesn't provide an extra
> abstraction layer that is "auto-magically" there in typical FP
> language.
>
> Not tested but this should work:
>
> class Nested(T){
> T left;
> void* right_; // or use opaque type if allergic to void*
> @property auto right()(){ // empty template to prevent
> recursion
> return cast(Nested!(T[])right_;
> }
> @property auto right(U)(Nested!U type){ // empty template to
> prevent recursion
> right_ = cast(void*)right_;
> }
>
> // and lastly the constructor should be templated
> this(U)(T l, Nested!(U) r) {
> left = l;
> right = r;
> }
> }
Wow! Template properties is nice idea. Thanks.
More information about the Digitalmars-d
mailing list