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

First class functions, function types, conversion functions #200

Merged
merged 16 commits into from
Feb 11, 2024

Conversation

sharkdp
Copy link
Owner

@sharkdp sharkdp commented Oct 8, 2023

This PR implements function types and function references (first class functions). It also allows single-parameter functions to be called using x -> f as an alias for f(x). Both things together enable a lot of interesting features:

  • We can implement functions like fn diff(f: Fn[(Scalar) -> Scalar], x: Scalar) -> Scalar in Numbat
  • We can write 10 million seconds -> human, 78 -> chr or now() -> unixtime instead of using the // operator or normal function call syntax.
  • We can return functions from functions. For example, we can return a base-b conversion function from base(b), allowing us to write things like 0xffee -> base(2)
  • We can simply type the name of a function to see it's type signature

image

Limitations:

  • This does not (yet) work with generics

To do:

  • Disallow taking function pointers to generic functions (for now)
  • Disallow function types with unknown types (no support for generics for now)
  • Proper type check for function pointer calls
  • Allow FFI functions to be the target of a function pointer
  • Re-enable suggestions for wrong function names ("Unknown callable: Cos, did you mean cos")
  • Resolve all todo!s
  • Resolve TODOs
  • Add tests
  • Allow returns_fn()(…) input? Currently only works using (returns_fn())(…)

closes #202

@sharkdp sharkdp mentioned this pull request Feb 8, 2024
@sharkdp sharkdp force-pushed the first-class-functions branch 2 times, most recently from c42fa17 to 7ac00af Compare February 11, 2024 10:10
@sharkdp sharkdp marked this pull request as ready for review February 11, 2024 21:28
@sharkdp sharkdp changed the title First class functions, function types First class functions, function types, conversion functions Feb 11, 2024
@sharkdp sharkdp merged commit 44336e5 into master Feb 11, 2024
15 checks passed
@sharkdp sharkdp deleted the first-class-functions branch February 11, 2024 22:13
@sharkdp sharkdp mentioned this pull request Feb 25, 2024
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.

Conversion functions
1 participant