I have a few questions currently that are kind of intertwined. But the all rise from the fact that for my project we are building an API on top of babylon.js. As part of this API we have basic math types like interface IVector3 { x, y, z }
We now also want to re-export Babylons math types to make it easier for users to do calculations, but as part of this I want to add some new helper stuff, mainly toIVector3() and fromIVector3() etc. I tried to do this by extending the classes available in babylon but this gave me some annoying things related to typescript and types. for example, all methods in math.vector.ts that returns the object itself does this using the Vector3 etc type rather than this type. This means that I canāt do things like const ivec3 = new MyVector3(1,2,3).normalize().toIVector3()
Since the normalize function returned a Vector3 (even though its performed in place).
This can be solved by changing the type returned by normalize() to this rather than Vector3.
So now I want to know if Iām missing some cavity with doing this type change? If not, is this something that the project is interested in me contributing with?
Iāve also noticed that there seems to be some inconsistencies with the returned value for ā¦ToRef() functions where some return void, some return this (aka object you called the function on), and some return the ref object.
I understand that fixing this is a breaking change, but is there an interest in normalizing this as well? this is something I would like to take on if so. Got bitten at least once from getting another value than I expected as the return.
Hope for some great discussion and insights on this topic
Itās not a fluent API by design. I canāt imagine it would be changed to not be - what about making your own builder that wraps babylon types that does return this? Have you seen the āinPlaceā operations they might work, although some are missing.
We have never considered the need to extend our math classes outside of the math module, and we therefore never saw the need to change the return type to this
The API has grown over time, and only ārecentlyā (i guess 1 or 2 versions back_ we have agreed about the consistency required in this class. And since we are backcompat, we still have a function like normalize, which is actually normalizeInPlace, as opposed to normalizeToRef.
Regarding the toRef - I donāt mind having all toRef return the reference if they donāt return anything. Moving from return voind to return a value is not a back-compat issue. The other way - yes.
I wouldnāt mind doing that, but TBH i would go with brianās suggestion and create a wrapper around this class and not extend the class itself. Thatās
Thanks for the quick responses
Yes, I know of the inPlace versions, inPlace sadly has the same issue as i pointed out with the normalize, the type get downgraded to Vector3 rather than my extended type.
@RaananW so if I understand your answer, a 6.0 breaking change to make math functions consistent is not on the table?
I also totally get that my needs are quite unorthodox and thatās why I want to check here if there is even an interest for extending the API with this functionality. Iāve been playing around with it a bit and the syntax in methods gets a bit wonky but should otherwise have no back-compat issues AFAIK.
public add(otherVector: DeepImmutable<Vector2>): this {
return new (<any> this.constructor)(this.x + otherVector.x, this.y + otherVector.y);
}
Also just to explain my use case a bit more, we are building an API around babylon that our users build 3D apps on. this has lead us to run Babylon through a fork which is currently running stock 5.23. Our goal is to contribute as much as we can but if this doesnāt fit with the project I can fix this on my side as a last resort. I would be happy to do this and make a PR though if there is interest. (or for parts of my changes)
Mixins sounds like an interesting concept, will have to check more in to that! thanks (though i suspect that I will run in to similar issues when trying to do method chaining)
Nope, not on the table
The team maintains back-compat between major versions, but we do deprecate older functions and possibly remove them when releasing a major version.
I donāt think it is unorthodox, i think you are trying to get more consistency for your users, and I totally get it.
I donāt think changing the return type from Vector3 to this is a back-compat issue, and I donāt mind doing that TBH. If you want to submit a PR for us to review, please go right ahead!
Iāve been bitten by this a few times in my own side projects. I did not expect ToRef functions to return the original object. Thatās just wrong in my opinion. I almost see fixing this as a bug fix as I canāt imagine someone wanting to continue with the original object. Are there legitimate uses for ToRef functions returning the original object?
The issue for me is that almost all of the ToRef functions return this and not the result. I didnāt bring this up before because almost all of the ToRef functions will need to change.
Yeah its quite a few places that would be changed, I would be more than happy to make that part of my PR, and then maybe we could drive the discussion from that PR?
@JohnK
The this type is a feature of typescript related to polymorphism. It lets a function return the type of a deriving class rather then the type of the base class. A very handy tool when writing fluent APIs. you can read more about the feature here: this Types