mercredi 30 mars 2016

Extract function name from a function

How can I create a function called getFuncName that takes a function of type (unit -> 'a) and returns its name.

I was talking to one of the C# devs and they said you could use the .Method property on a Func type as shown in an example here. I tried to convert this to F# for example convert (unit -> 'a) to a type Func<_> then call the property on it but it always returns the string "Invoke".

let getFuncName f =
    let fFunc = System.Func<_>(fun _ -> f())
    fFunc.Method.Name

let customFunc() = 1.0

// Returns "Invoke" but I want it to return "customFunc"
getFuncName customFunc

Is this possible?





Aucun commentaire:

Enregistrer un commentaire