Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating infix operators in Scala

Tags:

haskell

scala

I am trying to translate some of my Haskell code into Scala and 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.
like image 239
M.K. Avatar asked May 27 '14 13:05

M.K.


People also ask

What is Scala infix?

For a long time, Scala has supported a useful “trick” called infix operator notation. If a method takes a single argument, you can call it without the period after the instance and without parentheses around the argument.

What is :: operator in Scala?

The ::() operator in Scala is utilized to add an element to the beginning of the List. Method Definition: def ::(x: A): List[A] Return Type: It returns the stated list after adding an element to the beginning of it.

What is operator in infix?

Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.

What is operator overloading in Scala?

Operator Overloading in Scala Scala supports operator overloading, which means that the meaning of operators (such as * and + ) may be defined for arbitrary types. Example: a complex number class: class Complex(val real : Double, val imag : Double) { def +(other : Complex) = new Complex( real + other.


1 Answers

You can define implicit class

implicit class Iff(val b: Boolean) extends AnyVal {
  def <=>(that: Boolean) = this.b == that
}

and now you can call it without using new :

true <=> false // false
false <=> true // false
true <=> true  // true
like image 52
goral Avatar answered Oct 08 '22 13:10

goral