我可以在Predef的API文档中看到它们是泛型函数类型(From)=> To的子类,但就是这样.嗯什么?也许某处有文档,但搜索引擎不能很好地处理"<:<"之类的"名称",所以我无法找到它.
后续问题:我什么时候应该使用这些时髦的符号/类,为什么?
任何人都可以<:<在scala中提供有关运算符的一些细节.我认为:
if(apple <:< fruit) //checks if apple is a subclass of fruit.
Run Code Online (Sandbox Code Playgroud)
还有其他解释吗?我在scala源文件中看到了很多定义.
在以下代码中(来自Scala中的Functional Programming):
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait Monad[F[_]] {
def unit[A](a: => A): F[A]
def flatMap[A,B](ma: F[A])(f: A => F[B]): F[B]
def apply[A](a: => A): F[A]
}
Run Code Online (Sandbox Code Playgroud)
我看到以下警告:
[warn] C:\...\Monad.scala:3: higher-kinded type should be enabled
[warn] by making the implicit value scala.language.higherKinds visible.
[warn] This can be achieved by adding the import clause 'import scala.language.higherKinds'
[warn] or by setting the compiler option -language:higherKinds.
[warn] See the Scala docs for value scala.language.higherKinds …Run Code Online (Sandbox Code Playgroud) 我一直在和Scalaz一起玩,以获得scala的一点点haskell感觉.为了理解scala中的工作原理,我开始自己实现各种代数结构,并且遇到了Scalaz人员提到过的行为.
这是我实现仿函数的示例代码:
trait Functor[M[_]] {
def fmap[A, B](a: M[A], b: A => B): M[B]
}
sealed abstract class Foo[+A]
case class Bar[A]() extends Foo[A]
case class Baz[A]() extends Foo[A]
object Functor {
implicit val optionFunctor: Functor[Option] = new Functor[Option]{
def fmap[A, B](a: Option[A], b: A => B): Option[B] = a match {
case Some(x) => Some(b(x))
case None => None
}
}
implicit val fooFunctor: Functor[Foo] = new Functor[Foo] {
def fmap[A, B](a: Foo[A], b: A => B): Foo[B] = …Run Code Online (Sandbox Code Playgroud)