- Learning Scala Programming
- Vikash Sharma
- 692字
- 2025-04-04 17:32:11
Operators in Scala
Based on the way we use them, the Scala operators can be classified into three types:
- Infix operators
- Prefix operators
- Postfix operators
We use operators to perform some operation on operands, which is obvious, and the way we implement makes them infix, prefix, or postfix. A basic example of an infix operator is addition +:
scala> val x = 1 + 10
x: Int = 11
We have two operands (1 and 10) on which this addition operation is being performed. We've already discussed that operators are methods. It means that somehow the operation is being performed as 1.+(10), and 1 + 10 is just syntactic sugar of how we can write this. This is possible because the method + is defined for the given types. Here, in our case, the addition (+) method is defined for Int. Along with this, there are several versions of overloaded methods that support other numeric value types as well. It means that we can pass in any other type and it'll be a normal addition operation performed, given that the overloaded version of that method is present:
scala> val y = 1 + 'a'
y: Int = 98
Here, the method def+(arg: Char): Int is invoked and has given an Int as a result. Think about it, if these methods are not native Scala operators and are methods, then we can also create methods like these that work as operators. This makes you feel powerful. Let's try this:
class Amount(val amt: Double) {
def taxApplied(tax: Double) = this.amt * tax/100 + this.amt
}
object Order extends App {
val tax = 10
val firstOrderAmount = 130
def amountAfterTax(amount: Amount) = amount taxApplied tax
println(s"Total Amount for order:: ${amountAfterTax(new Amount(firstOrderAmount))}")
}
The following is the result:
Total Amount for order:: 143.0
Great! taxApplied is the first operator that we defined, it is defined for the type Amount. Our program has a class Amount that is just a Double value, and defines a method taxApplied. This method expects a double value for tax to be applied on this which is going to be the current value for the amount. Operators are a way we can use methods, which is why we have this operator. We've used it in object, Order while defining a function, amountAfterTax:
> amount taxApplied tax
It can also be written as amount.taxApplied(tax). There are also a few examples in Scala; for example, the indexOf operator that works on String:
scala> val firstString = "I am a String"
firstString: String = I am a String
scala> firstString indexOf 'a'
res1: Int = 2
We've talked about infix operators, where the operator sits between two operands. Now let's take a look at another way of using operators, that is, prefix and postfix. The first one, prefix operators, sits before an operand. Examples of these are -, !, and so on:
scala> def truthTeller(lie: Boolean) = !lie
truthTeller: (lie: Boolean)Boolean
scala> truthTeller(false)
res2: Boolean = true
Here, !lie, uses the prefix operator !, and this is the way we put an operand to the right of our operator. But this is getting invoked as a method. What happens in the background is that Scala uses unary_ to call these operators, and that's obvious because these operators use only one operand. So our implementation looks something like the following:
scala> def truthTeller(lie: Boolean) = lie.unary_!
truthTeller: (lie: Boolean)Boolean
The operator ! is defined for Boolean types, hence we were allowed to make calls on Boolean. The other way is where the operand sits on the left side, called postfix operators. Examples of these operators are convertors such as toLowerCase, toInt, toString, and so on:
scala> 1.toString
res4: String = 1
scala> "1".toInt
res5: Int = 1
scala> "ABC".toLowerCase
res7: String = abc
It means that these operators are defined as methods in the corresponding types. This is one way to classify operators in Scala. Now we'll go and have a quick look at types of operators based on the context they are used in programming languages. These are basically classified as:
- Arithmetic operators
- Relational operators
- Logical operators
- Bitwise operators