Closed NinoFloris closed 2 years ago
So you want to be able to freely do x :?> 'T
when the type of variable x
is not a supertype of 'T
? That sounds quite dodgy to me 😕 I agree that the error messages could be better, but I don't think that letting people do it willy-nilly is the correct fix.
@Tarmil nothing but obj is a super type of a generic parameter, and that's not by virtue of that fact being obvious or 'more principled'. Hence why it's allowed in C# too, needing to box first has unnecessary friction while adding zero conceptual clarity.
nothing but obj is a super type of a generic parameter, and that's not by virtue of that fact being obvious or 'more principled'.
That's exactly what it is though... You don't know anything about a generic parameter except maybe the constraints you put on it. When a function takes unconstrained 'T
, it means "I don't care what type you give me, I'll treat them all the same". I'd rather not make it easier to break this principle.
I agree with @Tarmil's analysis here. I feel extending the range of things that can be used as input for the type tests leads to weaker code. I'd prefer to stick to the F# 1.0 decisions here.
I propose we allow type tests like
x :? 'T
or casts likex :?> 'T
, which would then autobox. Today doing tests or casts like this requires manually boxingx
before doing the test/cast, C# does this for you which saves some noise around an already noisy operation.Today many of the errors that can be returned when you try to do so without boxing are fairly poor and do not help the user understand an explicit
box
might be what they need.Or
For inheritable types it does work but you get a compiler warning
This should also naturally extend to tests and casts that involve a generic value and a type parameter, again as in C#.
C# expanded their spec in 7.1 for pattern matching to allow anything they already allowed in
x as T
https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.1/generics-pattern-match.mdPros and Cons
The advantages of making this adjustment to F# are: Type tests and casts that are easier to use and have less sharp edges.
The disadvantages of making this adjustment to F# are: It may cause people to use them more?
Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: https://github.com/fsharp/fslang-suggestions/issues/828
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply: