-
Notifications
You must be signed in to change notification settings - Fork 245
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
feat(dotnet): handling optional and variadic parameters #680
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should variadic
arguments be defaulted to []
?
In C#, a parameter array can not have a default value. It is by definition optional because it is passed as an empty array if not specified. Section 7.4.1:
This means no changes to the runtime should be required, as the parameter will be passed as an empty array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we made sure this works with the current runtime? Have the compliance tests been updated to reflect this change?
optionalPrimitive = '?'; | ||
optionalKeyword = ' = null'; | ||
} else { | ||
optionalKeyword = ' = null'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The embedded if and else both set optionalKeyword to the same. Move this set to the parent if.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, this is a refactoring leftover. Removing
optionalKeyword = ' = null'; | ||
} | ||
} | ||
if (p.variadic) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this an else if?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like JSII is already doing the "mutual exclusion", but no harm in enforcing it with an if/else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah actually TypeScript won't allow you to use both the variadic syntax and the optional syntax at the same time... So if it's variadic, it cannot be optional, and vice-versa. But there's definitely no harm in checking again.
Tested with the CDKHello app (the S3 bucket instruction used to require a useless new BucketProps(), not anymore), calling 'cdk-synth' works fine. Tested with the ported EC2/ECS sample stack, which used to require useless new Ec2TaskDefinitionProps() and useless new RepositoryImageProps(), calling 'cdk-synth' works fine. Looks like the variadic might need some runtime work. Looking into this and holding off the PR so far. |
This is ready to be reviewed/merged. tested with the CDK for Lambda and variadic layers. Also added a full suite of compliance tests that run with the runtime with all sorts of tests around optionals and variadic params (nulls, empty, one value array, multiple values array etc..). |
Looking super good. Love the small enhancements to readability that you brought in there & the comments... Sweet sweet stuff! |
Handling optional and variadic parameters in .NET
Emits the =null or params[] keywords when required in constructors or methods.
Ran pack.sh in the CDK with this change, and the S3 construct now looks better:
Optionals:
public Bucket(Amazon.CDK.Construct scope, string id, Amazon.CDK.AWS.S3.IBucketProps props = null): base(new DeputyProps(new object[]{scope, id, props})) { }
Making the C# call look like:
var bucket = new Bucket(this, "bucketName");
Rather than
var bucket = new Bucket(this, "bucketName", null);
Variadic:
Tested with null values, empty array, one value array, multiple values array.
Fixes #153
Fixes #210
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.