说,我有这样的事情。它不会编译,但是您可以看到我正在尝试执行的操作。我尝试过以各种方式在Google上搜索,但没有骰子。能做到吗?
let inline read (s:string) : ^x =
let parsed = (^x : (static member ofString: string -> ^x option) (s))
// this is the bit I'm not sure how do to. This doesn't compile.
// I'm trying to determine what the statically chosen type for ^x is.
let t = typeof<^x>
match parsed with
| Some y -> y
| None -> failwithf "can't parse %s into %s" s (t.Name)
这可以正常工作,并且您可以使用typeof
静态解析的参数-问题是解析器无法处理<^
,因为它也可以作为运算符进行解析。
您只需在周围添加空格即可轻松解决此问题^x
:
let t = typeof< ^x >
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句