The + = operator on an immutable set

When I do, for example:

var airlines = Set("Qantas", "JetStar", "Air NZ")
airlines += "Virgin"

- fixed set.

+= not defined in immutable attribute Set.

So, the +=built-in statement in scala? I mean, how does scala know to reassign airlines to new ones set("Qantas", "JetStar", "Air NZ", "Virgin")?

+5
source share
2 answers

If an operator ending in =(for example +=) is used but not defined in the class, the Scala compiler will empty it, for example,

airlines = airlines + "Virgin"

or, for ++=, wed have

airlines ++= airlines

desugared in

airlines = airlines ++ airlines

Of course, like dmeister notes , this only compiles if this new expression makes sense. For example, if we are dealing with vars.

. Scala Β§6.12.4
(<=, >= != , , =.)

+14

+= , "Virgin", airlines. , , .

, var, val, val.

+3

All Articles