通常在Scala文献中,我遇到了"抽象结束"这个短语,但我不明白其意图. 例如,马丁奥德斯基写道
您可以将方法(或"函数")作为参数传递,也可以对它们进行抽象.您可以将类型指定为参数,也可以对它们进行抽象.
另一个例子,在"弃用观察者模式"一文中,
我们的事件流是第一类值的结果是我们可以抽象它们.
我已经读过第一阶泛型"抽象类型",而monads"抽象类型构造函数".我们还在Cake Pattern论文中看到了这样的短语.引用许多这样的例子中的一个:
抽象类型成员提供了抽象的具体类型的组件的灵活方式.
即使相关的堆栈溢出问题也使用此术语. "不能存在抽象的参数化类型..."
所以......"抽象"究竟意味着什么?
我试图定义一个在Scala中接受存在性更高的kinded类型的类型.
不幸的是,Scalac不允许这样做.
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait H[F[_, _]]
trait T[A, B]
val h:H[T] = null
val e:H[F] forSome { type F[A, B] } = h
// Exiting paste mode, now interpreting.
<console>:13: error: type mismatch;
found : H[T]
required: H[_[A, B] <: Any]
Note: T <: Any, but trait H …Run Code Online (Sandbox Code Playgroud) type-systems functional-programming scala existential-type higher-kinded-types
我有scala泛型的问题.虽然我在这里定义的第一个函数似乎完全可以,但编译器抱怨第二个定义:
error: Parameter type in structural refinement may not refer to an abstract type defined outside that refinement
def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = {
^
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
trait Lifter[C[_]] {
implicit def liftToMonad[A](c: C[A]) = new {
def >>=[B](f: A => C[B])(implicit m: Monad[C]): C[B] = {
m >>= (c, f)
}
def >>[B](a: C[B])(implicit m: Monad[C]): C[B] = {
m >> a
}
}
}
Run Code Online (Sandbox Code Playgroud)
重要提示:这不是关于Monads的问题,这是一个关于scala多态性的问题.
编辑:这是我的Monad定义
trait Monad[C[_]] {
def >>=[A, B](a: C[A], f: A => C[B]): …Run Code Online (Sandbox Code Playgroud)