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

Named Required Parameters are expected to be passed as optionalParameters #385

Open
eseidel opened this issue Dec 8, 2022 · 1 comment

Comments

@eseidel
Copy link
Contributor

eseidel commented Dec 8, 2022

Named Required Parameters are expected to be passed as optionalParameters

I had initially written my code to pass required named parameters into the requiredParameters array, that appears to be wrong. It generates this:

// generates "fib(required int i)" // which is invalid dart.
      Method(
        (b) => b
          ..name = 'fib'
          ..requiredParameters.add(
            Parameter(
              (b) => b
                ..name = 'i'
                ..named = true
                ..required = true
                ..type = refer('int').type,
            ),
          ),
      );

There is even a test for this. 🤣
https://github.com/dart-lang/code_builder/blob/master/test/specs/method_test.dart#L496

The meta issue is that the split between "requiredParameters" and "optionalParameters" doesn't make sense (and isn't documented):
https://pub.dev/documentation/code_builder/latest/code_builder/MethodBuilder-class.html
(There is no mention of what type of Parameters should end up in one vs the other.)

@eseidel
Copy link
Contributor Author

eseidel commented Dec 8, 2022

I suspect that before the addition of the "required" keyword to the language, all named arguments were functionally optional so I can see how we got here. :)

@NAMEER242
Copy link

For anyone still want to create a required named arguments, you can use the optionalParameters for both optinal and requeired parameters like this:

Parameter(
    (b) => b
     ..name = 'requiredParam'
     ..named = true
     ..required = true
     ..type = refer('int').type,
),

and for optinal use:

Parameter(
    (b) => b
     ..name = 'requiredParam'
     ..named = true
     ..type = refer('int').type,
),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants