gpt4 book ai didi

scala - 当 Disjunction 是最外层容器时,如何使用 Monad Transformer?

转载 作者:行者123 更新时间:2023-12-04 21:16:26 25 4
gpt4 key购买 nike

val vLInts = (1 to 10).toList.right[String]

for {
i <- ListT(vLints)
_ = println(i)
} yield i

//error: no type parameters for method apply:(underlying: M[List[A]])scalaz.ListT[M,A] in object ListT exist so that it can be applied to arguments (scalaz.\/[String,List[Int]])

这里的问题是析取 \/[A, B]有 2 个泛型,因此不是 Monad。当我创建类型别名时
type Attempt[A] = \/[String, A]

它成功了,因为我已经固定左侧并且我现在有了 Monad。如果最外层类型是析取,我如何让我的 Monad Transformer 工作,而不诉诸类型别名?

最佳答案

for{
i <- ListT[({type l[+a] = String \/ a})#l,Int](vLints)
_ = println(i)
} yield i

显然,答案是 lambda 类型。它不漂亮,但它有效。

关于scala - 当 Disjunction 是最外层容器时,如何使用 Monad Transformer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22412863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com