说吧,我有这样的事情。它无法编译,但您可以看到我正在尝试做什么。我试过用各种方式谷歌搜索,但没有骰子。这能做到吗?
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 >
关于f# - 我可以获得静态解析类型参数的类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45110854/