Ruka
(Ruka)
January 21, 2025, 2:08am
1
I was just reading this post Unitize a vector - Grasshopper (edit: and this one Python unitize vector and make it a vector - #12 by ivelin.peychev ) where David Rutten mentioned values types should be immutable, but that RhinoCommon made the decision that Unitize() will mutate a vector3d, and I was wondering what the reason why was?
I’ve seen in other 3d libraries that they will return a new vector i.e. Vector3.Normalize(Vector3) Method (System.Numerics) | Microsoft Learn .
Or have I misunderstood how Unitize() works?
I don’t believe any of the common RhinoCommon structs are immutable, e.g Point3d, Point2d, Transform, Vector3d.
As for why? Likely as RhinoCommon wraps the Open Nurbs types which also does not use immutability (or even structs) for these basic types.
It’s good for value types to be immutable, but not always practical.
1 Like
Ruka
(Ruka)
January 22, 2025, 12:43am
3
Thank for the explanation, that makes sense – follow up question though, is there a reason why you can do vector.Unitize()
but you can’t do vector.Negate()
(i.e. negate requires you to use Vector2d.Negate(vector)
and will return a new vector?)
Edit: I can see Vector3d has vector.Reverse()
but Vector2d does not?
You can do -vector
for Vector3d
or Vector2d
which would give you this negative result. As for why there isn’t a method available, I’m unsure.