4

I have a function like so:

def ifSome[B, _](pairs:(Option[B], B => _)*) {
  for((paramOption, setFunc) <- pairs)
    for(someParam <- paramOption) setFunc(someParam)
}

and overloaded functions like these:

class Foo{ 
  var b=""
  def setB(b:String){this.b = b}
  def setB(b:Int){this.b = b.toString}
}

val f = new Foo

then the following line produces an error:

ifSome(Option("hi") -> f.setB _)

<console>:11: error: ambiguous reference to overloaded definition,
both method setB in class Foo of type (b: Int)Unit
and  method setB in class Foo of type (b: String)Unit
match expected type ?
                 ifSome(Option("hi") -> f.setB _)

But the compiler knows that we're looking for a Function1[java.lang.String, _], so why should the presence of a Function1[Int, _] present any confusion? Am I missing something or is this a compiler bug (or perhaps it should be a feature request)?

I was able to workaround this by using a type annotation like so

ifSome(Option("hi") -> (f.setB _:String=>Unit))

but I'd like to understand why this is necessary.

2 Answers 2

2

You'll want to try $ scalac -Ydebug -Yinfer-debug x.scala but first you'll want to minimize.

In this case, you'll see how in the curried version, B is solved in the first param list:

[infer method] solving for B in (bs: B*)(bfs: Function1[B, _]*)Nothing 
based on (String)(bfs: Function1[B, _]*)Nothing (solved: B=String)

For the uncurried version, you'll see some strangeness around

[infer view] <empty> with pt=String => Int

as it tries to disambiguate the overload, which may lead you to the weird solution below.

The dummy implicit serves the sole purpose of resolving the overload so that inference can get on with it. The implicit itself is unused and remains unimplemented???

That's a pretty weird solution, but you know that overloading is evil, right? And you've got to fight evil with whatever tools are at your disposal.

Also see that your type annotation workaround is more laborious than just specifying the type param in the normal way.

object Test extends App {
  def f[B](pairs: (B, B => _)*) = ???
  def f2[B](bs: B*)(bfs: (B => _)*) = ???

  def g(b: String) = ???
  def g(b: Int) = ???

  // explicitly
  f[String](Pair("hi", g _))

  // solves for B in first ps
  f2("hi")(g _)

  // using Pair instead of arrow means less debug output
  //f(Pair("hi", g _))

  locally {
    // unused, but selects g(String) and solves B=String
    import language.implicitConversions
    implicit def cnv1(v: String): Int = ???
    f(Pair("hi", g _))
  }

  // a more heavy-handed way to fix the type
  class P[A](a: A, fnc: A => _)
  class PS(a: String, fnc: String => _) extends P[String](a, fnc)
  def p[A](ps: P[A]*) = ???
  p(new PS("hi", g _))
}
2

Type inference in Scala only works from one parameter list to the next. Since your ifSome only has one parameter list, Scala won't infer anything. You can change ifSome as follows:

def ifSome[B, _](opts:Option[B]*)(funs: (B => _)*) {
  val pairs = opts.zip(funs)
  for((paramOption, setFunc) <- pairs)
    for(someParam <- paramOption) setFunc(someParam)
}

leave Foo as it is...

class Foo{ 
  var b=""
  def setB(b:String){this.b = b}
  def setB(b:Int){this.b = b.toString}
}

val f = new Foo

And change the call to ifSome accordingly:

ifSome(Option("hi"))(f.setB _)

And it all works. Now of course you have to check whether opts and funs have the same length at runtime.

6
  • 1
    As I explained in my answer, type inference (figuring out that B is String) works from one parameter list to the next. So it is about parameter lists.
    – Kim Stebel
    Sep 28, 2012 at 22:53
  • Thanks for the second workaround, but I'm still scratching my head about why it's necessary.
    – scalapeno
    Sep 28, 2012 at 23:27
  • Also, can you explain a little more about what is being inferred in your example, and what wasn't inferred in mine? As I understand it, I'm looking for the compiler to infer from the types available in Foo to the type of the Option at the call site. How does changing the parameter list of ifSome provide more information to the compiler about what types are available from Foo?
    – scalapeno
    Sep 28, 2012 at 23:30
  • Actually, I think I'm getting closer to understanding. You're saying that the compiler doesn't check that the type B from Option[B] matches type B from B => _ because they are in the same parameter list, but if you put them in separate lists, then the compiler will check and will use that information to choose the correct function from Foo. But this doesn't seem right either because if I do ifSome(Option(1) -> ((x:String)=> x+"test")) the compiler notices I've got a type mismatch on B. If the compiler can check to notice the mismatch, why can't it choose the correct function?
    – scalapeno
    Sep 28, 2012 at 23:38
  • Yes, that's what I'm saying. I'm not familiar with the internals of the compiler, so I can't explain why it has this limitation or why it can't be removed easily.
    – Kim Stebel
    Sep 28, 2012 at 23:45

Your Answer

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

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