如何调用函数 which return 部分函数重载的函数
How to call function overloaded by function which return partail function
如何使用以下 f
函数之一打印 "I'm not partial",为什么下面的代码打印 "I'm partial"?也许有一些与函数相关的通用规则(可能与 arity 0)在 scala 中产生部分函数和名称重载?
object T1 extends App{
case class C()
def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") }
def f(c: C): Unit = { println("I'm not partial") }
(f:C ⇒ Unit)(C()) // Why this is printing "I'm partial"?
}
表达式不是应用程序。比较 x 的定义和模棱两可的调用:
scala> :pa
// Entering paste mode (ctrl-D to finish)
object T1 extends App{
case class C()
def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") }
def f(c: C): Unit = { println("I'm not partial") }
(f:C ⇒ Unit)(C()) // Why this is printing "I'm partial"?
}
// Exiting paste mode, now interpreting.
defined object T1
scala> T1 main null
I'm partial
scala> import T1._
import T1._
scala> val x: C => Unit = f
x: T1.C => Unit = <function1>
scala> f(C())
<console>:12: error: ambiguous reference to overloaded definition,
both method f in object T1 of type (c: T1.C)Unit
and method f in object T1 of type => PartialFunction[T1.C,Unit]
match argument types (T1.C)
f(C())
^
scala> x(C())
I'm partial
The spec 说 f
和 f(...)
(第 3 条)一样具体,但不是相反(第 1 条)。
应用程序的括号意味着您正在比较两个看起来像 f(...)
.
的东西
如果这有帮助,通常的优先级排序方式:
scala> trait Helper { def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") } }
defined trait Helper
scala> :pa
// Entering paste mode (ctrl-D to finish)
object T extends Helper {
def f(c: C): Unit = { println("I'm not partial") }
}
// Exiting paste mode, now interpreting.
defined object T
scala> T f new C
I'm not partial
还有其他 API 您可以编写但不能以正常方式调用:
scala> trait X { def f(i: => Int) = i }
defined trait X
scala> trait Y { def f(i: Int) = 2 * i }
defined trait Y
scala> class Z extends X with Y
defined class Z
scala> new Z().f(42)
res5: Int = 84
scala> typeOf[Z].members filter (_.name.toString == "f")
res7: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(method f, method f)
scala> (new Z(): X).f(42)
res8: Int = 42
或者,无法在此处调用其他函数:
scala> class YY extends Y { def f(i: => Int) = i }
defined class YY
scala> new YY().f(42)
<console>:20: error: ambiguous reference to overloaded definition,
both method f in class YY of type (i: => Int)Int
and method f in trait Y of type (i: Int)Int
match argument types (Int)
new YY().f(42)
^
scala> (new YY(): Y).f(42)
res15: Int = 84
如何使用以下 f
函数之一打印 "I'm not partial",为什么下面的代码打印 "I'm partial"?也许有一些与函数相关的通用规则(可能与 arity 0)在 scala 中产生部分函数和名称重载?
object T1 extends App{
case class C()
def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") }
def f(c: C): Unit = { println("I'm not partial") }
(f:C ⇒ Unit)(C()) // Why this is printing "I'm partial"?
}
表达式不是应用程序。比较 x 的定义和模棱两可的调用:
scala> :pa
// Entering paste mode (ctrl-D to finish)
object T1 extends App{
case class C()
def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") }
def f(c: C): Unit = { println("I'm not partial") }
(f:C ⇒ Unit)(C()) // Why this is printing "I'm partial"?
}
// Exiting paste mode, now interpreting.
defined object T1
scala> T1 main null
I'm partial
scala> import T1._
import T1._
scala> val x: C => Unit = f
x: T1.C => Unit = <function1>
scala> f(C())
<console>:12: error: ambiguous reference to overloaded definition,
both method f in object T1 of type (c: T1.C)Unit
and method f in object T1 of type => PartialFunction[T1.C,Unit]
match argument types (T1.C)
f(C())
^
scala> x(C())
I'm partial
The spec 说 f
和 f(...)
(第 3 条)一样具体,但不是相反(第 1 条)。
应用程序的括号意味着您正在比较两个看起来像 f(...)
.
如果这有帮助,通常的优先级排序方式:
scala> trait Helper { def f: PartialFunction[C, Unit] = { case c:C ⇒ println( "I'm partial") } }
defined trait Helper
scala> :pa
// Entering paste mode (ctrl-D to finish)
object T extends Helper {
def f(c: C): Unit = { println("I'm not partial") }
}
// Exiting paste mode, now interpreting.
defined object T
scala> T f new C
I'm not partial
还有其他 API 您可以编写但不能以正常方式调用:
scala> trait X { def f(i: => Int) = i }
defined trait X
scala> trait Y { def f(i: Int) = 2 * i }
defined trait Y
scala> class Z extends X with Y
defined class Z
scala> new Z().f(42)
res5: Int = 84
scala> typeOf[Z].members filter (_.name.toString == "f")
res7: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(method f, method f)
scala> (new Z(): X).f(42)
res8: Int = 42
或者,无法在此处调用其他函数:
scala> class YY extends Y { def f(i: => Int) = i }
defined class YY
scala> new YY().f(42)
<console>:20: error: ambiguous reference to overloaded definition,
both method f in class YY of type (i: => Int)Int
and method f in trait Y of type (i: Int)Int
match argument types (Int)
new YY().f(42)
^
scala> (new YY(): Y).f(42)
res15: Int = 84