Generics 在fsharp中,GetUnionCases可以替换为泛型吗?

Generics 在fsharp中,GetUnionCases可以替换为泛型吗?,generics,f#,discriminated-union,Generics,F#,Discriminated Union,我有以下功能。它用于将字符串的部分转换为区分的并集大小写。我有一些特定的代码,并且一直在重复和推广代码 let extractor ty (characters:list<char>)= //find the UnionCaseInfo for the named subtype of Token (ty) let uci = (Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<Token>) |

我有以下功能。它用于将字符串的部分转换为区分的并集大小写。我有一些特定的代码,并且一直在重复和推广代码

let extractor ty (characters:list<char>)= 
 //find the UnionCaseInfo for the named subtype of Token (ty)
 let uci = (Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof<Token>) |> Array.filter (fun u->u.Name.Equals(ty))).[0]
 //construct the chosen type from the passed in ElementValue (l)
 let maker l = Microsoft.FSharp.Reflection.FSharpValue.MakeUnion(uci,[|l|]) :?> Token
 match characters with
 | '('::t -> match t with
             | '!'::t2  -> match t2 with
                           | '\''::t3 -> let (entries,rest) = t3 |> entriesUntil '\''
                                             (maker(LiteralInsertion(stringOf entries)),trimHeadIf ')' rest)
             | '\''::t2 -> let (entries,rest) = t2 |> entriesUntil '\''
                           (maker(LiteralValue(stringOf entries)),trimHeadIf ')' rest)
             | '$'::t2  -> let (entries,rest) = t2 |> entriesUntil ')'
                           (maker(CaptureValue(stringOf entries)), rest)
             | _ -> failwith "Expecting !'Insertion', $Capture or 'Literal' following ("
 | l      -> (maker(DataCellValue),l)
let提取器ty(字符:列表)=
//查找令牌(ty)的命名子类型的UnionCaseInfo
设uci=(Microsoft.FSharp.Reflection.FSharpType.GetUnionCases(typeof)|>Array.filter(fun u->u.Name.Equals(ty))。[0]
//从传入的ElementValue(l)构造所选类型
让maker l=Microsoft.FSharp.Reflection.FSharpValue.MakeUnion(uci,[| l |]):?>Token
将字符与
|“(”::t->将t与匹配
|“!”::t2->将t2与匹配
|'\'':t3->let(entries,rest)=t3 |>entries直到'\''为止
(maker(LiteralInsertion(stringOf entries)),trimHeadIf')rest)
|'\'':t2->let(entries,rest)=t2 |>entries直到'\''为止
(maker(LiteralValue(stringOf entries)),trimHeadIf')rest)
|“$”::t2->let(entries,rest)=t2 |>entriesintill''
(制作人(CaptureValue(条目字符串)),其余)
|->failwith“Expecting!'Insertion',$Capture或“Literal”后跟(“
|l->(制造商(数据单元值),l)
我想做如下工作:

let extractor<'T> (characters:list<char>) = 
 etc...
 | l -> ('T(DataCellValue),l)
let提取器
所以,在你的情况下,它可能看起来像这样

type DU1 = 
    | LiteralInsertion
    | DataCellValue

type Token (du: DU1) = class end

let inline extractor ty (characters: list<char>) : 'a = 
    match characters with
    | '(' :: t -> (^a : (new : DU1 -> ^a) LiteralInsertion)
    | l -> (^a : (new : DU1 -> ^a) DataCellValue)

let x: Token = extractor "ty" []
类型DU1=
|文字分割
|数据单元值
类型令牌(du:DU1)=类结束
让内联提取器ty(字符:列表):'a=
将字符与
|“(”::t->(^a:(新:DU1->^a)文字插入)
|l->(^a:(新:DU1->^a)数据单元值)
设x:Token=提取器“ty”[]

如果没有帮助,请显示完整代码(我是指类型令牌和DU).

谢谢,我看不出如何将此应用到我的案例中。我的失败。但我的大脑被困在
提取程序上,这很有帮助:谢谢。我必须在上下文中尝试一下,看看我是否真的理解它。它似乎仍然适用于字符串名称而不是泛型。你认为泛型不可能吗?
type DU1 = 
    | LiteralInsertion
    | DataCellValue

type Token (du: DU1) = class end

let inline extractor ty (characters: list<char>) : 'a = 
    match characters with
    | '(' :: t -> (^a : (new : DU1 -> ^a) LiteralInsertion)
    | l -> (^a : (new : DU1 -> ^a) DataCellValue)

let x: Token = extractor "ty" []