Generics 有没有办法将泛型类型约束为一组类型的成员?

Generics 有没有办法将泛型类型约束为一组类型的成员?,generics,f#,Generics,F#,我有以下记录 type RecordPath<'a,'b> = { Get: 'a -> 'b Path:string } 然后对在记录路径上运行的所有函数使用SRTPs,但是从技术上讲,有人决定扩展我不想使用该扩展方法支持的某种类型是可能的(尽管不太可能) 那么,在F#?中,使用私有构造函数的类是实现这一点的唯一方法吗?下面的内容似乎满足了我的要求 module RecordPath = type RecordPath<'a, 'b> =

我有以下记录

type RecordPath<'a,'b> = {
    Get: 'a -> 'b
    Path:string
}
然后对在
记录路径上运行的所有函数使用SRTPs,但是从技术上讲,有人决定扩展我不想使用该扩展方法支持的某种类型是可能的(尽管不太可能)


那么,在F#?

中,使用私有构造函数的类是实现这一点的唯一方法吗?下面的内容似乎满足了我的要求

module RecordPath =
    type RecordPath<'a, 'b> = private {
        Get: 'a -> 'b
        Path:string
    }
    with 
        static member Create (f: 'a -> string) = {Get = f; Path = "not important for this demo"}
        static member Create (f: 'a -> int) = {Get = f; Path = "not important for this demo"}
        static member Create (f: 'a -> DateTime) = {Get = f; Path = "not important for this demo"}
模块记录路径=
类型RecordPath=private{
得到:'a->'b
路径:字符串
}
具有
静态成员Create(f:'a->string)={Get=f;Path=“对于此演示不重要”}
静态成员Create(f:'a->int)={Get=f;Path=“对于此演示不重要”}
静态成员Create(f:'a->DateTime)={Get=f;Path=“对于此演示不重要”}

在接受这个答案之前,我会稍等片刻,以防有人想出更好的方法。

这正是我在看到你的问题和答案之后的建议:-)。我认为这可能是最好的方法。我有一个类似的场景,我做了完全相同的事情。
module RecordPath =
    type RecordPath<'a, 'b> = private {
        Get: 'a -> 'b
        Path:string
    }
    with 
        static member Create (f: 'a -> string) = {Get = f; Path = "not important for this demo"}
        static member Create (f: 'a -> int) = {Get = f; Path = "not important for this demo"}
        static member Create (f: 'a -> DateTime) = {Get = f; Path = "not important for this demo"}