Nullable trilogy part III: Nullable as type parameter in a generic class
Luca -
☕ 2 min. read
Another commonly asked question relates to the behavior of Nullable
Generics have a runtime representation and, as such, the compiler needs to generate IL that works whatever type is used at runtime to instantiate the generic class. This means that the compiler cannot call user defined operators, because at compile time it has no knowledge about them. The compiler doesn’t know at that point which type will be used to instantiate the generic class in the future. It cannot know that it has to generate code to call these user-defined operators.
In the same vein the compiler doesn’t know that a generic class will be instantiated with a Nullable
A similar behavior is observable with the following code using strings:
string s1 = Bob John;
string s2 = Bob;
Console.WriteLine(Equals(s1, s2 + John));
static bool Equals
return a == b;
}
This code would return false, because the ‘==’ operator for reference types gets invoked.
A case could be made that the compiler should generate IL to check the runtime type of the generic parameter and call the correct operator for at least some well-known types. This solution wouldn’t work for user defined operators as the compiler doesn’t know at compile time the set of types that could be used to instantiate a generic class. In general we don’t like to keep this sort of ‘lists of special things’ in the codebase, More importantly the solution would impose a somehow significant performance penalty at each use of the operator. This feeling of ‘hacking’ things and the significant performance problem convinced us not to implement this solution.
0 Webmentions
These are webmentions via the IndieWeb and webmention.io.