gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:31:15 24 4
gpt4 key购买 nike

说吧,我有这样的事情。它无法编译,但您可以看到我正在尝试做什么。我试过用各种方式谷歌搜索,但没有骰子。这能做到吗?

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com