Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.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_Scala Cats - Fatal编程技术网

为什么scala在包装它们时不能推断出正确的类型?

为什么scala在包装它们时不能推断出正确的类型?,scala,scala-cats,Scala,Scala Cats,代码如下: // eventually will be an implicit class with extension methods class EitherTWrapper [L,R] (ei: EitherT[Future,L,R]) new EitherTWrapper(??? : EitherT[Future,Nothing,Boolean]) 无法使用以下命令编译: type mismatch; found : cats.data.EitherT[scala.concur

代码如下:

// eventually will be an implicit class with extension methods
class EitherTWrapper [L,R] (ei: EitherT[Future,L,R])

new EitherTWrapper(??? : EitherT[Future,Nothing,Boolean])
无法使用以下命令编译:

type mismatch;
 found   : cats.data.EitherT[scala.concurrent.Future,Nothing,Boolean]
 required: cats.data.EitherT[scala.concurrent.Future,L,Boolean]
Note: Nothing <: L, but class EitherT is invariant in type A.
You may wish to define A as +A instead. (SLS 4.5)
这是可行的,但如果我试图使它成为隐式类,我就不能这样做


我希望这能奏效。如何定义一个可以封装EitherT的类?

正如错误所示,将包装器更改为
类EitherTWrapper[+L,R](ei:EitherT[Future,L,R])
将修复编译错误


您的错误表明
Nothing显然,这是一个已知的scala编译器错误(限制?):

似乎有两种解决方法:

  • 使包装器上的类型协变(这不是很好,因为现在包装器与被包装的对象具有不同的变化行为,并且仅当类型参数没有被反向使用时才起作用)
  • 通过为
    EitherT
    创建一个单独的包装器,专门使用
    Nothing
    处理该版本。即使在尝试将包装器用作隐式类时,这种方法仍然有效

  • 如果你在一个同伴对象中使用一个DEF来构建,而不是直接使用新的,那么它会起作用吗?正如错误建议的那样,<代码>类iEthTrWrrPer-[+L,R](EI:EthTur[未来,L,R])< /代码>不起作用吗?@ JAMESWITELY,我认为这个错误是指EitherT中的一个,所以我甚至没有考虑在EitherTWrapper改变它。但你是对的,在包装器上更改它确实解决了这个问题(它也带来了其他问题,因为我有一些方法使用了一个反向变量,但也能够找到一种方法让它们工作)。谢谢。我的问题不是方差,而是推断。当显式提供类型时,原始代码将正确编译。更改方差是解决推理问题的一种变通方法。我认为问题更多地与
    Nothing
    类型以及编译器如何对其进行特殊处理有关。稍后将不得不返回到此,并提供更多信息。
    new EitherTWrapper[Nothing,Boolean](??? : EitherT[Future,Nothing,Boolean])