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

Allow default values for generic parameters #239

Merged
merged 1 commit into from
Jun 14, 2022

Conversation

israellot
Copy link
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.

Copy link
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);

src/DynamicExpresso.Core/Parsing/Parser.cs Outdated Show resolved Hide resolved
generalize default expressions for generic methods
Copy link
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
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