Templates, templates, templates...

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 23 05:19:34 PST 2016


On 23.01.2016 12:30, Voitech wrote:
> Ok so i want to hold different types in LogicRule maybe Algebraic
> implementation would do?
>
> private alias ControllTemplate(T) =Rule!(T,ControllFlag);
> private alias SymbolRule =ControllTemplate!(SymbolType);
> private alias StringRule =ControllTemplate!(SymbolRule[]);
> private alias LogicTemplate(T...)
> =Rule!(Algebraic!(ControllTemplate(T))[],LogicFlag); <--error

You're missing an exclamation mark there, and you've got the order of 
Algebraic and ControllTemplate wrong. This compiles:

private alias LogicTemplate(T...) = 
Rule!(ControllTemplate!(Algebraic!T)[],LogicFlag);

> private alias AlgebraicLogicRule = LogicTemplate!(SymbolRule,StringRule);
>
> error:
> Error: cannot pass type (Rule!(SymbolType, ControllFlag),
> Rule!(Rule!(SymbolType, ControllFlag)[], ControllFlag)) as a function
> argument
[...]
> Is there any nicer way to handle this case ?

Instead of Algebraic you could use a common base class, or interface, 
for the Rule instantiations:

abstract class RuleBase
{
     ... whatever common functionality rules have ...
}
class Rule(V,F) : RuleBase { ...}

But I have to say that I'm having trouble making sense of all that class 
and template complexity, and how it helps in actually validating user input.

Since this is a parsing thing, you may want to look into writing parsers 
an/or using a parse generator. I think Pegged is the most popular one 
for D. http://code.dlang.org/packages/pegged


More information about the Digitalmars-d-learn mailing list