Multiple parameter closure argument type not inferred

后端 未结 2 560
无人共我
无人共我 2020-12-03 11:55

I have a piece of code that I can\'t get to behave in the way I\'d like. I have a class defined in the following way (stripped down for this):

class Behaviou         


        
2条回答
  •  自闭症患者
    2020-12-03 12:50

    See this scala-debate thread for a discussion of what's going on here. The problem is that Scala's type inference happens per parameter list, not per parameter.

    As Josh Suereth notes in that thread, there's a good reason for the current approach. If Scala had per-parameter type inference, the compiler couldn't infer an upper bound across types in the same parameter list. Consider the following:

    trait X
    class Y extends X
    class Z extends X
    
    val y = new Y
    val z = new Z
    
    def f[A](a: A, b: A): (A, A) = (a, b)
    def g[A](a: A)(b: A): (A, A) = (a, b)
    

    f(y, z) works exactly as we'd expect, but g(y)(z) gives a type mismatch, since by the time the compiler gets to the second argument list it's already chosen Y as the type for A.

提交回复
热议问题