Multiple parameter closure argument type not inferred

后端 未结 2 562
无人共我
无人共我 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:49

    One of the ways to fix this is to define multiple argument lists. So your map1 method would be defined like this:

    def map1[U, V](behaviour: Behaviour[U])(func: (T, U) => V): Behaviour[V] = ...
    

    and you can use it like this:

    beh.map1(beh2)((a, b) => a + b)
    beh.map1(beh2)(_ + _)
    

    I'm not completely sure why type inference does not work in your case, but I believe that it has something to do with usage of U type parameter. You are using it twice - for the first and second argument. It's probably too complicated for compiler to figure it out. In case of 2 argument lists, U would be inferred during first argument list compilation, and the second argument list will use already inferred type.

提交回复
热议问题