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

Abstract Union Types #2

Open
fcard opened this issue Sep 12, 2019 · 0 comments
Open

Abstract Union Types #2

fcard opened this issue Sep 12, 2019 · 0 comments

Comments

@fcard
Copy link
Owner

fcard commented Sep 12, 2019

Since I already implemented julia-style abstract types, I may as well implement union types. The easy way would be to just create a variant of TypeMatch that matches against multiple types and have the matched argument inferred as Value, which will result in the following semantics:

multifunction! {
  fn F(x: Union![i32, i64]) -> Value {
    x + 1 // x is of type Value
  }
}

fn main() {
  println!("{}", F(1i32)) // 2
  println!("{}", F(1i64)) // 2
}

But I wonder if it wouldn't be better if two methods were created, one where x is a i32 and another where x is a i64... That might be too tricky to handle even from the user's perspective, actually; in the above case, you would need to choose a return type that will work for both. One could convert x to i64 in this case because it's sort of a supertype, but if one had say a Union![f32, i64] this wouldn't work. One could convert x to Value but then that's just the first implementation, but more verbose. (Well, it's also opt-in...)

HMMMM...

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

No branches or pull requests

1 participant