Why make structs immutable




















Does the type logically represent something that is an "eternal" value? The number 12 is the number 12; it doesn't change. Integers should be immutable. The point 10, 30 is the point 10, 30 ; it doesn't change.

Points should be immutable. The string "abc" is the string "abc"; it doesn't change. Strings should be immutable. The list 10, 20, 30 doesn't change. And so on. Sometimes the type represents things that do change.

Mary Smith's last name is Smith, but tomorrow she might be Mary Jones. Or Miss Smith today might be Doctor Smith tomorrow. The alien has fifty health points now but has ten after being hit by the laser beam.

Some things are best represented as mutations. Not as such. As I mentioned before though, one of the nice things about immutable values is that something you can re-use parts of them without making copies. So in that sense, memory allocation can be very different. So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.

If your struct is immutable then all automatic copies resulting from being passed by value will be the same. If you want to change it you have to consciously do it by creating a new instance of the struct with the modified data.

If you are passing your structures around and assigning them in some way to variables or pass-by-value parameters no ref or out keywords then a copy is being used.

Of course, this means that any changes made to the copy are not reflected in the original structure, but it's something to be aware of when passing them around.

If you are accessing the structure directly in a manner that doesn't involve copy-by-value semantics e. Immutability is a popular term to qualify an object as totally constant right after its creation. Simple immutable variables aren't so interesting excluding may be the immutable strings but structs and objects are.

In some languages all the fields of an object are immutable by default f. Rust , in other we can declare them as immutable. Note the constructor Immutable — it is the single place where we can change immutable fields in C. Instead of using a Hash or OpenStruct , you can create a bit more clarity around your types by using ImmutableStruct , which is almost as convienient. You can coerce values into struct types by using the from method. This is similar to Ruby's conversion functions, e.

You can treat the interior of the block as a normal class definition with the exception of setting constants. Skip to content. Star Immutable properties make an object closed for modification. This is a separate matter from making it closed for extension. But we can do that too:. Many other languages have a formal data structure that has one or more named properties that are open for modification, but closed for extension.

Structs and Immutable Structs are a handy way to prevent inadvertent errors and to explicitly communicate that an object is intended to be used as a struct and not as a dictionary.

So if we write:. This works for things that look like cheques, and for things that look like money orders: 2. The general idea here is that as long as we pass deposit an instrument that has dollars and cents properties, the function will work. Every single entity that has dollars and cents has the imaginary type hasDollarsAndCents , and every single function that takes a parameter and uses only its dollars and cents properties is a function that requires a parameter of type hasDollarsAndCents.

They exist logically in the running system, but not manifestly in the code we write.



0コメント

  • 1000 / 1000