更高的金属类型 - 多种类型

时间:2015-09-21 18:17:32

标签: scala higher-kinded-types

鉴于以下Maybe的代数数据类型:

scala> sealed trait Maybe[A]
defined trait Maybe

// "Nada" in Spanish means "nothing"
scala> case object Nada extends Maybe[Nothing]
defined object Nada

scala> case class Just[A](x: A) extends Maybe[A]
defined class Just

然后,我定义了一个方法success,它采用了类型A和更高知名度的类型M[A]

scala> def success[A, M[A]](x: A): M[A] = ???
warning: there was one feature warning; re-run with -feature for details
success: [A, M[A]](x: A)M[A]

我可以定义Functor

scala> case class Functor[A](x: A)
defined class Functor

然后,我可以将其应用于success

scala> success[Int, Functor](5)
scala.NotImplementedError: an implementation is missing

但是,我无法应用需要 2类型参数的更高级别的类型。

scala> success[Int, Either]
<console>:12: error: Either takes two type parameters, expected: one
       success[Int, Either]
                    ^

我是否可以修改success以接受任何更高级别的类型,即1 ... N种?

0 个答案:

没有答案