bda*_*ics 1 scala operator-overloading anonymous-function
尝试运行此代码:
def ! : Int => Boolean = (p : Int => Boolean) => !p
有编译错误:
[error] value unary_! is not a member of Int => Boolean
[error] def ! : Int => Boolean = (p : Int => Boolean) => !p
Run Code Online (Sandbox Code Playgroud)
错误突出显示为“!p”
编译器不应该自动计算出 p 的结果是 aBoolean吗?
提前致谢
编辑:根据评论,也尝试了以下内容。使用其他方法完成了我的任务,但我正在尝试学习如何定义一元运算符
def unary_! : Int => Boolean = (p : Int => Boolean) => !(p(_))
仍然收到编译器错误 "!(p(_))"
也许您打算这样做:
scala> class C(p: Int => Boolean) { def unary_! : Int => Boolean = !p(_) }
defined class C
scala> val c = new C(i => i < 0)
c: C = C@4d9cad9d
scala> (!c)(42)
res0: Boolean = true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2875 次 |
| 最近记录: |