mathnet / mathnet-spatial

Math.NET Spatial
http://spatial.mathdotnet.com
MIT License
376 stars 132 forks source link

Why let the X, Y, Z readonly? #234

Closed zone-1614 closed 1 year ago

zone-1614 commented 1 year ago

Why not provide some ways to change the X, Y, Z of Point3D or Vector3D? Are there any considerations?

ugumba commented 1 year ago

These are structs, which are passed around as tuple value type instances, instead of references to class type instances. It is recommended practice to make such structs immutable (readonly) - making them mutable is usually only confusing, as any mutation is ephemeral and invisible anywhere but the current instance and stack frame.

Embrace this pattern - it's good for performance and multithreading! Create new instances instead. This is precisely what the record type and its "with" syntax was made for.

zone-1614 commented 1 year ago

Thank you. I'm not familiar with c# and I didn't know the "with" syntax before.