3

I want to define an overloaded method, one that accepts a function0, and one that accepts a function1, viz:

def produces(f: Context => Any): Processor = ...

def produces(thunk: => Any): Processor = ...

This compiles fine because the first is compiled to use a Function1 and the second to use a Function0, however the issue arises when I want to invoke, thus:

produces {
 ctx => "hello"
}

Gives me an

missing parameter type

There's only one parameter that accepts an input, so why can't it infer.

Any tips on what I can do to get around this (other than rename one of the methods :))

4
  • 1
  • Not the same thing really.
    – sksamuel
    Apr 14, 2014 at 6:40
  • @monkjack I'd say it's deceptively close actually. How is it supposed to know after the first string argument which curried function definition you're setting up to call next?
    – wheaties
    Apr 14, 2014 at 18:20
  • Ok I see. I don't know what the curried parameter groups gets complied into, but would have thought the 2nd parameter in the invocation was enough for inference?
    – sksamuel
    Apr 14, 2014 at 18:48

2 Answers 2

2

It's kind of silly-looking, but you can (awkwardly) help out the compiler's search strategy by requiring an implicit conversion for the I-can-take-anything-by-name version:

object Test {
  implicit def low_priority_conversion(t: Test.type) = LowPriority
  object LowPriority {
    def produces(x: => Any) : Boolean = false
  }
  def produces(f: String => Any): Boolean = true
}

Whether you import Test._ or not, you'll find you have the correct behavior:

scala> Test.produces("fish")
res0: Boolean = false

scala> Test.produces(_.length)
res1: Boolean = true

scala> import Test._
import Test._

scala> produces("fish")
res2: Boolean = false

scala> produces(_.length)
res3: Boolean = true
4
  • This looks promising, but I cannot get it to work even if I have the implicit in the same class. Does this only work on objects->objects?
    – sksamuel
    Apr 14, 2014 at 6:44
  • @monkjack - If it is a class instead of an object, you need to put the implicit def into the companion object. Otherwise I'm not sure what you're doing so I can't tell why it doesn't work.
    – Rex Kerr
    Apr 14, 2014 at 8:22
  • I've had a play about and your trick only works when you qualify the method invocation, eg with an object or this. In my case the methods are part of a trait that is mixed in. I cannot get it to work for this scenario.
    – sksamuel
    Apr 14, 2014 at 18:04
  • @monkjack - This is an answer to the question you asked, though. Maybe you should be explicit about the required scenario in your question?
    – Rex Kerr
    Apr 14, 2014 at 19:06
0

I think the real answer here is, "don't do that"! ;-) The argument you're passing is actually valid for both your functions. The only reason you got an error rather than having the compiler silently call the "wrong"/unexpected function is because it couldn't infer the type for ctx (which is only because Scala's type inferrer doesn't support unification (yet)).

Basically, your problem is that Any is a supertype of Context => Any.

If you really want to name both functions the same, an alternative is to make one of them take a thunk as an argument rather than a pass-by-name Any. (But then you'd need to thunk any arguments you want to pass to the thunk version rather than having it implicitly evaluated lazily.)

For example:

scala> object Test {
     | def produces(f: String => Any): Boolean = true
     | def produces(x: () => Any): Boolean = false
     | }
defined object Test

scala> Test.produces(x => 5)
res9: Boolean = true

scala> Test.produces(5)
<console>:12: error: overloaded method value produces with alternatives:
  (x: () => Any)Boolean <and>
  (f: String => Any)Boolean
 cannot be applied to (Int)
       Test.produces(5)
            ^

scala> Test.produces(() => 5)
res11: Boolean = false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.