Scala partially applied type constructor inference
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)
That worked even though (as has been asked before!) the method <|*|>
is on MA
which has one type pa开发者_如何学运维rameter, 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)
}
}
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)
In my REPL, this actually causes a scalac error, rather than a sensible error message
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])
In the second case, the type argument to the method ∘∘
cannot be inferred, until #2712 is tackled.
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)
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.
精彩评论