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

dotnet: support optional arguments in methods and constructors #210

Closed
mpiroc opened this issue Sep 5, 2018 · 0 comments · Fixed by #680
Closed

dotnet: support optional arguments in methods and constructors #210

mpiroc opened this issue Sep 5, 2018 · 0 comments · Fixed by #680
Labels
effort/small Small work item – less than a day of effort language/dotnet Related to .NET bindings (C#, F#, ...) p0

Comments

@mpiroc
Copy link
Contributor

mpiroc commented Sep 5, 2018

jsii methods/constructors can have optional arguments. The .NET generator should respect those and pass null in calls that don't supply them.

-- from @mpiroc:

jsii-dotnet-generator should supply default values for arguments with optional types

jsii-dotnet-generator ensures that null is a valid value for types marked optional in JSII. For value types, this means making them nullable, i.e. int?. For reference types, null is already a valid value.

When a type like this is used in a parameter list, jsii-dotnet-generator should create a default value of null for the parameter. Currently, the user has to explicitly pass null.

@mpiroc mpiroc added the language/dotnet Related to .NET bindings (C#, F#, ...) label Oct 25, 2018
@eladb eladb changed the title jsii-dotnet-generator should supply default values for arguments with optional types dotnet: support optional arguments in methods and constructors Nov 15, 2018
@fulghum fulghum added the p0 label Apr 2, 2019
@fulghum fulghum added the effort/small Small work item – less than a day of effort label Apr 22, 2019
@RomainMuller RomainMuller added this to the .NET Support milestone Jul 30, 2019
RomainMuller pushed a commit that referenced this issue Aug 9, 2019
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.


```
// Array with no value in constructor params
var variadicClassNoParams = new VariadicMethod();

// Array with null value in constructor params
var variadicClassNullParams = new VariadicMethod(null);

// Array with one value in constructor params
var variadicClassOneParam = new VariadicMethod(1);

// Array with multiple values in constructor params
var variadicClassMultipleParams = new VariadicMethod(1, 2, 3, 4);
```

Fixes #153
Fixes #210
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/small Small work item – less than a day of effort language/dotnet Related to .NET bindings (C#, F#, ...) p0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants