Is there an easy way to mimic generics with an accept method of a visitor pattern?

vitamin vit at vit.vit
Thu Feb 18 14:51:09 UTC 2021


On Thursday, 18 February 2021 at 14:43:43 UTC, Paul Backus wrote:
> On Thursday, 18 February 2021 at 14:26:37 UTC, vitamin wrote:
>>
>> Or combination of discriminate uninons and classes:
>>
>> /+dub.sdl:
>> dependency "sumtype" version="~>0.10.0"
>> +/
>> import std.stdio;
>>
>> import sumtype;
>>
>> alias Expression = SumType!(
>>     ExprValue,
>>     ExprBinary,
>>     ExprUnary
>> );
>>
>> class Expr{
>>     abstract Expression expression()pure nothrow @safe @nogc;
>>
>> }
>
> I don't see what this buys you compared to sticking with one or 
> the other, but you are correct that it is technically possible.

It infer function atributes (pure, nothrow @nogc @safe) for 
"visitor" and let you use classes and inheritence.
With standard visitor pattern you need PureVisitor. 
NothrowVisitor, PureNothrowVisitor...




More information about the Digitalmars-d-learn mailing list