Skip to content

Conversation

jonsequitur
Copy link
Contributor

@jonsequitur jonsequitur commented Jan 8, 2020

This addresses #728, in part, by preventing null from being set as the default argument via the Argument.SetDefaultValue(object) overload. Additional design questions here.

public void SetDefaultValue(object value) => SetDefaultValue(() => value);
public void SetDefaultValue(object value)
{
if (value is null)
Copy link
Member

Choose a reason for hiding this comment

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

So I don't get this block here, since this implies calling Argument.SetDefaultValue((object)null) would throw, but Argument.SetDefaultValue(() => null) would be fine, even though I'd expect those to meant the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I think the API needs to change so that intention is more clear between:

  • there is a default value, and it is null, and
  • there is no default value.

The latter is not currently representable in the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the latest commit, I've differentiated SetDefaultValue from SetDefaultValueFactory and added tests to clarify the behaviors. Do you think this helps with the ambiguity?

These APIs still feel fairly provisional though, and a few issues (#581, #704, #723) have pointed toward the need for a more holistic approach to default values, custom conversion, and validation. I won't try to address this here but I suspect it might result in SetDefaultValue and SetDefaultValueFactory becoming obsolete.

jonsequitur and others added 3 commits January 8, 2020 11:05
Co-Authored-By: Kevin B <Keboo@users.noreply.github.com>
@jonsequitur jonsequitur merged commit f5ee0ab into dotnet:master Jan 12, 2020
}

public Argument(Func<T> defaultValue) : this()
{
SetDefaultValue(() => defaultValue());
SetDefaultValueFactory(() => defaultValue());
Copy link
Member

Choose a reason for hiding this comment

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

@jonsequitur: it seems the original bug I hit isn't being fixed here: if I pass null in for defaultValue, this will still call SetDefaultValueFactory and ultimately null ref once the property tries being accessed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I realized I made this change on another branch where I'm refactoring toward fixing #723. These APIs may change significantly by the time that's done but the refactorings are worth merging earlier which will actually fix this issue.

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