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

[geometry-1] DomMatrixReadOnly is missing preMultiply() method (better mutable vs immutable patterns?) #587

Open
trusktr opened this issue Dec 28, 2024 · 1 comment

Comments

@trusktr
Copy link

trusktr commented Dec 28, 2024

DOMMatrix has mutating multiplySelf() and preMultiplySelf(), while DOMMatrixReadOnly has only the non-mutating multiply() but not preMultiply().

It would be nice to add preMultiply for consistency. Similar for other *ReadOnly geometry classes if needed.

As an alternative, instead of adding more methods to *ReadOnly as it has elsewhere been discussed that the *ReadOnly pattern is a direction we wish we didn't take (that's why there's DOMQuad but not DOMQuadReadOnly), we should consider adding a new pattern of methods for working with immutability vs not.

I propose adding clone() and copy() methods, and then people can pursue immutability like so:

const mat1 = new DOMMatrix(...)
const mat2 = new DOMMatrix(...)

const mat3 = mat1.clone().preMultiplySelf(mat2)

or

const mat1 = new DOMMatrix(...)
const mat2 = new DOMMatrix(...)

const mat3 = new DOMMatrix()
mat3.copy(mat1).preMultiplySelf(mat2)

As a real-world example, pretty much all Three.js classes have clone() and copy() methods. Many of their methods also accept targets, for example obj.doSomething() (writes to self) vs obj.doSomething(other) writes to other instead of self.

I believe that the approach Three.js takes is nice because it is more concise (methods that can be mutable or not mutable, insead of double the amount of methods for mutable vs not).

@trusktr trusktr changed the title [geometry-1] DomMatrixReadOnly is missing preMultiply() method (and the idea of better mutable vs immutable patterns) [geometry-1] DomMatrixReadOnly is missing preMultiply() method (better mutable vs immutable patterns?) Dec 28, 2024
@trusktr
Copy link
Author

trusktr commented Dec 28, 2024

There'd be something nice about the non-mutable (non-*Self) methods, in that returning a *ReadOnly interfaces allows immutability to be enforced when the performance tradeoff is acceptable.

With methods like clone() and copy(), and methods that create new instances or write to targets, immutable behavior can be applied, but not strictly enforced. You could return mat.clone().rotateSelf(10) and someone might think that modifying the returned value will do something when in fact it does not.

For people who choose to have immutable designs, the non-*Self methods would seem to be desirable if they were to return *ReadOnly interfaces, but right now clone() and copy() patterns would be nicer if the non-*Self methods continue to return mutable interfaces.

We should at least go one direction or the other, rather than stay stuck in the middle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant