Mil*_*oDC 9 generics f# types pattern-matching
我有一个带泛型参数的函数,在其中我需要执行两个函数中的一个,具体取决于参数的类型.
member this.Load<'T> _path =
let hhType = typeof<HooHah>
match typeof<'T> with
| hhType -> this.LoadLikeCrazy<'T> _path
| _ -> this.LoadWithPizzaz<'T> _path
Run Code Online (Sandbox Code Playgroud)
....其中LoadLikeCrazy和LoadWithPizzaz都返回'T.
VS通知我,通配符的情况永远不会被执行,因为我显然在编译时得到泛型的类型,而不是运行时的实际类型.我该怎么做?
nyi*_*ann 13
在您的代码中,第一个模式匹配规则不会将typeof <'T>与hhType进行比较.相反,它将引入一个名为hhType的新值.这就是你得到警告的原因.我宁愿修改这样的代码.
member this.Load<'T> _path =
match typeof<'T> with
| x when x = typeof<HooHah> -> this.LoadLikeCrazy<'T> _path
| _ -> this.LoadWithPizzaz<'T> _path
Run Code Online (Sandbox Code Playgroud)