In scala, there isn't something like operators. You have objects and these ones have methods. In the oficial docs of scala where it talk about operators says
In Scala, operators are methods. Any method with a single parameter can be used as an infix operator. For example, + can be called with dot-notation:
Methods can be named with any name like the followings (of course there are naming conventions)
object DifferentNamesForMethods {
def +(i: Int) = ???
def $(i: Double) = ???
def `class`(a: String, b: Boolean = ??? // you can use reserved words but you need to put them between backtics
def `a name with spaces`(x: List[Int) = ???
}
When you are doing:
val a = b + c
what you are doing is
val a = b.+(c)
which means, you are invoking the method of an object that is named +
(same for *
, -
, /
).
Backing to your question, based on your example
@main def foo() :Unit = {
var i = 0
while i < 10 do {
println(s"$i -> ${i^2}") // here is where the `^`is being used
i += 1
}
}
Knowing that the variable i
is of type Int
, you are calling the method named ^ from the abstract final classInt extends AnyVal. As you can see in the description of the method: Returns the bitwise XOR of this value and x.
, which means you are doing an XOR operation at bit level between the instance of the object Int and the parameter received in the ˆ
method. This will be translated as
println(s"$i -> ${i.^(2)}")
That's why you are getting those outputs. If you are using an IDE, it should show you the scaladoc of the method or even you would be able to browse the code.
offtopic
the piece of code you shared, can be rewritten in a more functional way without using variables that can be mutated
@main def foo() :Unit = {
(1 until 10).foreach(i => println(s"$i -> ${i^2}"))
}