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

Functions: generics / parametric polymorphism #25

Closed
sharkdp opened this issue Sep 20, 2022 · 3 comments
Closed

Functions: generics / parametric polymorphism #25

sharkdp opened this issue Sep 20, 2022 · 3 comments

Comments

@sharkdp
Copy link
Owner

sharkdp commented Sep 20, 2022

Add support for generic functions, to allow things like

fn sqr(x) = x^2

Syntax

We can go the Rust route and do something like

fn sqr<T>(x: T) -> T^2 = x^2

Or we could add a special syntax for type variables, e.g. $T:

fn sqr(x: $T) -> $T^2 = x^2

Or go the Haskell way and use lowercase expressions as variables:

fn sqr(x: d) -> d^2 = x^2

that could work well with inferred types (#29):

f(x: d0, y: d1, z: d2) -> dR = …
@sharkdp sharkdp changed the title Generic functions Functions: generics Sep 20, 2022
@sharkdp sharkdp changed the title Functions: generics Functions: generics / parametric polymorphism Sep 25, 2022
@sharkdp
Copy link
Owner Author

sharkdp commented Sep 26, 2022

Generic parameter deduction is definitely not straightforward. Imagine:

fn foo<D>(x: Meter * D^3) -> Scalar = …

foo(3 * meter * time^2)  // D^3 = time^2   ==>   D = time^(2/3)

or even worse:

fn bar<D1, D2>(x: D1 * D2, y: D1, z: D2) -> Scalar = …

This is a potential use case and could probably be supported somehow but maybe we should first disallow this. Or only allow it in the opposite order.

@sharkdp
Copy link
Owner Author

sharkdp commented Sep 26, 2022

Error cases to consider:

  • fn foo<Length>(x: Length) = …
  • fn foo<D1, D2>(x: D1*D2) = … (see above)
  • fn foo<D1>(x: Scalar) -> Scalar = …

sharkdp added a commit that referenced this issue Sep 26, 2022
@sharkdp
Copy link
Owner Author

sharkdp commented Sep 27, 2022

image

@sharkdp sharkdp closed this as completed Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant