Search code examples
scalacompiler-constructiontypesprogramming-languagesfunctional-programming

Scala not handling non-explicitly typed closures for ':' functions


So basically I want to write a function that can be written like this:

{ println(_) } =: thing

Here I want it to actually do thing.=:(println(_)). Lets say for the sake of argument that =: has the following implementation:

def =:(block : Int => Any) {
    block(5)
}

So I try calling it the above way and I get:

<console>:10: error: type mismatch;
   found   : Unit
   required: Int => Any
             println(_) =: thing

I then try doing this:

thing.=:(println(_))

This way I get a nice 5 printed to the terminal. I then tried this:

{ value => println(value) } =: thing

This again failed and told me there is a "missing parameter type". I'm guessing that this is because Scala tries to parse/compile the function argument first in this case and doesn't guess the type as it would (I'm completely guessing here) when it is called the more conventional way (with the dot operator).

Could anyone shed more light on the problems here and also maybe suggest the best way to achieve something close to my original goal?

P.S. Sorry about the title. I'll rename it once I have a better understanding of the problem.


Solution

  • Type inference works left-to-right, even if symbolic method names of the #: form end up working right to left. You can use a helper method if you actually only have one type:

    def let(fi: Int => Any) = fi
    case class Thing(i: Int) {
      def =:(f: Int => Any) = f(i)
    }
    
    scala> let(_+2) =: Thing(5)
    res4: Any = 7
    

    but this isn't an entirely satisfactory solution if you have many possible type signatures because you have to match the helper method name on the left to the required type on the right.