我认为你不能or
在两个约束之间放置一个。通常当我需要像constraint1或constraint2或...或constraintN这样的东西时,我所做的是创建重载:
open System.Collections.Generic
// unconstrained function
let getOrDefaultG (d: IDictionary< _ , 'T>) key =
match d.TryGetValue(key) with
| true, v -> v
| _ -> Unchecked.defaultof<'T>
// individually constrained
let getOrDefaultS<'K,'T when 'T :struct> (d:IDictionary<'K,'T>) = getOrDefaultG d
let getOrDefaultN<'K,'T when 'T :null > (d:IDictionary<'K,'T>) = getOrDefaultG d
// overloads
type GetOrDefault = GetOrDefault with
static member ($) (GetOrDefault, d) = fun dummyArg -> getOrDefaultS d
static member ($) (GetOrDefault, d) = fun (dummyArg:unit) -> getOrDefaultN d
// the desired function
let inline getOrDefault d key = (GetOrDefault $ d) () key
注意: dummyArg 是我用来创建两个不同签名并使其编译的技巧。