Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why did we choose to make Vec2 and Vec4 namedtuples? #234

Open
pvallet opened this issue Jun 25, 2021 · 4 comments
Open

Why did we choose to make Vec2 and Vec4 namedtuples? #234

pvallet opened this issue Jun 25, 2021 · 4 comments
Labels
todo Task that needs to be done

Comments

@pvallet
Copy link

pvallet commented Jun 25, 2021

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!

@johanfforsberg
Copy link

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)

@KinoxKlark KinoxKlark added the todo Task that needs to be done label Oct 21, 2021
@ReblochonMasque
Copy link

ReblochonMasque commented Jun 7, 2022

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?

@KinoxKlark
Copy link
Member

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.

@ReblochonMasque
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
todo Task that needs to be done
Projects
None yet
Development

No branches or pull requests

4 participants