degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

Implicit type conversion for tuples #776

Open degory opened 3 years ago

degory commented 3 years ago

The CLR doesn't natively support covariance on element types of System.ValueTuple`N, but we want to be able to assign, for example, (int,int) to (object,object). This will require emitting type conversion code wherever a tuple with more derived element types is consumed. Since the tuple's constructor type parameters are naturally contravariant, all that's needed is to destructure the tuple, and construct a new one of the target type, passing in the elements to the constructor of the target tuple type as parameters (and hope the JIT compiles that all down to nothing...)

degory commented 3 years ago

This is a similar problem to autoboxing - the boxer already hooks into all places where conversions from value types to objects might be needed, and tuple type conversion (and any other auto type conversion we may want to apply in future) can probably be applied in the same place.