我可以获得静态解析类型参数的类型吗?

Dan*_*iel 5 f#

说,我有这样的事情.它没有编译,但你可以看到我正在尝试做什么.我试图谷歌每个方向,但没有骰子.可以这样做吗?

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)

Tom*_*cek 9

这工作正常,您可以使用typeof静态解析的参数 - 问题是解析器无法处理<^,因为它也可以解析为运算符.

您只需添加空格即可轻松解决此问题^x:

let t = typeof< ^x >
Run Code Online (Sandbox Code Playgroud)

  • 噢,这真令人沮丧!花了我很长时间研究,然后stackoverflow,只是为了找出我需要一个空白区域! (2认同)