I am new to Scala. I was trying to create functions like +
, *
in one of the class.
Below is the class and its definition -
case class StringWrapper2(str : String){
def +(x:Int) : String ={
println(s"x: $x, str :$str")
str.concat(x.toString);
}
def *(x:Int) : String = {
println(s"x: $x, str :$str")
str.repeat(x);
}
}
I created instance and called above function -
val sw: StringWrapper2 = new StringWrapper2("ABC")
val result = sw + 10 * 2
I am expecting the result - ABC10ABC10 But I am getting result as ABC20
Also the println
statements that I added in method are not printed.
It means these symbolic methods are not getting called and some different implementation (probably String class) is getting called.
Any idea why this might be happening and how to resolve this issue?