-
Notifications
You must be signed in to change notification settings - Fork 31
Description
I'd really like to see immutable version of existing vec/mat/quat/array types. Personally, I have 2 use cases for such types:
Immutable objects that contain glm objects
I have objects which are immutable which have pyglm object as attributes. Since pyglm objects are mutable, whenever a user wants to access one of these attributes it requires making a copy of the pyglm object so that the parent object remains publicly immutable. For example:
class MyImmutableObject:
def __init__(self, point: vec3):
self._point = point
@property
def point(self) -> vec3:
return vec3(self._point)This isn't the worst thing ever, but I also have cases where I'm doing this with rather large arrays, which is pretty wasteful.
Hashing
I've encountered situations where I'd like to use pyglm objects as dictionary keys and other situations where I've wanted to eliminate duplicates. Since pyglm objects are mutable they can't be hashed, so I've had to resort to converting them back and forth from tuples in many cases.
unique_points = array(vec3(p) for p in {tuple(p) for p in non_unique_points})Immutable versions of pyglm object could be hashable and eliminate these needless conversions.
Implementation
There is some precedent for this already with array having readonly. As far as I know it isn't really possible to construct a read only array through the python API though. If all objects had a readonly attribute that could be set on creation with a keyword only argument that may work?
point = vec3(0, readonly=True)Personally, I'd rather see a whole extra type to avoid the overhead from readonly checks. Something like:
point = const_vec3(0)I think math operations would continue to return the normal objects (so adding two readonly vec3 would result in a regular vec3) to keep things simple.
Thoughts? I'm happy to work on this if it sounds reasonable.