+ = / * = / etc on AnyVal types (Int, Double, etc.) in Scala

Where exactly are the * = / + = / etc methods for subclasses of AnyVal? I suppose something special has been done for these types, because they are not valid as a value, but like var they are beautiful. Is this even more syntactic sugar? I guess he turns

a *= 5

in

a = a * 5

which is obviously not true for val. Is this intuition right? I also assume that these are just attempts for AnyVals?

Thanks:)

+3
source share
2 answers

. AnyVal, , a OP= b a = a OP b. , OP, - .

Scala Reference ( Scala), 6.12.4.

+9

Foo:

class Foo (val v: String, val n: String) {
  override def toString : String = v + " : " + n
  def #: (i: Int) : Foo = {
    new Foo (v.substring (i), n.substring (i)) }}

var foo = new Foo ("Martin", "Odersky") 

scala> foo #:= 2
scala> foo
res30: Foo = rtin : ersky

scala> foo #:= 2
scala> foo      
res32: Foo = in : sky

scala> foo #:= 2
scala> foo      
res34: Foo =  : y
0

Source: https://habr.com/ru/post/1722648/


All Articles