Skip to content

Conversation

zii-dmg
Copy link
Contributor

@zii-dmg zii-dmg commented Jan 14, 2017

Make comparisons by reference in TokenPartitioner. Fixes #398.

Make comparisons by reference in TokenPartitioner.
var values = nonOptions.Where(v => v.IsValue()).Memorize();
var errors = nonOptions.Except(values).Memorize();
var errors = nonOptions.Except(values, ReferenceEqualityComparer.Default).Cast<Token>().Memorize();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you casting back to Token because the comparer casts everything into Object? What if you made the comparer inherit IEqualityComparer<Token> and then called [Object.ReferenceEquals](https://msdn.microsoft.com/en-us/library/system.object.referenceequals(v=vs.110\).aspx) in the implementation rather than ==? That's a bit more explicit too, and wouldn't need a comment explaining that we're aiming for reference equality on line 16.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized that I can make other cast - (IEqualityComparer<Token>)ReferenceEqualityComparer.Default so Cast<Token> will not be needed. Or can make modify comparer to classic generic version - ReferenceEqualityComparer<T>. Don't want to inherit IEqualityComparer<Token>, it's too concrete for this task.

Will replace == with ReferenceEquals.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Generic sounds good to me. Any time we can make the type system work for us is a win in my book 😃

Copy link
Contributor Author

@zii-dmg zii-dmg Jan 16, 2017

Choose a reason for hiding this comment

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

But reference comparer doesn't have to be generic because it compares references anyway. I used this implementation from stackoverflow. As author says it's using contravariance feature so we can cast comparer to any type of IEqualityComparer<T> and we have one instance of comparer for any object type.

@zii-dmg
Copy link
Contributor Author

zii-dmg commented Jan 17, 2017

If you really want ReferenceEqualityComparer<T> I will make it, just say. Waiting for answer.

@nemec nemec merged commit 27243c4 into gsscoder:master Jan 18, 2017
@nemec
Copy link
Collaborator

nemec commented Jan 18, 2017

Looks good

@zii-dmg
Copy link
Contributor Author

zii-dmg commented Jan 20, 2017

I accidently removed FactAttribute from last test with merge. Can you restore it?

@nemec
Copy link
Collaborator

nemec commented Jan 20, 2017

Fixed

@zii-dmg zii-dmg deleted the same-value-option-arg branch January 20, 2017 15:44
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