You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not only is this awkward, but it wastefully creates 4 matrices and an unnecessary typed array, instead of only 3 matrices, because mat1.multiply(mat2) returns a DOMMatrix, so we need to call toFloat64Array to pass the array to DOMMatrixReadOnly to create the immutable isntance.
Furthermore, the way the APIs currently work prevents people from using the APIs for subclasses. 🥲
classMyMatrixextendsDOMMatrix{specialNewMethod(){constnewMat=this.rotateAxisAngle(...)// do something with newMatreturnnewMat}}
Now the user's new specialNewMethod will erroneously return a DOMMatrix instance instead of a MyMatrix instance, forcing the user to make the code more expensive again like this:
classMyMatrixextendsDOMMatrix{specialNewMethod(){constnewMat=this.rotateAxisAngle(...)// do something with newMatreturnnewthis.constructor(newMat.toFloat64Array())}}
If the browser's implementation of multiply (and other immutable methods) were to use the equivalent of new this.constructor(), making subclasses would be simpler, and immutability would also be preserved as expected.
For example,
DOMMatrixReadOnly.multiply
should returnDOMMatrixReadOnly
instead ofDOMMatrix
.This makes the API awkward to use when choosing immutability:
Not only is this awkward, but it wastefully creates 4 matrices and an unnecessary typed array, instead of only 3 matrices, because
mat1.multiply(mat2)
returns aDOMMatrix
, so we need to calltoFloat64Array
to pass the array toDOMMatrixReadOnly
to create the immutable isntance.Furthermore, the way the APIs currently work prevents people from using the APIs for subclasses. 🥲
Now the user's new
specialNewMethod
will erroneously return aDOMMatrix
instance instead of aMyMatrix
instance, forcing the user to make the code more expensive again like this:Instead, the ideal API would be like this:
If the browser's implementation of
multiply
(and other immutable methods) were to use the equivalent ofnew this.constructor()
, making subclasses would be simpler, and immutability would also be preserved as expected.Related:
The text was updated successfully, but these errors were encountered: