Scala partially applied type constructor inference

巧了我就是萌 提交于 2019-12-08 10:19:35

问题


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 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)
  }
}

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


回答1:


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.



来源:https://stackoverflow.com/questions/4526822/scala-partially-applied-type-constructor-inference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!