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

标签 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)

最佳答案

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

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

let t = typeof< ^x >

关于f# - 我可以获得静态解析类型参数的类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45110854/

相关文章:

linux - Monodevelop 上的 F# : compiler doesn't understand valid Linux file path

asp.net-mvc-3 - 我应该在 MVC3 和 ASP.net 中尝试 F# 吗?

random - 这是一个适当的线程安全随机包装器吗?

.net - 为什么F#中有这么多不同类型的 `map`函数

c# - CS1501 : No overload for method 'ToString' takes 0 arguments?

types - F# 强制转换/将自定义类型转换为原始类型

postgresql - 使用 F# 中的 Npgsql 的用户定义的 postgresql 类型

struct - 结构构造函数中的 f# NativePtr.stackalloc

concurrency - MailboxProcessor 类型是锁的替代品吗?

for-loop - For循环不推断无符号整数