immutability - Does Scala have record update syntax for making modified clones of immutable data structures? -



immutability - Does Scala have record update syntax for making modified clones of immutable data structures? -

in mercury can use:

a = b^some_field := somevalue

to bind a re-create of b, except some_field somevalue instead of whatever in b. believe haskell equivalent like:

a = b { some_field = some_value }

does scala have "modifying" immutable values. alternative seems to have constructor straight sets every field in instance, isn't ideal (if there invarients constructor should maintaining). plus clunky , much more fragile if had explicitly pass every other value in instance want have modified re-create of.

i couldn't find googling, or in brief survey of language reference manual or "scala example" (which have read start-to-finish, haven't absorbed of yet, may in there).

i can see feature have weird interactions java-style access protection , subclasses though...

you can utilize case classes this, don't have to. case classes nil magical - modifier case saves lot of typing. re-create method realized utilize of named , default parameters. names same fields , defaults current values of fields. here's example:

class classwithcopy(val field1:string, val field2:int) { def copy(field1:string = this.field1, field2:int = this.field2) = { new classwithcopy(field1,field2); } }

you can utilize re-create method on case classes. named , default parameters useful feature, , not re-create methods.

scala immutability

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -