Scala 部分应用类型构造函数推断 [英] Scala partially applied type constructor inference
问题描述
我正在使用 scala-2.8.1 和 scalaz-5.0.谁能解释一下为什么 PartialApply1Of2
可以在一种情况下被推断出来,而在另一种情况下不能?
I'm using scala-2.8.1 and scalaz-5.0. Can anyone explain exactly why a PartialApply1Of2
can be inferrred in the one case but not in the other?
scala> 1.success[String] <|*|> "Bah".fail[Int]
res1: scalaz.Validation[String,(Int, Int)] = Failure(Bah)
即使(之前已经问过!)方法<|*|>
在 MA
上,它有一个类型参数,而不是两个(如 Validation
有).我无法在 IDEA REPL 中使用 unicode,所以这里是:
That worked even though (as has been asked before!) the method <|*|>
is on MA
which has one type parameter, not two (as Validation
has). I cannot get unicode working in my IDEA REPL, so here goes:
object testy {
def main(args: Array[String]) {
import scalaz._
import Scalaz._
val ps = NonEmptyList(1.success[String], "Bah".fail[Int])
val res = ps.∘∘((_ : Int) % 2 == 0) //DOES NOT COMPILE
println(res)
}
}
我可以为呼叫提供特定类型,一切都很好.为什么 scalac
不能推断出这个?
I can provide a specific type to the call and all is good. Why can scalac
not infer this?
ps.∘∘[PartialApply1Of2[Validation, String]#Apply, Int, Boolean]((_ : Int) % 2 == 0)
在我的 REPL 中,这实际上会导致 scalac 错误,而不是合理的错误消息
In my REPL, this actually causes a scalac error, rather than a sensible error message
推荐答案
在第一种情况下,隐式视图 ValidationMA
是推断出来的,类型参数 Int
是推断:
In the first case, the implicit view ValidationMA
is inferred, and the the type argument Int
is inferred:
Scalaz.ValidationMA(1.success[String]).<|*|>[Int]("Bah".fail[Int])
在第二种情况下,∘∘
方法的类型参数无法推断,直到 #2712 已解决.
In the second case, the type argument to the method ∘∘
cannot be inferred, until #2712 is tackled.
我怀疑您遇到的 scalac 内部错误与 #2741 有关/#4079.如果是这样,您可以使用 Type Lambda 重写它以解决该错误.
I suspect that the scalac internal error you encountered is related to #2741 / #4079. If so, you can rewrite it with a Type Lambda to workaround the error.
ps.∘∘[({type X[a]=Validation[String, a]})#X, Int, Boolean]((_ : Int) % 2 == 0)
我建议在所有情况下都使用此语法而不是 PartialApplyNofM
,因为我发现它更具可读性.使用最新版本的 IntelliJ,您甚至可以启用代码折叠(设置、代码样式、Scala、折叠、Lambas 类型),以 隐藏一些语法混乱.
I recommend using this syntax instead of PartialApplyNofM
in all cases, as I find it more readable. With a recent build of IntelliJ, you can even enable a code folding (Settings, Code Style, Scala, Folding, Type Lambas), to hide some syntactic clutter.
这篇关于Scala 部分应用类型构造函数推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!