Skip to content

Allow default values for generic parameters#239

Merged
davideicardi merged 1 commit intodynamicexpresso:masterfrom
israellot:master
Jun 14, 2022
Merged

Allow default values for generic parameters#239
davideicardi merged 1 commit intodynamicexpresso:masterfrom
israellot:master

Conversation

@israellot
Copy link
Copy Markdown
Contributor

@israellot israellot commented Jun 10, 2022

public T MethodWithGenericParamAndDefault<T>(T a, T b = default) { return a; }
The above method throws when parameter b is not set. This PR does a best effort in searching for a previously specified parameter of the same type to construct the default expression for this case.

When invoking MethodWithGenericParamAndDefault(1) it uses the knowledge the param a has the same type int as b.

Comment thread src/DynamicExpresso.Core/Parsing/Parser.cs Outdated
Copy link
Copy Markdown
Contributor

@metoule metoule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good, but I'd add the following test to be sure:

public List<T> MethodWithGenericParamAndDefault<T>(T a, List<T> b = default)
{
   return b;
}

var b = MethodWithGenericParamAndDefault(1);
Assert.Null(b);

Comment thread src/DynamicExpresso.Core/Parsing/Parser.cs Outdated
generalize default expressions for generic methods
Copy link
Copy Markdown
Contributor

@metoule metoule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks!

Copy link
Copy Markdown
Member

@davideicardi davideicardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@davideicardi davideicardi merged commit b502ef3 into dynamicexpresso:master Jun 14, 2022
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

Successfully merging this pull request may close these issues.

3 participants