-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow object patterns to tear off methods? #2561
Comments
I'd generally say no. Just because tear-offs and getter invocations share the same syntax, doesn't mean that they are the same thing. I'd be fine with a specialized tear-off syntax (which also works to tear off getters). I can see cases where I'd use it anyway: var (:add, :addError, :close) = streamController; // Assumes inferring the tear-off type.
otherStream.listen(add, addError, _wrapAction(close)); Basically, any time you want to access more than one thing on the same value, you can use destructuring for it. The real question is what we can do for dynamic tear-off. var dynamic(:Function add, :Function addError, :Function close) = streamController as dynamic; is there any reasonable implementation which will not tear off the methods. |
I don't have strong opinions here, but I could definitely see some places one might use it, e.g. I know I've seen people write code like this (because Object a = ...;
bool result;
if (a is Iterable) {
result = a.contains(3);
} else if (a is Map) {
result = a.containsKey(3) {
}
if (result) {
...
} which could become: switch(a) {
case Iterable(:var contains):
case Map(containsKey:var contains):
if (contains(3)) {
....
}
} |
It could just be me, but destructuring a tear-off is starting to get a little less than intuitively readable. Wouldn't it be better to encourage something like the following instead? Object a = // ...
bool Function(int) test = switch(a) { // the new switch expression
case Iterable: a.contains;
case Map: a.containsKey;
};
if (test(3)) { /* */ } |
I think we should allow method tear-offs in objectPatterns because we allow extension getter invocations, and also because @lrhn had some quite reasonable use cases. (You could actually say that a method tear-off is very similar to a getter invocation, whereas an extension getter invocation is a completely different thing.) This also means that we don't have to explain why it doesn't fail in the dynamic case if we use a patternField to invoke a supposed getter, and it turns out to be a method tear-off. Perhaps it isn't very hard to implement a dynamic error in that situation, but I just don't see how it would help anyone. |
I think we should allow it too. My reasoning is basically:
So we may as well make it do the thing they think it will do. |
- Allow object pattern fields to tear off methods. - No exception for non-exhaustive switch statements that don't need to be exhaustive. - Promoted types and bounds can be always-exhaustive. - Fix incorrect static error with cast patterns in assignments. - Clarify that patterns are not const contexts. Fix #2561. Fix #2698. Fix #2765. Fix #2758.
* [patterns] Fix a handful of small-ish patterns issues: - Allow object pattern fields to tear off methods. - No exception for non-exhaustive switch statements that don't need to be exhaustive. - Promoted types and bounds can be always-exhaustive. - Fix incorrect static error with cast patterns in assignments. - Clarify that patterns are not const contexts. Fix #2561. Fix #2698. Fix #2765. Fix #2758. * Tweak wording in a few places.
The patterns proposal currently says:
(Emphasis added.) This implies that you can't refer to a method in an extractor pattern in order to tear it off. I'm not sure if allowing that is particularly useful, but we certainly could support it. Should we?
The text was updated successfully, but these errors were encountered: