-
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
Add return-type annotations? #124
Comments
👍 makes sense. I guess it is only relevant in a TypeScript context, so it may be enough to just rely on TypeScript definitions and not something similar to the map with JavaScript (input) signatures that |
I think with the advent of a very promising proposal in #123 that looks like it would actually work, the priority for this issue rises. So there becomes the question of notation. Would we want to put the return type in the object key like:
Or put them in the value, like:
The former is prettier but the latter would almost certainly be easier to deal with in the code, so i think i come down on that side personally. This also begs the question of how to deal with an operation like sqrt whose return type depends on config variables (although organizing as in the Pocomath prototype would solve that, as the implementation is not returned until the config is read). |
And actually, another reason why this is not only relevant in a TypeScript world and hence is well worth doing (quoted from https://code.studioinfinity.org/glen/pocomath/issues/52):
|
Agree, I find a notation in the signature, like Thinking aloud, if we go for the second approach, maybe it is possible to go for a notation like: const sqrtImps = [
['number|Complex', 'number|complex', imp1],
['BigNumber', 'Bignumber|undefined', imp2]
] or something like: const sqrtImps = [
signature({ args: 'number|Complex', returns: 'number|complex', fn: imp1 }),
signature({ args: 'BigNumber', returns: 'Bignumber|undefined', fn: imp2 })
] |
As for the first approach, we could possibly write that in a TypeScript compatible notation: const imps = {
'sqrt(x: number|Complex) : number|Complex)' : imp1,
'sqrt(x: BigNumber) : BigNumber|undefined' : imp2
} |
I am in the middle of implementing a proof-of-concept of return-type annotations in https://code.studioinfinity.org/glen/pocomath (but I am also still in the middle of the continent, so it won't be done for a bit) and based on the experience so far:
where the return type depends on the configuration, and in
where the return type depends on the actual input type T that is captured by the implementation (because Pocomath has a subtype NumInt of number for numbers that happen to be integers, and we need to capture the fact that the negation of a NumInt is a NumInt without replicating the implementation, which is good for both the NumInt subtype and regular number type). So specifically in the Pocomath style, only the second approach is feasible since the return type has to be determined inside the outer layer that takes the dependencies. I'll let you know when this aspect of Pocomath is "done" so you can take a look; let me know if you want me to replace all occurrences of "R_" with "Returns". |
Very good points, thanks for the clear explanation. Yes I prefer |
Is there any other progress on this? |
No |
There are two circumstances so far in which the utility of typed-function knowing the return type of (each signature of) a typed-function has come up: (1) supporting useful type inference in automatically-generated TypeScript declarations for typed-functions, see #123, and (2) determining what sort of an entity a sub-expression in math js will be, so as to know what simplifications are valid to apply to that subexpression. (Right now, mathjs just applies the same configurable list of possible simplifications at all levels of an expression, but that could be refined to allow changing the order of numbers being multiplied but leave the order of matrix multiplications alone, if mathjs could tell which parts of an expression evaluated to numbers or to matrices.)
Given these two indications of its value, it's worth considering whether to add return-type annotations to typed-function.
The text was updated successfully, but these errors were encountered: