When i do e.g:
var airlines = Set("Qantas", "JetStar", "Air NZ")
airlines += "Virgin"
airlines is an immutable Set.
+= is not defined on the immutable Set trait.
So is += a built-in operator in scala? I mean how does scala know to reassign airlines with a new set("Qantas", "JetStar", "Air NZ", "Virgin") ?
If an operator ending with
=(e.g.+=) is used but not defined on a class, the Scala compiler will desugar this into e.g.or, for
++=, we’d havedesugared into
Of course, as dmeister notes, this will only compile if that new expression makes sense. For example, if we deal with
vars.See Scala Reference §6.12.4 Assignment Operators
(
<=,>=and!=are excluded as special cases, as are patterns also starting with=.)