I got a question, mostly because I found a case where classes and subtyping was needed.
let say we got some function
let IsA<'item, 'input> (subject: 'input) =
match subject with
| :? 'item as item -> Some item
| _ -> None
is there a way to constraint the generic types to allow this.
The alternative is to do a IsA function for all subtype constrains.
This is a dummy case, but it clearly describes what I need to do.
I'm not exactly sure what your motivation here is. You can certainly
boxthesubjectbefore pattern matching on the value:This lets you call
IsAon various things, including the ones that make sense (but also, because of the boxing, some that cannot possibly make sense). You need to constrain the'itemtype parameter, either by explicitly sayingIsA<Cat, _>or by having an annotation elsewhere:In principlie, it would be nice if the last case with
Randomwas not allowed. Unfortunately, you cannot specify a constraint for this though. The following does not work:The issue is discussed, for example, here: How to constrain one type parameter by another