官术网_书友最值得收藏!

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
主站蜘蛛池模板: 鹤山市| 尉氏县| 隆子县| 容城县| 和平区| 察哈| 宁强县| 乐都县| 萨嘎县| 鹤山市| 武平县| 东阳市| 孟村| 临湘市| 凤山市| 泰和县| 巴彦淖尔市| 东港市| 奈曼旗| 昌宁县| 凤阳县| 工布江达县| 榕江县| 新巴尔虎右旗| 精河县| 盘山县| 仁化县| 新和县| 虎林市| 万盛区| 镇远县| 岳普湖县| 延寿县| 桃源县| 巴中市| 华蓥市| 乌什县| 永善县| 美姑县| 景洪市| 海晏县|