在下面的代码中,您可以看到一些令人生畏的重复输入:
implicit def intEqualInstance
[ root,
path <: TypePath[root] ]
( implicit mappingResolver: rel.rules.MappingResolver[path] )
= {
new genExp.Compiler
[ genExp.templates.Where.Comparison[root, path, genExp.templates.Operator.Equal, typeLevel.Bool],
genExp.values.Where.Comparison[ genExp.values.Expression.Value[ Int ] ],
relExp.templates.Where,
List[rel.Value] ]
{
override def compileTemplate(tpl: genExp.templates.Where.Comparison[root, path, genExp.templates.Operator.Equal, typeLevel.Bool]) = {
...
}
override def processValues(vals: genExp.values.Where.Comparison[ genExp.values.Expression.Value[ Int ] ]) = {
...
}
}
}
我想为重复的类型声明类型别名。
我已尝试添加子类型约束,如下所示,但它使隐式解决方案失败:
implicit def intEqualInstance
[ ...,
inputTemplate <: genExp.templates.Where.Comparison[root, path, genExp.templates.Operator.Equal, typeLevel.Bool],
inputValues <: genExp.values.Where.Comparison[ genExp.values.Expression.Value[ Int ] ] ]
( implicit mappingResolver: rel.rules.MappingResolver[path] )
= {
new genExp.Compiler
[ inputTemplate,
inputValues,
... ]
{
override def compileTemplate(tpl: inputTemplate) = ...
override def processValues(vals: inputValues) = ...
}
...
添加本地类型别名会使隐式解析失败:
implicit def intEqualInstance
...
= {
type InputTemplate = genExp.templates.Where.Comparison[root, path, genExp.templates.Operator.Equal, typeLevel.Bool]
type InputValues = genExp.values.Where.Comparison[ genExp.values.Expression.Value[ Int ] ]
new genExp.Compiler
[ InputTemplate
InputValues,
... ]
...
有没有解决方案?
答案 0 :(得分:1)
我能想到的最好的方法是在外部范围内声明类型别名:
private type InputTemplate[root, path <: TypePath[root]] =
genExp.templates.Where.Comparison[root, path, genExp.templates.Operator.Equal, typeLevel.Bool]
private type InputValues =
genExp.values.Where.Comparison[ genExp.values.Expression.Value[ Int ] ]
implicit def intEqualInstance
[ root, path <: TypePath[root] ]
( implicit mappingResolver: rel.rules.MappingResolver[path] )
=
new genExp.Compiler
[ InputTemplate[root, path], InputValues, ... ]
{
override def compileTemplate(tpl: InputTemplate[root, path]) = ...
override def processValues(vals: InputValues) = ...
}