Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 一无所有不';不要在猫身上工作_Scala_Monads_Scalaz_Scala Cats - Fatal编程技术网

Scala 一无所有不';不要在猫身上工作

Scala 一无所有不';不要在猫身上工作,scala,monads,scalaz,scala-cats,Scala,Monads,Scalaz,Scala Cats,我对猫的理解有问题。 请考虑以下摘录: import cats._ import cats.implicits._ final case class My[T]() implicit val monadMy: Monad[My] = new Monad[My] { ... } // this compiles def test11[A, B](m: My[A], f: A => B): My[B] = m.map(f) // this fails: value map is not a

我对猫的理解有问题。 请考虑以下摘录:

import cats._
import cats.implicits._

final case class My[T]()
implicit val monadMy: Monad[My] = new Monad[My] { ... }

// this compiles
def test11[A, B](m: My[A], f: A => B): My[B] = m.map(f)
// this fails: value map is not a member of My[Nothing]
def test12[B](m: My[Nothing], f: Nothing => B): My[B] = m.map(f)

// this compiles
def test21[A, B](m: My[A], f: A => My[B]): My[B] = m.flatMap(f)
// this fails: type mismatch;
// [error]  found   : A => My[Nothing]
// [error]  required: A => My[B]
def test22[A](m: My[A], f: A => My[Nothing]): My[Nothing] = m.flatMap(f)
我意识到
test12
可能看起来很奇怪,但是它允许
使用
语法:

for (...; nothing <- My[Nothing]()) yield nothing

for(…;nothing尝试使
My
T
中协变。然后代码编译

final case class My[+T]()
用不变量
T

Information:27.09.17 15:55 - Compilation completed with 2 errors and 0 warnings in 2s 804ms
/home/dmitin/Projects/myproject/src/main/scala/App.scala

Information:(19, 59) toFunctorOps is not a valid implicit value for m.type => ?{def map: ?} because:
type mismatch;
 found   : m.type (with underlying type App.My[Nothing])
 required: App.My[A]
Note: Nothing <: A, but class My is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
  def test12[B](m: My[Nothing], f: Nothing => B): My[B] = m.map(f)

Error:(19, 61) value map is not a member of App.My[Nothing]
  def test12[B](m: My[Nothing], f: Nothing => B): My[B] = m.map(f)

Error:(26, 73) type mismatch;
 found   : A => App.My[Nothing]
 required: A => App.My[B]
  def test22[A](m: My[A], f: A => My[Nothing]): My[Nothing] = m.flatMap(f)
信息:27.09.17 15:55-编译完成,2s 804ms中有2个错误和0个警告
/home/dmitin/Projects/myproject/src/main/scala/App.scala
信息:(19,59)toFunctorOps不是m.type=>?{def map:?}的有效隐式值,因为:
类型失配;
找到:m.type(具有基础类型App.My[无])
必需:App.My[A]
注:无B):My[B]=m.map(f)
错误:(19,61)值映射不是App.My[无]的成员
def test12[B](m:My[Nothing],f:Nothing=>B):My[B]=m.map(f)
错误:(26,73)类型不匹配;
找到:A=>App.My[无]
必需:A=>App.My[B]
def test22[A](m:My[A],f:A=>My[Nothing]):My[Nothing]=m.flatMap(f)

有时打开
scalacOptions+=“-Xlog implicits”

会很有用,我想问一下,为什么您需要一个monad
Nothing
,这是您无法创建的类型?您试图实现什么?例如,列表monad失败,如果发生该故障,将使所有内容都变为空列表,这将是
val失败:list[Nothing]=Nil
。当然,我可以做
def failure[T]:List[T]=Nil
,它会工作,但是…List monad的失败意味着什么?我不太明白。这是一种共识,即空列表意味着失败,就像
或者
左[某物]
是失败,
选项
是失败一样。因为你不能创建任何东西的实例,你不能为它创建类型类实例,使用选项对于您的用例来说更为合理。无论如何,列表[无]是毫无意义的,而列表[选项]是。这会有所帮助,但我无法使其协变,因为我最初使用的是
cats.Free
,它不是协变的,而是它的第二个参数。我会更新我的问题。