As already indicated, there is no way to do this directly (pattern matching can only bind values, but cannot bind new type variables). In addition to the (more general) kvb workaround, you can use the fact that all collections implement non-general IEnumerable , so you can check this type:
match box value with | :? System.Collections.IEnumerable as l when // assumes that the actual type of 'l' is 'List<T>' or some other type // with single generic type parameter (this is not fully correct, because // it could be other type too, but we can ignore this for now) typedefof<SomeType>.IsAssignableFrom (value.GetType().GetGenericArguments().[0]) -> l |> Seq.cast<SomeType> | _ -> failwith "doesn't match"
The code checks to see if this value is not generic IEnumerable and whether the type parameter is a subtype of SomeType . In this case, we got a list of some derived type, so we can apply it to a sequence of SomeType values โโ(this is slightly different from working with a list of values โโof derived types, but it does not matter for practical purposes).
Tomas petricek
source share