scala - 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[_]]]
)
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 – i.e. 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 type 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)) }
推荐阅读
- apache-kafka - kafka 消费者命令行工具未按预期工作
- r - 如何为 r 中的两个变量提供不同的色谱?
- php - Geonames api循环并获取城市名称
- python - 如何在执行 SMOTE、python 之前删除具有少于一定数量示例的少数类
- php - WordPress:联系表格 7 删除所有类和 ID
- android - 当使用 .xml 代替 google_services.json 时,Gradle 构建失败
- sql-server - 登录失败。登录来自不受信任的域,不能仅在一台计算机上与 Windows 身份验证一起使用
- python - 无法安装 pip 模块
- elasticsearch - 无法更改弹性搜索配置中的 path.data
- android - 如何在 Android API 30 或 Android R 版本设备中创建文件夹目录