-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Make "void" arguments syntactically optional? #34176
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
This would be great since Something I just thought about: Would |
Yeah, that's a good point. It's valid to throw away anything into a void location (such as Regarding
This is actually already true! To be "optionally typed," and yet still have some type of safety, we made it a compile-time error to mix both "return;" and "return exp;" in one function body. (same with yield). It would maybe be useful to allow this in the Dart 2 at some point. It's arguably still suspect behavior, but maybe if we had non-nullable types, we could allow a mixture when the return type is a nullable one. |
I would find this more useful if it also worked for named functions. A fairly common situation for me is that I have a function However, I don't see how this could work well as just syntax sugar. As sugar, I think it would have to be some "rewrite rules" based on the expected type at a position to e.g. rewrite // Does not work if you only do anonymous function rewriting
var x = () {};
Future<void>.then(x); // Does not work (i.e. returns null) in either case,
// since the rewrite rules depend on static typing
Future<void> bar(callback) {
if (callback is void Function(void)) {
return Future<void>().then(callback);
}
return null;
}
bar(() {}); // Implicitly wrapping named functions changes the semantics of function equality,
// which could be a nasty surprise if you're registering and deregistering callbacks
var nullaryFuncs = Set<void Function()>();
var unaryFuncs = Set<void Function(void)>();
void foo() {}
nullaryFuncs.add(foo);
nullaryFuncs.add(foo);
unaryFuncs.add(foo);
unaryFuncs.add(foo);
// as expected:
assert(nullaryFuncs.length == 1);
nullaryFuncs.remove(foo);
assert(nullaryFuncs.isEmpty);
// but surprisingly:
assert(unaryFuncs.length == 2);
unaryFuncs.remove(foo);
assert(unaryFuncs.isNotEmpty); To avoid these issues I think you would have to make |
Shouldn't this be in the language repository ? |
Yep, I think I filed this before the language repository existed
…On Sat, Sep 3, 2022, 3:47 PM cedvdb ***@***.***> wrote:
Shouldn't this be in the language repository ?
—
Reply to this email directly, view it on GitHub
<#34176 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAMNM66QIREIZREDDYUZLCTV4PIRTANCNFSM4FQIMZWQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Instead of
Could we make it possible to write just
?
Some tendrils I can think of here:
() {}
is inferred as(void) -> R
, is there a way to write this explicitly? Is(void _) {}
close enough?The text was updated successfully, but these errors were encountered: