我试图表示衍生尺寸。基本设置如下:
trait Dimension
trait BaseDimension extends Dimension
trait Length extends BaseDimension
trait Weight extends BaseDimension
trait HigherDimension[D <: BaseDimension, E <: Nat] extends Dimension
此处,HigherDimension
来自某些BaseDimension
,为其指定了一个指数(HigherDimension[Length, Nat._2]
(&#34; Length^2
&#34;),即区域)。< / p>
现在,我想计算二维产品的类型。其中一种情况是,两个具有相同HigherDimension
的{{1}}成倍增加(f.ex。BaseDimension
):
Length^1 * Length^2 = Length^3
但这不起作用(/** Type Class computing product: D1 * D2 **/
trait DimensionProd[D1 <: Dimension, D2 <: Dimension] { type Out <: Dimension }
object DimensionProd {
def apply[D1 <: Dimension, D2 <: Dimension](implicit prod: DimensionProd[D1, D2]): Aux[D1, D2, prod.Out] = prod
type Aux[A <: Dimension, B <: Dimension, C <: Dimension] = DimensionProd[A, B] { type Out = C }
// handle the case with shared base dimension (f.ex. Length^2 * Length = Length^3)
implicit def higherDimSharedBase[D1 <: HigherDimension[Base, D1E], D2 <: HigherDimension[Base, D2E], Base <: BaseDimension, D1E <: Nat, D2E <: Nat]
(implicit sum: Sum[D1E, D2E]) = new DimensionProd[D1, D2] { type Out = HigherDimension[Base, sum.Out] }
}
不会编译)。我理解这是因为编译器无法从D1 / D2的边界中找出implicitly[DimensionProd[HigherDimension[Length, Nat._1], HigherDimension[Length, Nat._2]]]
/ Base
/ D1E
的类型。
明确输入时一切正常:
D2E
问题是如何定义// this compiles
DimensionProd.higherDimSharedBase[HigherDimension[Length, Nat._1], HigherDimension[Length, Nat._2], Length, Nat._1, Nat._2]
以便可以推断出所有内容,并且它仅涵盖预期的情况(两个higherDimSharedBase
具有共享HigherDimension
)?