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

[geom] update polymorphic function signatures to provide better return types #467

Closed
postspectacular opened this issue May 9, 2024 · 1 comment

Comments

@postspectacular
Copy link
Member

postspectacular commented May 9, 2024

(extracted as new issue from a conversation in #464)

The shape operators returning new shapes are only returning the generic IShape type at current. This is often sufficient, however can cause weird type errors when passing these generically typed results to other functions expecting a concrete shape type. The current solution is to add manual type casts to satisfy the TS compiler, but maybe this can be improved...

I've been trying a few approaches to provide better return types, but none of them are truly "watertight" and/or as extensible as these polymorphic functions are intended to be... e.g. support for custom shape types can be added via e.g. flip.add("myshapeid", (shape) => { ... }) and this can be done for almost all of the thi.ng/geom functions... but that flexibility comes at a price, i.e. only a generic return type...

At least for the built-in types, a possible solution could involve function overrides and/or mapped types, possibly by renaming the actual thi.ng/defmulti-based polymorphic functions to something else (e.g. functionName => functionNameImpl) and exposing the new function overrides as the main API...

The issue is also quite complex to solve, because some of these operators are resulting in a different shape type than the input shape. Sometimes this predictable, other times, depends on optional parameters given... E.g. non-uniformly scaling a circle, triggers a conversion to an ellipse; rotating a rectangle results in a quad, etc.

@postspectacular
Copy link
Member Author

Completed & released with v8.0.0

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