pyimgui icon indicating copy to clipboard operation
pyimgui copied to clipboard

Why did we choose to make Vec2 and Vec4 namedtuples?

Open pvallet opened this issue 3 years ago • 4 comments

Hello! Using namedtuples make those vectors immutable, which makes them way less convenient to use.

    vec2.x = 2.0
AttributeError: can't set attribute

It seems like there are mutable alternatives where you can set the components of the vector directly, were those too difficult to implement? Or maybe it was a conscious choice because of limitations (sounds weird since the C++ version does allow it)

https://stackoverflow.com/questions/2970608/what-are-named-tuples-in-python

Cheers!

pvallet avatar Jun 25 '21 05:06 pvallet

Maybe this doesn't address your problem, but "modifying" a namedtuple doesn't have to be too bad:

new_vec2 = vec2._replace(x=2.0)

johanfforsberg avatar Jul 06 '21 17:07 johanfforsberg

In core.pyx, Vec2 and Vec4 are mostly used as tuples of 2/4 values to represent width/height, window padding, item spacing, RGBA values, etc. The naming is indeed a little unfortunate in a sense that it suggests they are vectors when they are not "proper vectors"; however tuple or namedtuple seem appropriate for their use case.

What task does the todo tag refer to?

ReblochonMasque avatar Jun 07 '22 01:06 ReblochonMasque

It is an old topic but I think the todo tag was for testing how we could change this to be mutable. Indeed it makes sense in the context those are used but I can see why someone would want to construct its Vec2 or Vec4 in multiple steps or edit a component of its vector before sending it to a function. Especially since they could be accessible as editable attributes of structs like IO or as return values from some functions such as GetClipRectMin().

We need to cast whatever type they are defined in Python as a ImVec2 anyway before forwarding them.

KinoxKlark avatar Jun 07 '22 06:06 KinoxKlark

Thank you!

ReblochonMasque avatar Jun 07 '22 12:06 ReblochonMasque