1

Trying to overload a method with multiple parameter groups, where the 2nd parameter type differs doesn't seem to work. This will not compile:

class Foo {
  def boo(a: String)(b: String): Unit = ()
  def boo(a: String)(b: Int): Unit = boo(a)(b.toString)
}

I would have thought that it would be compiled into boo(String, String) and boo(String,Int) methods and thus be ok in the JVM. But I guess not.

What I'm looking for is a workaround - I want to keep the parameter groups and the overloaded name so its transparent to the caller, but any other hacks are welcome.

2 Answers 2

2

You could have an intermediary layer that has an overloaded apply method:

object Foo {
    def boo(a: String) = BooMaker(a)
    case class BooMaker(a: String) {
        def apply(b: String): Unit = ()
        def apply(b: Int): Unit = apply(b.toString)
    }
}

This will allow you to call Foo.boo("a")("b") or Foo.boo("a")(1), but introduces the potentially undesired intermediary tier.

Sign up to request clarification or add additional context in comments.

Comments

1

Does it need to be curried? It won't compile because of the partial application. The following should compile just fine.

class Foo {
  def boo(a: String, b: String): Unit = ()
  def boo(a: String, b: Int): Unit = boo(a, b.toString)
}

If you can give more context to your actual use case, it might help with giving a better answer.

1 Comment

It needs to be curried because I want the 2nd parameter group for block style invocation. It's for a DSL.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.