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

Improve operator overloading logic #1011

Merged
merged 1 commit into from Nov 19, 2021

Conversation

lahma
Copy link
Collaborator

@lahma lahma commented Nov 19, 2021

Hopefully the overload selection logic now a bit easier to read and maintain.

  • cleanup and refactor code
  • use struct record instead of value tuples and custom code
  • prefer more specific types over object
  • prefer CLR array against JS array match over changing types
  • as TryOperatorOverloading is Try pattern, it should not throw on conversion error, just return false

fixes #1004
fixes #1008

* cleanup and refactor code
* use struct record instead of value tuples and custom code
* prefer more specific types over object
* prefer CLR array against JS array match over changing types
@lahma lahma added the interop label Nov 19, 2021
@lahma lahma marked this pull request as ready for review November 19, 2021 14:40

var castOperator = _knownCastOperators.GetOrAdd(key, _ =>
valueType.GetOperatorOverloadMethods()
.Concat(type.GetOperatorOverloadMethods())
.FirstOrDefault(m => type.IsAssignableFrom(m.ReturnType)
&& (m.Name == "op_Implicit" || m.Name == "op_Explicit")));
.FirstOrDefault(m => type.IsAssignableFrom(m.ReturnType) && m.Name is "op_Implicit" or "op_Explicit"));
Copy link
Owner

Choose a reason for hiding this comment

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

🤕

#else
private static readonly ConcurrentDictionary<string, MethodDescriptor> _knownOperators = new ConcurrentDictionary<string, MethodDescriptor>();
#endif
private readonly record struct OperatorKey(string OperatorName, Type Left, Type Right);
Copy link
Owner

Choose a reason for hiding this comment

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

@lahma lahma merged commit 1cf0e2b into sebastienros:main Nov 19, 2021
@lahma lahma deleted the string-operator-overload branch November 19, 2021 18:33
Comment on lines +878 to +882
if (method.Parameters.Length == 0 && arguments.Length == 0)
{
// perfect
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean method will match perfectly if argument lengths are the same, regardless of the argument types? Isn't this wrong, or am I missing something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Method has no parameters and JS call has no arguments? What is there to match?

Copy link
Contributor

Choose a reason for hiding this comment

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

I had a little brain fart. I saw another thing when reviewing.

Sorry for the trouble. Thank you.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No worries, I always appreciate reviews and and extra pair of eyes 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong overloaded method is called under certain circumstances Operator overloading and string concatenation
3 participants