0

Given that we have two functions in scope with the same name but a different parameter list. How can I distinguish one from the other, such as in the case of trying to access the tupled function

Example:

scala> :paste
// Entering paste mode (ctrl-D to finish)


def f(s: String, l: Long): String = ???
def f(i: Int, l: Long): String = ???

val t:((String, Long)) => String = f.tupled

// Exiting paste mode, now interpreting.

<pastie>:15: error: ambiguous reference to overloaded definition,
both method f of type (i: Int, l: Long)String
and  method f of type (s: String, l: Long)String
match expected type ?
val t:((String, Long)) => String = f.tupled

Simplifying the problem to function literals yields:

scala> :paste
// Entering paste mode (ctrl-D to finish)

def f(i: Int, l: Long): String = ???
def f(s: String, l: Long): String = ???

val g = f _

// Exiting paste mode, now interpreting.

<pastie>:15: error: ambiguous reference to overloaded definition,
both method f of type (s: String, l: Long)String
and  method f of type (i: Int, l: Long)String
match expected type ?
val g = f _
        ^

But the explicit type annotation in the function literal example manages to solve the problem, where as for the apply method above in the context of tupled did not work

scala>

scala> :paste
// Entering paste mode (ctrl-D to finish)

def f(s: String, l: Long): String = ???
def f(i: Int, l: Long): String = ???

val g: (String, Long) => String  = f _

// Exiting paste mode, now interpreting.

f: (s: String, l: Long)String <and> (i: Int, l: Long)String
f: (s: String, l: Long)String <and> (i: Int, l: Long)String
g: (String, Long) => String = $$Lambda$1458/794413935@60cbba57

2 Answers 2

3

Here is the way you can achieve this

val t1 = (f(_: String, _: Long)).tupled
val t2 = (f(_: Int, _: Long)).tupled

output

t1: ((String, Long)) => String = scala.Function2$$Lambda$1188/922196321@3994b698
t2: ((Int, Long)) => String = scala.Function2$$Lambda$1188/922196321@4249db51
Sign up to request clarification or add additional context in comments.

1 Comment

Actually did end up consolidating my last two lines in my answer to come up with what you have done here. Comes out to quite cryptic syntax...
0

I found the following hack for the tupled problem:

scala> :paste
// Entering paste mode (ctrl-D to finish)

def f(s: String, l: Long): String = ???
def f(i: Int, l: Long): String = ???

val g: (String, Long) => String  = f _
val t = g.tupled

// Exiting paste mode, now interpreting.

f: (s: String, l: Long)String <and> (i: Int, l: Long)String
f: (s: String, l: Long)String <and> (i: Int, l: Long)String
g: (String, Long) => String = $$Lambda$1476/1981627424@172f2717
t: ((String, Long)) => String = scala.Function2$$Lambda$259/530042637@bda4cbe

Comments

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.