说,我有这样的事情.它没有编译,但你可以看到我正在尝试做什么.我试图谷歌每个方向,但没有骰子.可以这样做吗?
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)
Run Code Online (Sandbox Code Playgroud)
这工作正常,您可以使用typeof静态解析的参数 - 问题是解析器无法处理<^,因为它也可以解析为运算符.
您只需添加空格即可轻松解决此问题^x:
let t = typeof< ^x >
Run Code Online (Sandbox Code Playgroud)