Return type annotations #52
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
It would be interesting to add to this a proof of concept of return-type annotations. The ides is that an implementation of sqrt that is currently:
would become
and a short and sweet implementation like one from negate:
would become
(The symbol
R_
was proposed above to be as short and sweet as possible since idealy the function name would be '->' ;-). But 'yields' is another possibility, as well as a straight structural notation like:The advantage of using a function symbol like
R_
is that there's an opportunity to put more code there if it ends up being helpful; the advantage of a purely structural notation is that there is no symbol to import and it is very compact.Note the exampe of
sqrt
was chosen above to show that the return type would need to come after the dependencies, not on the outer level, because the return type can depend on the dependencies as supplied. (In fact, in some sense the latter part of the sqrt implementation should be something like:but that's now getting a bit cumbersome... maybe R_ could be made cleverer so that you
could just write:
That now seems like the way to go.
Besides allowing a typescript declaration generator as per https://github.com/josdejong/typed-function/issues/123, return type annotation could be used to
math.compose(math.sqrt, math.negate)
which produces a function that computes sqrt(-x) but only performs type dispatch once, when negate gets the argument, but arranges to call the proper implementation of sqrt on the result without having to typecheck. This compose operation could be used to provide the semantics of expressions, nearly eliminating the typechecking that would go on in evaluating an expression.So it seems well worth the effort.
Resolved in #53