简体   繁体   English

对 Scala 类型参数施加多个泛型类型约束

[英]Impose more than one generic type constraint on a Scala type parameter

I want to do the following stuff using Scala's context-bound pattern:我想使用 Scala 的上下文绑定模式执行以下操作:

    class Polynomial[T: Ring] {
        def apply[X: Ring with Includes[T]](x: X): X = ...
        ...
    }

This is a polynomial class which requires the coefficients are elements in a Ring T .这是一个多项式类,它要求系数是Ring T中的元素。 When applying this polynomial to a element (evaluation), the type of the parameter x is required to be a ring, and elements of type T can be implicitly cast to type X .将此多项式应用于元素(求值)时,参数x的类型必须是环,并且类型T元素可以隐式转换为类型X for example T = Double, X = SquareMatrix .例如T = Double, X = SquareMatrix

How can I impose more than one type constraints to a generic type parameter in Scala?如何对 Scala 中的泛型类型参数强加一种以上的类型约束?

It seems that [X: T] syntax is not enough for imposing two generic type constraints.似乎[X: T]语法不足以强加两个泛型类型约束。 Using two implicit parameters solved this problem:使用两个隐式参数解决了这个问题:

   def apply[X](x: X)(implicit ring: Ring[X], conv: Includes[X, T]): X = {
     var xi = ring.one
     var sum = ring.zero
     for (i <- 0 to degree) {
       sum += xi * conv.from(coef(i))
       xi *= x
     }
    return sum
   }

The [X: T] syntax is simply syntactic sugar for requiring an implicit parameter of type T[X] . [X: T]语法只是语法糖,用于要求类型为T[X]的隐式参数。 For example, f and f2 below are identical:例如,下面的ff2是相同的:

 def f[T: X]
 def f2[T](implicit xt: X[T])

As for your code, if you explicitly write out the implicit parameter you will be able to express your constraints:至于您的代码,如果您明确写出隐式参数,您将能够表达您的约束:

class Polynomial[T: Ring] {
    def apply[X](x: X)(implicit xt: Ring[X] with Includes[T]): X = ...
}

Note that you aren't imposing more than one constraint on either X or T , your constraint is simply too complex for the context-bound syntactic sugar.请注意,您不会对XT施加多个约束,您的约束对于上下文绑定的语法糖来说太复杂了。 However, Scala does allow you to impose as many context-bounds on type parameters as you like, and you can combine them with upper- and lower-bounds as well:但是,Scala 确实允许您根据需要在类型参数上施加尽可能多的上下文边界,并且您也可以将它们与上限和下限结合起来:

def f[T >: List[Int] <: AnyRef : Ordering : List] = ???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM