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

[ ] automatic parameter brackets #4247

Open
nate-thegrate opened this issue Jan 31, 2025 · 3 comments
Open

[ ] automatic parameter brackets #4247

nate-thegrate opened this issue Jan 31, 2025 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@nate-thegrate
Copy link

Optional positional parameters coexisting with named parameters would be great by any means, and we already have some well-thought-out feature proposals:

This issue presents a slight variation on those ideas.



Maybe the compiler could "automatically add the square brackets" to as many parameters as it can, starting from the rightmost positional parameter. For example:

foo(int a, int? b) {
  // b is optional
}

foo(int? a, int b) {
  // both parameters are required
}

foo(int a = 42, int? b) {
  // both parameters are optional
}

foo(int? a = 42) {
  // should probably trigger a linter warning, since "?" signals that the param is null by default
}

foo(int a = 42, int b) {
  // compile-time error
}

That compile-time error would be consistent with e.g. Python:

def foo(a=42, b):
    return a + b
File "script.py", line 1
  def foo(a=42, b):
                ^
SyntaxError: parameter without a default follows parameter with a default



If this were implemented, named arguments could follow a similar pattern: instead of typing required, a named argument would be required if it's non-nullable and lacks a default value. (A nullable parameter such as a button's onPressed could be given a @required annotation if desired.)

Essentially, this is identical to the proposal in #2232 but would also allow for parameters with default values, since I think it's really cool that the IDE shows this info when your mouse is hovering on a constructor.

AnimationController default values

@nate-thegrate nate-thegrate added the feature Proposed language feature that solves one or more problems label Jan 31, 2025
@nate-thegrate
Copy link
Author

Aside from "not needing to add brackets", there's something else from #2232 that I really like:

Completer.complete(T value) ...

That means that you can omit the argument on a Completer<int?>, but not on a Completer<int>. That's precisely the desired behavior, and now it's supported by the static type system.

@mateusfccp
Copy link
Contributor

This would disable the possibility of having a required positional nullable parameter at the end of the parameters list?

@nate-thegrate
Copy link
Author

This would disable the possibility of having a required positional nullable parameter at the end of the parameters list?

Yes, or at least the compiler wouldn't enforce it. You could still do

foo(int a, @required int? b) {}

And then (as long as the lint isn't disabled) you'd get a warning by not including it.

In my opinion, APIs generally should avoid requiring someone to specify null, but to each their own :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

2 participants