Repository navigation
Implement CanCastTo - #1183
Implement CanCastTo#1183
Conversation
Type system support for answering questions about whether a type can be cast to another type, along with unit tests. I largely ported the C++ code from the Project N compiler to C#. The only divergence is in the handling of casting arrays to System.Array and array interfaces. We won't use the "treat it as `Array<T>`" wart in CoreRT. The unit tests are all new because there were none. I'm also adding a few convenience properties to the type system, stolen from the Project N compiler type system: * `Variety` - an enum on `TypeDesc` that can easily tell what kind of type it is (Array? DefType? Generic parameter?...) * A few new `HasXXX` and `IsXXX` shortcuts on `GenericParameterDesc`. Makes code much more readable. * `IsGenericParameter` and `IsDefType` on `TypeDesc`. This brings the existing `IsArray`, `IsPointer` etc. to completion. * `HasVariance` on `TypeDesc`. This is a cached answer to whether a type has variance. It lets us quickly decide if we need to do the slow things.
21340ae to
8de78cb
Compare
|
@davidwrighton PTAL |
| break; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Could you pull this loop into a ComputeHasVariance function that is virtual? I believe for InstantiatedType it should return GetTypeDefinition().HasVariance, and the implementation on TypeDesc it should contain the code here.
|
Other than the one comment, looks good to me |
|
I wish used the same structure as the other type cast implementations in the repo ( |
|
You can delete |
|
This is a side note but I've seen this pattern used so much in the CoreRT C# code that I'd like to know if this is a conscious decision as in my opinion it hurts the ease of reading and understanding of the code. The issue is the extensive use of extension methods and partial classes. I can understand somewhat the use of partial class due to way things are built and shared. However, for extension methods I really do not understand. When I look at a type in a text editor, I really want to see all the available methods and not to have to look everywhere in the source base. Extensions methods were introduced as a way to augment types that were sealed from the client point of view, but here we are in full control of the type systems and we can easily add methods to type. In this particular case, it seems that we could benefit from dynamic binding instead of if / switch statements to pick up the right implementation of the CanCast... method. Sorry for the digression. |
I looked at that, but it feels like the compiler's needs for type casting are slightly different than the runtime needs (e.g. the handling of |
|
@manu-silicon Both extension methods and partial classes serve the same purpose - provide extensibility points and an a la carte access to various type system features. This is a conscious decision and a design goal. I would say IntelliSense does a pretty good job helping with discoverability. For the "I want to see all the methods on this type in a single file" argument, I can counter with "I want to know how casting works without having to hunt down virtual overrides in 5 different files"... |
The overall structure of the algorithm (names of methods, shape of the code, etc.) does not need to be different to handle these extra cases ... they are just extra paths in a few spots. (I am ok with what you got ... it is just that I had to chase bugs in casting algorithm implementations number of times, and I always scratched my head on why each implementation has to always be so different.) |
|
I understand the argument of putting stuff together, however, I believe that it goes against basic OO design principles where dynamic dispatch is replaced by a sequence of if/switch because of the usage of extension methods. VS IntelliSense helps a bit to understand the final end result design but the tooling is still not perfect at giving users a full view of the OO hierarchy and classes interactions. Anyway this was off topic. Thanks for your reply. |
| [Fact] | ||
| public void TestArrayCasting() | ||
| { | ||
| TypeDesc intType = _context.GetWellKnownType(WellKnownType.Int32); |
There was a problem hiding this comment.
Shouldn't you add some test for casting between array of int to array of uint and vice-versa?
There was a problem hiding this comment.
See TestSameSizeArrayTypeCasting above :)
|
LGTM otherwise |
* Get rid of Variety * Introduce TypeFlag.SzArray (and fix up places that need to handle it)
| } | ||
| } | ||
|
|
||
| public bool IsReferenceType |
There was a problem hiding this comment.
ECMA 335 has defined meaning for "reference type" that is different from this one (it even includes unmanaged pointers). IsObjRef or something similar would be better here. Also, I think it should return true for interfaces as well.
|
LGTM otherwise |
Type system support for answering questions about whether a type
can be cast to another type, along with unit tests.
I largely ported the C++ code from the Project N compiler to C#. The only
divergence is in the handling of casting arrays to System.Array and array
interfaces. We won't use the "treat it as
Array<T>" wart in CoreRT.The unit tests are all new because there were none.
I'm also adding a few convenience properties to the type system, stolen
from the Project N compiler type system:
SzArrayflag toTypeFlagsHasXXXandIsXXXshortcuts onGenericParameterDesc. Makes code much more readable.IsGenericParameterandIsDefTypeonTypeDesc. This brings the existingIsArray,IsPointeretc. to completion.HasVarianceonTypeDesc. This is a cached answer to whether a type has variance. It lets us quickly decide if we need to do the slow things.