在Scala中创建中缀运算符
发布时间:2020-12-16 09:17:27 所属栏目:安全 来源:网络整理
导读:我正在尝试将我的一些 Haskell代码翻译成 Scala,而且我在创建中缀操作系统时遇到困难. 在Haskell说我有这个中缀运算符定义为: infix 1 = // this specifies the operator precedence(=) :: Bool - Bool - Bool // this is the type signature of this opera
我正在尝试将我的一些
Haskell代码翻译成
Scala,而且我在创建中缀操作系统时遇到困难.
在Haskell说我有这个中缀运算符定义为: infix 1 <=> // this specifies the operator precedence (<=>) :: Bool -> Bool -> Bool // this is the type signature of this operator (it says,it takes two Boolean values and returns a Boolean value) x <=> y = x == y // this is the definition of the operator,it is mimicking the behaviour of the logical implication 'if-and-only-if' 所以现在如果我有两个布尔,p和q,其中p == True和q == False,p = q将返回False. 我的问题是我如何去将这个翻译成Scala.我看过在Scala书中的Odersky编程中定义的Rational类 class Iff (b : Boolean){ def <=> (that : Boolean) : Boolean = { this.b == that } } val a = new Iff(true) println(a.<=>(false)) // returns false as expected 我可能没有在惯用的Scala中做到这一点,所以我正在寻找该部门的帮助. 我的问题是: >我是否在Scala中成语地实现了这一点?如果没有,在Scala中最好的办法是什么? 解决方法
您可以定义隐式类
implicit class Iff(val b: Boolean) extends AnyVal { def <=>(that: Boolean) = this.b == that } 现在你可以调用它而不使用新的: true <=> false // false false <=> true // false true <=> true // true (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- POJO AXIS2 发布WEBSERVICE的讨论
- angular – 如何在延迟加载的模块中提供服务,并将该服务限定
- 对术语SSH,Shell,终端,命令提示符和Telnet的困惑
- libraryDependencies on sbt Build.scala Full Configurati
- vim – 为什么没有更多人使用viper模式的emacs?
- scala – 如何通过询问Akka Actor认为结果不可用作同一消息
- 数组 – Scala中的高效二维数组列提取
- 关于您提到使用WebServices时候遇到“基础连接以关闭”的问
- Lazy val在Scala中实现惰性列表
- 命令提示符在ssh连接后显示的时间太长