简体   繁体   English

为什么Option [Try [_]]不符合F [_]?

[英]Why does Option[Try[_]] not conform to F[_]?

So having something like this: 所以有这样的事情:

@ trait IntWrapper[F[_]] { def apply(i: Int): F[Int] }
defined trait IntWrapper

@ class OptWrapper extends IntWrapper[Option] { def apply(i: Int) = Option(i) }
defined class OptWrapper

I now want to do something like this: 我现在想做这样的事情:

@ class TryOptWrapper extends IntWrapper[Try[Option]] { def apply(i: Int) = Try(Option(i)) }
cmd19.sc:1: scala.util.Try[Option] takes no type parameters, expected: one
class TryOptWrapper extends IntWrapper[Try[Option]] { def apply(i: Int) = Try(Option(i)) }
                                       ^
Compilation Failed

(Same thing if I declare the trait extension as class TryOptWrapper extends IntWrapper[Try[Option[_]]] ) (如果我在class TryOptWrapper extends IntWrapper[Try[Option[_]]]声明了特征扩展,则与此相同)

Now, perhaps the most interestingly, this works: 现在,也许最有趣的是,这可行:

@ type Topt[T] = Try[Option[T]]

@ class ToptWrapper extends IntWrapper[Topt] { def apply(i: Int) = Try(Option(i)) }
defined class ToptWrapper

Now, is it possible to do the same thing – ie implement a trait with a type parameter being a nested parametrized type – without having to explicitly declare the type alias? 现在,是否可以做同样的事情-即使用类型参数为嵌套参数化类型的类型参数实现特征-而无需显式声明类型别名? It definitely feels like I'm missing some syntax here. 确实感觉就像我在这里缺少一些语法。

Try expects type parameter of kind * and Option has kind * => * so you can't write Try[Option] , only Try[Option[Int]] , Try[Option[String]] , Try[Option[_]] ... Try期望类型*类型参数,而Option具有类型* => *因此您不能编写Try[Option] ,只能编写Try[Option[Int]]Try[Option[String]]Try[Option[_]] ...

Try type lambda 尝试输入lambda

class TryOptWrapper extends IntWrapper[({ type λ[A] = Try[Option[A]] })#λ] { def apply(i: Int) = Try(Option(i)) }

Or with kind-projector 或带投影仪

class TryOptWrapper extends IntWrapper[Lambda[A => Try[Option[A]]]] { def apply(i: Int) = Try(Option(i)) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM