-
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
Add wildcard to method/constructor arguments [Breaking change suggestion] #2704
Comments
It does seem like it'd be nice to have a standard method of naming parameters you have no intention of using, which are only there because you're passing the function to something that expects a certain parameter list. void errorCodePrinter(_, _, int errorCode) => print(errorCode); //Only cares about the third parameter; no name collision with the first two unused ones.
var thing = CallbackThing(
errorHandler: printer //Expects a function that takes (Object, StackTrace, int).
); I dunno if there's a good way to extend that idea syntax to named parameters. You'd have to name them anyway to specify which ones that you don't care about. Maybe just In general it'd be nice if you could specify a parameter that takes a function without caring too much what optional parameters that function takes. For instance, if you have a function like |
I expect that we'll end up allowing (irrefutable) patterns in parameter position in a later language version. It's not part of the scope of the initial feature, but I don't see any technical reason to not get there eventually. Should we just make I'm all for it! :) |
I got hinted by @parlough that there seem to be already ongoing work on this feature request in the following document made by @munificent: |
Heh, oops! I don't think I meant to commit that doc. :D But, yes, I did start working on a proposal to make |
Update: I have a more complete proposal now, but it's not going to make it into 3.0. |
FWIW I tried out the new // "workaround" for https://github.com/dart-lang/language/issues/8
xs.map((_) => _.toString());
// "workaround" for https://github.com/dart-lang/language/issues/3001
users.map((_) {
final (user, selected) = _;
...
}) |
(I could not find any issue similar to this. Also, I might be completely wrong and if that is the case, just close it :) )
As far as I understand the patterns proposal, we have a special meaning if a variable is called
_
where we will not bind to any variable. But as far as I can read, it does not sound like this feature is going to be implemented in other areas like e.g. arguments to methods/constructors?If not, then I would suggest the breaking change (maybe for Dart 3? Dart 4?) of making a
_
in parameters an unbinding variable and make it so we cannot read this argument.This should also make it possible to have multiple arguments named
_
compare to the current behavior where_
is more like a suggestion from the programmer but data is still being assigned to the argument, which allow us to read the argument and prevents us from having multiple arguments named_
but are instead going to use__
,___
or similar.As I see this, it would improve the usability of the language in general when patterns are introduced since I currently think it is kinda confusing that
_
have a specific meaning in patterns but not outside of patterns.I would expect a lot of people are already using
_
to communicate an argument is not going to be used. But I do wonder how many are actually reading from an argument named_
.The text was updated successfully, but these errors were encountered: