-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Overload resolution to take into account Generic Parameters. #177
Comments
"For example the difference is the null checks in the case of a class type." This isn't the best example. Nothing stops you from having null checks for T = struct, they're no-op. |
Yes you could do an type check at runtime.
but the type check is additional code to run in the case of structure type. |
Well, I don't know VB but I did a quick test with your code and I don't see any null checks in the code generated from a call such as IsBetween(2,3,4). Additionaly, IsBetween(0, 0, 0) doesn't throw any exception which seems to contradict what you are saying. Am I missing something? |
Correct it doesn't throw (my bad), but it still does (three) redundant checks.
You get the error Wait a second it shoud be calling the first method? |
"but it still does (three) redundant checks" Nope, it doesn't. The JIT compiler knows that a value type is never null so all that null check stuff goes away. |
yes it does. Here's IL for
The constrained on class implementation.
and the constrained on structure implementation.
If we could overload (I'm using different methods) the IL would look some like.
Optimisations turned on
You see that the "null check" would be avoided. |
I'm talking about the JIT compiler and you're showing IL. The IL is irrelevant, the generated native code is what matters and that code doesn't contain the null checks when T is a value type. |
If I'm reading this right I think that you're proposing that generic methods could be overloaded on their constraints. While I've more than once found myself desiring this feature I don't think it's possible without CLR support. Because constraints aren't part of the signature I think it would be malformed metadata to have duplicate definitions (in the same way that in C# you can't overload on ref vs out because in IL they have the same representation). That's the real reason you can't define these methods today, not a shortcoming of overload resolution. @ljw1004 once showed a workaround to this by adding two different optional parameters on the end like this: Sub M(Of T As Structure)(obj As T, Optional __ As Integer = 0)
Sub M(Of T As Class)(obj As T, Optional __ As String = Nothing) Then you can call M and the compiler/overload resolution will actually take constraints into account even if you omit the second parameter. That's because VB considers constraints in the applicability of a method before overload resolution. It's still not the same as specializing the generic at runtime though but depending on your needs it's a functional workaround. Regards, -ADG |
This is a duplicate of bullet 2 of #250. |
Overload resolution should bind to the overload that is most specific including generic type parameters (constrained or not)
If
Foo
is called with an integer, then method 0 is used.If
Foo
is called with a string, then method 1 is used.This would allow the methods to me specialised for that type of object. For example the difference is the null checks in the case of a class type.
The text was updated successfully, but these errors were encountered: