Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of function argument in F#?

Tags:

reflection

f#

Can I write a function which returns the name of the function given as the argument?

let funName f: string =
   // returns the name of f.

For example, if I pass printfn as an argument to funName, it returns "printfn".

> funName printfn;;
val it : string = "printfn"

EDIT: I wanted to write a function doc which returns the XML documentation associated with the given function.

let doc f = // returns the XML documentation of the function `f`.

To retrieve the summary of the function using something like NuDoq, I wanted to know the name of the function.

like image 898
Kwang Yul Seo Avatar asked Apr 13 '15 06:04

Kwang Yul Seo


2 Answers

I cannnot imagine why you would want to do this and I do not think that there's a way to do this with reflection, but F# Code Quotations might get you halfway there.

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"

But note that certain predefined library functions have a divergent internal name.

funName <@ printfn @>   // "PrintFormatLine"
funName <@ id @>        // "Identity"
like image 94
kaefer Avatar answered Sep 22 '22 02:09

kaefer


Note that as of F# 4.0, class types can automatically quote their arguments, simplifying the call site compared to kaefer's answer:

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
like image 40
kvb Avatar answered Sep 22 '22 02:09

kvb