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

TypeScript experiments #89

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft

TypeScript experiments #89

wants to merge 2 commits into from

Conversation

cshaa
Copy link

@cshaa cshaa commented Apr 13, 2021

Since there are speculations about porting math.js to math.ts (see #2076), I think porting typed-function to TypeScript would be a good start. This PR is something between a proof-of-concept and my personal playground. I don't expect it to be ready for merging anytime soon, but we can definitely discuss the future of this project here.

@josdejong
Copy link
Owner

You're totally right, thanks Michal! I'm curious whether we will run into any difficulties adding types.

@gwhitney
Copy link
Collaborator

gwhitney commented Mar 7, 2022

(I will just chime in with my off-the-cuff two cents: it would seem ideal, if possible, for a putative math.ts, should it ever exist, to use the built-in typing mechanisms of TypeScript and so not have a typed-function module at all. It seems very counterintuitive to implement a runtime type-checking facility on top of a statically-typed language... But maybe I am missing something?)

@josdejong
Copy link
Owner

It seems very counterintuitive to implement a runtime type-checking facility on top of a statically-typed language

yes, that is exactly the difficulty that we'll need to solve somehow. And it will not be trivial. There may be ways to use TypeScript too for runtime type checking. Or the other way around: it may be possible to have typed-function correctly generate TypeScript definitions. I think we should try to think totally out of the box and rethink how to solve the things typed-function solves right now: run time type-checking, automatic type conversion, composing/extending functions, etc.

@gwhitney
Copy link
Collaborator

Ahhh, I understand now. I thought TypeScript allowed you to overload with different function bodies. (As I said, I am no TypeScript pro!) Reading up on this, I see that the overloads are only a convenient way of expressing a more complicated function type, and you are only allowed one implementation, so you have to do all of your own runtime type dispatch (unlike, say, in C++). So I see that there is a real need for some extra capability here.

Having understood that gap in TypeScript, it seemed clear that others would have a similar desire, and indeed a quick search on "TypeScript runtime type dispatch" surfaced at least four packages that seemed potentially relevant: https://caderek.github.io/arrows/packages/multimethod/, https://github.com/Hypercubed/dynamo, https://github.com/gcanti/io-ts, and https://github.com/fabiandev/ts-runtime.

My naive guess is that in the pursuit of a potential mathts (someday), it might well be less time/effort to adopt an existing facility along the lines of one of the above (and or contribute to it if it does almost all but not quite all of what we need), rather than rewriting typed-function for TypeScript... just a thought.

And on the topic of TypeScript CASs, I imagine you're aware of http://algebrite.org/ (but definitely the documentation, at least, of that system is not as clear and extensive as mathjs!)

@josdejong
Copy link
Owner

Yeah, so basically TypeScript is only about compile time type checking, and typed-function is only about runtime type checking.

My naive guess is that in the pursuit of a potential mathts (someday), it might well be less time/effort to adopt an existing facility along the lines of one of the above (and or contribute to it if it does almost all but not quite all of what we need), rather than rewriting typed-function for TypeScript... just a thought.

Yes that could very well be that porting functionality into a new, fresh initiative is the best solution. We really need to fully rethink this. Mathjs has quite some "luggage" from the past, it will be hard to move forward if we try to keep everything backward compatible with all the principles and ideas from the past.

And on the topic of TypeScript CASs, I imagine you're aware of http://algebrite.org/

Yes I've seen it, it looks really neat 😎 . I would love to bundle forces somehow in this regard instead of having two similar solutions. Not sure if that is possible though. For reference, see josdejong/mathjs#920, josdejong/mathjs#1725.

@mattvague
Copy link

Having understood that gap in TypeScript, it seemed clear that others would have a similar desire, and indeed a quick search on "TypeScript runtime type dispatch" surfaced at least four packages that seemed potentially relevant: https://caderek.github.io/arrows/packages/multimethod/, https://github.com/Hypercubed/dynamo, https://github.com/gcanti/io-ts, and https://github.com/fabiandev/ts-runtime.

In working on this PR I had the same thought that maybe there was another library that we could reimplement the innards of this library with. I have a bit of experience with https://github.com/gcanti/io-ts so I might experiment with it

@gwhitney
Copy link
Collaborator

I've looked at io-ts a couple of times and it seems like it could be helpful for type conversion but I haven't seen how it's directly related to dispatching different behaviors based on the type of an input, which to me is the core of typed-function. As mentioned in josdejong/mathjs#2076 (comment) my first biggest worry is the fact of the (typescript) types of various methods of the typed-function package changing as you add conversions and such; I'd love to see even just a toy proof of concept of how that could work.

@josdejong
Copy link
Owner

Thanks Matt. I think indeed the biggest challenge is not making the typed-function API itself typed, but make functions that we dynamically create with it typed.

io-ts indeed looks promising. I'm curious to understand how it works under the hood.

I can imagine we can write some TypeScript compiler plugin to generate types at compile time or so.

Let's keep in mind that we not necessarily have to translate typed-function one-on-one to TypeScript. It may be that there is a totally different way to solve what typed-function does: runtime type-checking and type-conversion, resolving the right function signature for a given list with signatures.

@mattvague
Copy link

Let's keep in mind that we not necessarily have to translate typed-function one-on-one to TypeScript. It may be that there is a totally different way to solve what typed-function does: runtime type-checking and type-conversion, resolving the right function signature for a given list with signatures.

Yeah that's definitely possible! I think I'll maybe try starting from a blank slate and see what's possible with io-ts in general and then we can evaluate from there

@mattvague
Copy link

Also, just noticed this issue on their repo which looks promising / interesting: gcanti/io-ts#29. Might use that as a starting point

@josdejong
Copy link
Owner

I think I'll maybe try starting from a blank slate and see what's possible with io-ts in general and then we can evaluate from there

😎 sounds good, looking forward to see how your experiments work out.

@mattvague
Copy link

Alright, quick update: I got io-ts working (somewhat) for matching and creating typed functions but not quite ready to show it off yet. Will try and put in a little more time this weekend to show it off 😄

@josdejong
Copy link
Owner

That sounds promising! Thanks for the update

@gwhitney
Copy link
Collaborator

Not to put any damper on the io-ts direction -- if mattvague can get something good with it working, that's great -- I just wanted to mention that on some more inspection https://github.com/Hypercubed/dynamo looks pretty close to a straight analogy of typed-function in a TypeScript setting.

(I still personally don't know how to deal with the fact that typed-function/mathjs seem to want to be dynamic while TypeScript needs everything static, but as I have said before, hopefully someone can figure out a good way to reconcile that apparent mismatch.)

@mattvague
Copy link

Not to put any damper on the io-ts direction -- if mattvague can get something good with it working, that's great -- I just wanted to mention that on some more inspection https://github.com/Hypercubed/dynamo looks pretty close to a straight analogy of typed-function in a TypeScript setting.

@gwhitney Not at all, if there's a preexisting option out there that suits the needs of MathJS that'd be great. Very sorry about going radio silent, am just under a huge crunch right now to meet a fall deadline. Was hoping to have some time in the next few weeks to continue on my prototype, but I totally understand if you want to get going with things and/or take it in another direction.

@gwhitney
Copy link
Collaborator

I totally understand if you want to get going with things and/or take it in another direction.

No, sorry if I wasn't clear. My efforts on the typed-function/mathjs ecosystem are currently focused in another direction (see josdejong/mathjs#1975 and https://code.studioinfinity.org/glen/pocomath). My comments were merely meant as suggestive for those who have the interest/energy/drive/opportunity for working on a TypeScript direction.

@josdejong
Copy link
Owner

josdejong commented Aug 16, 2022

Thanks for the updates. https://github.com/Hypercubed/dynamo looks indeed quite similar to what we're looking for. There are indeed two big things we want to tackle with mathjs: one is a better solution to import and extend functions and datatypes (josdejong/mathjs#1975), and the other is the wish to have TypeScript support (josdejong/mathjs#2076). At this point we do not yet have a clear solution to solve both :)

@gwhitney

This comment was marked as resolved.

@josdejong

This comment was marked as resolved.

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

Successfully merging this pull request may close these issues.

4 participants