Skip to content

Conversation

tmilnthorp
Copy link
Collaborator

IQuantity As/ToUnit methods would blindly cast an Enum to the unit type, even if it was the wrong unit type. Throwing an exception for wrong type.

IQuantity As/ToUnit methods would allow casting an Enum to the wrong unit type. Throw exception.
Copy link
Owner

@angularsen angularsen left a comment

Choose a reason for hiding this comment

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

It's good as-is, just some suggestions. Just merge when you are happy.

/// <inheritdoc />
double IQuantity.As(Enum unit)
{
if(!(unit is AmountOfSubstanceUnit))
Copy link
Owner

Choose a reason for hiding this comment

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

Optional, but you can rewrite this to latest c# for extra vanity points:

if (unit is AmountOfSubstanceUnit validUnit)
{
    return As(validUnit);
}

throw new ArgumentException("The given unit is not of type AmountOfSubstanceUnit.", nameof(unit));

Copy link
Owner

Choose a reason for hiding this comment

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

Also, we could add unit.GetType() to the exception message to give extra help figuring out what went wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I only thought you could do that with as. Sweet! And I agree, I updated the message.

Pretty :)
@tmilnthorp tmilnthorp merged commit 2e1c12e into angularsen:master Mar 1, 2019
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.

2 participants