假设在作用域中有两个函数具有相同的名称,但参数列表不同。如何区分两者,例如在尝试访问
tupled
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
将问题简化为函数文字会产生:
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 _
^
apply
方法
不起作用
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