How to create infix operators in Scala

844 views
Skip to first unread message

Iftikhar Khan

unread,
May 27, 2014, 7:08:03 AM5/27/14
to scala...@googlegroups.com
Hi,

I'm new to Scala. I come from a Haskell background and I am trying to translate some of my Haskell code into Scala.

I am having difficulty with creating infix operators.

In Haskell say I have this infix operator defined as:

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'

So now if I have two booleans, p and q where p == True and q == False, p <=> q will return False.

My question is how do I go about translating this into Scala. I had a look at the Rational class defined in Odersky's Programming in Scala book and
tried to follow the example. This is as far as I got:

class Iff (b : Boolean){

  def <=> (that : Boolean) : Boolean = {
    this.b == that
  }
}

val a = new Iff(true)
println(a.<=>(false))  // returns false as expected

I've probably not done this in idiomatic Scala so I am looking for help in that department.

My questions are:
  1. Have I implemented this idiomatically in Scala? If not, what is that best way to this in Scala?
  2. Did I have to create that class in order to define this operator? Meaning, can I define a standalone method in Scala like I have in the Haskell code above?
  3. How to specify the fixity level of the operator in Scala? That is, it's precedence level.
Thanks
Iftikhar

Simon Ochsenreither

unread,
May 27, 2014, 9:19:19 AM5/27/14
to scala...@googlegroups.com
Hi Iftikhar,

  1. Have I implemented this idiomatically in Scala? If not, what is that best way to this in Scala?
outside a very limited area in which symbols are either understood globally (like +, -, /, *, ...) or represent a common sub-language (like ! for actors), symbolic method names are pretty much never idiomatic Scala.
  1. Did I have to create that class in order to define this operator? Meaning, can I define a standalone method in Scala like I have in the Haskell code above?
You could also use a method to define an implicit conversion with an anonymous class definition within, but due to JVM issues that would mean more overhead when using that method.
  1. How to specify the fixity level of the operator in Scala? That is, it's precedence level.
You can't. Precedence is determined by the first symbol of your method.

Bye,

Simon
 

Harshad RJ

unread,
May 27, 2014, 11:48:53 AM5/27/14
to scala...@googlegroups.com


On Tuesday, 27 May 2014 18:49:19 UTC+5:30, Simon Ochsenreither wrote:
  1. Did I have to create that class in order to define this operator? Meaning, can I define a standalone method in Scala like I have in the Haskell code above?
You could also use a method to define an implicit conversion with an anonymous class definition within, but due to JVM issues that would mean more overhead when using that method.

Wouldn't using a value class solve the problem of overheads?
http://docs.scala-lang.org/overviews/core/value-classes.html

Iftikhar Khan

unread,
May 28, 2014, 7:07:15 AM5/28/14
to scala...@googlegroups.com
After some help on StackOverflow as well, this is the solution I have come up with:

object BooleanHelper {

  implicit class BooleanHelpers(val b: Boolean) extends AnyVal {

    // implication
    def ==>(that: Boolean): Boolean = !b || that

    // equivalence

    def <=>(that: Boolean): Boolean = this.b == that

    // exclusive disjunction
    def <+>(that: Boolean): Boolean = this.b != that
Reply all
Reply to author
Forward
0 new messages