-
Notifications
You must be signed in to change notification settings - Fork 213
Strawman union proposal question regarding intersection #2731
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
Comments
Dart way to do this: // Base class for A & B
abstract class AB {
int get something;
}
class A implements AB {
@override
int get something {
// ...
}
}
int method(AB data) {
return data.something;
}
void main() {
print(method(A()));
} |
Yes, but then you get really limited and you can't do a lot of things. The proposal is a solution for this. |
@ykmnkmi the "Java way" works for simple cases, only when these classes are under our control. The same can't be done for |
@Wdestroier Dart way to do this: void processStream<T>(Stream<T> stream);
void processIterable<T>(Iterable<T> iterable);
int hash(String input);
int hashBytes(List<int> inputBytes); For example look at SDK code. |
Thanks @ykmnkmi. I just want to have |
With the upcoming support for pattern matching, you will be able to write: method1(Object e) {
switch (e) {
case num n: // Use n...
case String s: // Use s...
case bool b: // Use b...
}
} |
Now if you could just replace Object with |
BTW, does the upcoming pattern matching allow for: method1(Object e) {
switch (e) {
case Area n: // Use n...
case Circle s: // Use s...
}
} ? Because on the proposal I need to use |
They are different patterns. A pattern like |
What I never understood is where this 'n' came from. Like, if it is BTW, thanks for the explanation, it makes even easier to do what I want. Unionssss, please 😭. |
With an object pattern, it is always named because we need to know what getter to call. It's just that we allow you infer the name from the variable.
Yes. |
Is this related to #1123, ie the feature in other languages where you can elide the argument name of named arguments if they're the same? In JS for example in an object |
No, it is just a way of saying "this is the output variable" so you can play with it later. You are kind of declaring something that hasn't appeared yet. It is different, but it is a great idea. |
Ah I see, so it's the bound variable for the matched pattern. Makes sense, it's similar to other languages like OCaml or Rust. Why is |
We don't currently have any plans to support this, but it's not a bad idea. Right now, the eliding names is only within patterns, not argument lists.
Correct. There's some more context (though it describes a somewhat outdated version of the proposal) here. |
Thanks, just wanted to clarify another thing. If |
With Dart 3 allowing to switch on types, I only miss A | B, so that I stop using objects. I don't need any other union behavior in my apps for now. |
You can model this in your type system by using a |
Is there a library that does this? I want to use on Flutter, so it needs to be native. |
There are many packages that implements simple A naïve implementation would be: void myFunction(Either<int, String> value) {
switch (value) {
case Left(:final value):
print('Got an int: $value');
case Right(:final value):
print('Got an String: $value');
}
}
sealed class Either<T, U> {}
final class Left<T, U> implements Either<T, U> {
const Left(this.value);
final T value;
}
final class Right<T, U> implements Either<T, U> {
const Right(this.value);
final U value;
} With this you get all the pros of a language-level union type:
The only obvious downside is that it is a little more verbose. |
75% of my union usage in TS is
As the proposal says, intersection is hard, so maybe for now Dart could avoid
data.something
where something exists in both A and B.But... Just having A | B in the parameter and being able to switch between them would be extremely helpful for me!! I hate using dynamic or object. And I hate using a second variable that's not null when the first one is.
So, this would be extremely helpful to me:
Yet I hear "is" is really really expensive. So maybe this is faster than comparing by "is" twice.
Any thoughts?
I'm not sure opening an issue is the correct path, just wanted to contribute to the discussion.
The text was updated successfully, but these errors were encountered: