When this code is executed:
var a = 24
var b = Array (1, 2, 3)
a = 42
b = Array (3, 4, 5)
b (1) = 42
I see three (five?) assignments here. What is the name of the method call that is called in such circumstances? Is it operator overloading?
Update:
Can I create a class and overload assignment? ( x = y not x(1) = y )
Having this file:
//assignmethod.scala
object Main {
def main(args: Array[String]) {
var a = 24
var b = Array (1, 2, 3)
a = 42
b = Array (3, 4, 5)
b (1) = 42
}
}
running scalac -print assignmethod.scala
gives us:
[[syntax trees at end of cleanup]]// Scala source: assignmethod.scala
package <empty> {
final class Main extends java.lang.Object with ScalaObject {
def main(args: Array[java.lang.String]): Unit = {
var a: Int = 24;
var b: Array[Int] = scala.Array.apply(1, scala.this.Predef.wrapIntArray(Array[Int]{2, 3}));
a = 42;
b = scala.Array.apply(3, scala.this.Predef.wrapIntArray(Array[Int]{4, 5}));
b.update(1, 42)
};
def this(): object Main = {
Main.super.this();
()
}
}
}
As you can see the compiler just changes the last one (b (1) = 42
) to the method call:
b.update(1, 42)
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