- From: Joe Pea via GitHub <sysbot+gh@w3.org>
- Date: Sat, 28 Dec 2024 02:26:14 +0000
- To: public-fxtf-archive@w3.org
trusktr has just created a new issue for https://github.com/w3c/fxtf-drafts:
== [geometry-1] Methods of immutable classes should return immutable instances. ==
For example, `DOMMatrixReadOnly.multiply` should return `DOMMatrixReadOnly` instead of `DOMMatrix`.
This makes the API awkward to use when choosing immutability:
```js
const mat1 = new DOMMatrixReadOnly(...)
const mat2 = new DOMMatrixReadOnly(...)
const mat3 = new DOMMatrixReadOnly(mat1.multiply(mat2).toFloat64Array())
```
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. 🥲
```js
class MyMatrix extends DOMMatrix {
specialNewMethod() {
const newMat = this.rotateAxisAngle(...)
// do something with newMat
return newMat
}
}
```
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:
```js
class MyMatrix extends DOMMatrix {
specialNewMethod() {
const newMat = this.rotateAxisAngle(...)
// do something with newMat
return new this.constructor(newMat.toFloat64Array())
}
}
```
Instead, the ideal API would be like this:
```js
const mat1 = new DOMMatrixReadOnly(...)
const mat2 = new DOMMatrixReadOnly(...)
const mat3 = mat1.multiply(mat2)
```
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.
Related:
- https://github.com/w3c/fxtf-drafts/issues/275
Please view or discuss this issue at https://github.com/w3c/fxtf-drafts/issues/586 using your GitHub account
--
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config
Received on Saturday, 28 December 2024 02:26:14 UTC