[英]How to get the name of function argument in F#?
我可以编写一个函数来返回作为参数给出的函数的名称吗?
let funName f: string =
// returns the name of f.
例如,如果我将printfn
作为参数传递给funName,则返回“printfn”。
> funName printfn;;
val it : string = "printfn"
编辑:我想编写一个函数doc
,它返回与给定函数关联的XML文档。
let doc f = // returns the XML documentation of the function `f`.
要使用NuDoq之类的东西检索函数的摘要,我想知道函数的名称。
我无法想象为什么你会想要这样做,我不认为有办法用反射来做到这一点,但F#Code Quotations可能会让你到达那里。
open Microsoft.FSharp.Quotations
let rec funName = function
| Patterns.Call(None, methodInfo, _) -> methodInfo.Name
| Patterns.Lambda(_, expr) -> funName expr
| _ -> failwith "Unexpected input"
let foo () = 42
funName <@ foo @> // "foo"
但请注意,某些预定义的库函数具有不同的内部名称。
funName <@ printfn @> // "PrintFormatLine"
funName <@ id @> // "Identity"
请注意,从F#4.0开始,类类型可以自动引用它们的参数,与kaefer的答案相比,简化了调用站点:
open Microsoft.FSharp.Quotations
type DocumentGetter =
static member GetName([<ReflectedDefinition>]x:Expr<_->_>) =
match x with
| DerivedPatterns.Lambdas(_, Patterns.Call(_,methodInfo,_)) ->
methodInfo.Name
let f x y = x + y
DocumentGetter.GetName f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.