-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: develop
Are you sure you want to change the base?
Conversation
You're totally right, thanks Michal! I'm curious whether we will run into any difficulties adding types. |
(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?) |
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 |
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!) |
Yeah, so basically TypeScript is only about compile time type checking, and
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.
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. |
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 |
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. |
Thanks Matt. I think indeed the biggest challenge is not making the
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 |
Yeah that's definitely possible! I think I'll maybe try starting from a blank slate and see what's possible with |
Also, just noticed this issue on their repo which looks promising / interesting: gcanti/io-ts#29. Might use that as a starting point |
😎 sounds good, looking forward to see how your experiments work out. |
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 😄 |
That sounds promising! Thanks for the update |
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.) |
@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. |
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. |
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 :) |
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.