In Scala, I can make a caseclass, case class Foo(x:Int)
, and then put it in a list like so:
List(Foo(42))
Now, nothing strange here. The following is strange to me. The operator ::
is a function on a list, right? With any function with one argument in Scala, I can call it with infix notation. An example is 1 + 2
is a function (+)
on the object Int
. The class Foo
I just defined does not have the ::
operator, so how is the following possible?
Foo(40) :: List(Foo(2))
In Scala 2.8 RC1, I get the following output from the interactive prompt:
scala> case class Foo(x:Int) defined class Foo scala> Foo(40) :: List(Foo(2)) res2: List[Foo] = List(Foo(40), Foo(2))
I can go on and use it, but what is the explanation?
In Scala, all operators are methods. Operators themselves are just syntactic sugar or a shorthand to call methods. Both arithmetic addition (+) and String. charAt() are examples of infix operators.
Remarks. The logical AND operator ( && ) returns true if both operands are true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical AND has left-to-right associativity.
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.
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Scala is rich in built-in operators and provides the following types of operators − This chapter will examine the arithmetic, relational, logical, bitwise, assignment and other operators one by one.
Bitwise Operators. In Scala, there are 7 bitwise operators which work at bit level or used to perform bit by bit operations. Following are the bitwise operators : Bitwise AND (&): Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
This Scala operator tells Scala to perform arithmetic operations on two values. We have five of these. Let’s take two values for exemplary purposes. (We did this in the Command Prompt). i. Addition (+) This adds two operands. See an example. ii. Subtraction (-) This subtracts the second operand from the first. iii. Multiplication (*)
Any method in Scala can be used as an operator. For example, the String. charAt () method, which returns a character at a given index can be invoked using the standard dot notation: It can also be invoked as an operator: Both arithmetic addition ( +) and String.charAt () are examples of infix operators.
From the Spec:
6.12.3 InfixOperations An infix operator can be an arbitrary identifier. Infix operators have precedence and associativity defined as follows.
...
The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left- associative.
You can always see how these rules are applied in Scala by printing the program after it has been through the 'typer' phase of the compiler:
scala -Xprint:typer -e "1 :: Nil" val r: List[Int] = { <synthetic> val x$1: Int = 1; immutable.this.Nil.::[Int](x$1) };
It ends with a :
. And that is the sign, that this function is defined in the class to the right (in List
class here).
So, it's List(Foo(2)).::(Foo(40))
, not Foo(40).::(List(Foo(2)))
in your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With