Mat*_*ews 5 generics f# pattern-matching
我这里有一个奇怪的。我想匹配泛型参数的类型。这是我到目前为止所拥有的:
open System.Reflection
type Chicken = {
Size : decimal
Name : string
}
let silly<'T> x =
match type<'T> with
| typeof<Chicken> -> printfn "%A" x
| _ -> printfn "Didn't match type"
enter code here
Run Code Online (Sandbox Code Playgroud)
我希望silly<'T>函数采用通用参数,然后匹配函数中的类型以确定输出。现在我收到一个关于不正确缩进的编译器错误。我很确定缩进没问题,但是编译器根本不喜欢我正在做的事情。想法?我有一个蛮力解决方法,但这种方法会简单得多。
我认为这就是你要找的:
let silly x =
match box x with
| :? Chicken as chicken -> printfn "is a chicken = %s %A" chicken.Name chicken.Size
| :? string as txt -> printfn "is a string = '%s'" txt
| :? int as n -> printfn "is an int = %d" n
| _ -> printfn "Didn't match type"
Run Code Online (Sandbox Code Playgroud)
像这样调用它:
silly "Hello"
silly 7
silly { Name = "Claudius" ; Size = 10m }
// is a string = 'Hello'
// is an int = 7
// is a chicken = Claudius 10M
Run Code Online (Sandbox Code Playgroud)
小智 2
这就是我一直在做的事情,不确定它是“最好的”,但它有效并且对我和我的团队有意义。
let silly<'T> x =
match typeof<'T> with
| t when t = typeof<TypeA> -> TypeASpecificFunction x
| t when t = typeof<TypeB> -> TypeBSpecificFunction x
| t when t = typeof<TypeC> -> TypeCSpecificFunction x
| _ -> printfn "Didn't match type"
Run Code Online (Sandbox Code Playgroud)
它需要您拥有的通用函数,这种方法不能直接在 typeof< x > 上工作,您必须执行 typeof<'T> 。