6

F# 中有什么方法可以获取传递给函数的变量的名称吗?

例子:

let velocity = 5
let fn v = v.ParentName
let name = fn velocity // this would return "velocity" as a string

先感谢您

编辑:

为什么这段代码不起作用?它作为值匹配,因此我无法检索“变量”名称。

type Test() =
  let getName (e:Quotations.Expr) =
    match e with
      | Quotations.Patterns.PropertyGet (_, pi, _) -> pi.Name + " property"
      | Quotations.Patterns.Value(a) -> failwith "Value matched"
      | _ -> failwith "other matched"
  member x.plot v = v |> getName |> printfn "%s"

let o = new Test()

let display () =
  let variable = 5.
  o.plot <@ variable @>

let runTheCode fn = fn()

runTheCode display
4

3 回答 3

12

为了完成 Marcelo 的回答,是的,您可以为此任务使用引号:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

let velocity = 5

let fn (e:Expr) =
  match e with
    | PropertyGet (e, pi, li) -> pi.Name
    | _ -> failwith "not a let-bound value"

let name = fn <@velocity@> 

printfn "%s" name

正如您在代码中看到的那样,F# let-bound 顶级定义值(函数或变量)被实现为类的属性。

我再也找不到显示如何使用 C# 以功能方式重写一段 F# 代码的链接。PropertyGet看到代码,你为什么需要一个模式就很明显了。

现在,如果您也想评估表达式,则需要在项目中安装F# powerpack和引用FSharp.PowerPack.Linq

EvalUntyped在类上添加了一个方法Expr..

open Microsoft.FSharp.Linq.QuotationEvaluation

let velocity = 5

let fn (e:Expr) =
  match e with
    | PropertyGet (eo, pi, li) -> pi.Name, e.EvalUntyped
    | _ -> failwith "not a let-bound value"

let name, value = fn <@velocity@> 

printfn "%s %A" name value

如果您需要为实例的方法执行此操作,我将这样做:

let velocity = 5

type Foo () =
  member this.Bar (x:int) (y:single) = x * x + int y

let extractCallExprBody expr =
  let rec aux (l, uexpr) =
    match uexpr with
     | Lambda (var, body) -> aux (var::l, body)
     | _ -> uexpr
  aux ([], expr)

let rec fn (e:Expr) =
  match e with
    | PropertyGet (e, pi, li) -> pi.Name
    | Call (e, mi, li) -> mi.Name
    | x -> extractCallExprBody x |> fn
    | _ -> failwith "not a valid pattern"

let name = fn <@velocity@> 
printfn "%s" name

let foo = new Foo()

let methodName = fn <@foo.Bar@>
printfn "%s" methodName

只是回到显示用法的代码片段,如果您想要/需要保持类型安全EvalUntyped,您可以添加显式类型参数 forExpr和向下转换 ( )::?>

let fn (e:Expr<'T>) = 
  match e with
    | PropertyGet (eo, pi, li) -> pi.Name, (e.EvalUntyped() :?> 'T)
    | _ -> failwith "not a let-bound value"
    
let name, value = fn <@velocity@> //value has type int here
printfn "%s %d" name value
于 2010-04-29T11:07:23.880 回答
1

您可以通过代码引用来实现这一点:

let name = fn <@ velocity @>

fn函数将被传递一个Expr对象,它必须强制转换为Quotations.Var(仅当您传递单个变量时才会如此)并提取Name实例成员。

于 2010-04-29T09:49:10.073 回答
0

基于之前的解决方案,我提出了一个更通用的解决方案,您可以在其中获取函数、lambda、值、属性、方法、静态方法、公共字段、联合类型的名称:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

let cout (s:string)= System.Console.WriteLine (s)

let rec getName exprs =
    let fixDeclaringType (dt:string) =
        match dt with
        | fsi  when fsi.StartsWith("FSI_") -> "Fsi"
        | _ -> dt
    let toStr (xDeclType: System.Type) x = sprintf "%s.%s" (fixDeclaringType xDeclType.Name)  x
    match exprs with
    | Patterns.Call(_, mi, _) -> 
        toStr mi.DeclaringType mi.Name
    | Patterns.Lambda(_, expr) -> 
        getName expr
    | Patterns.PropertyGet (e, pi, li) ->  
        toStr pi.DeclaringType pi.Name
    | Patterns.FieldGet (_, fi) -> 
        toStr fi.DeclaringType fi.Name
    | Patterns.NewUnionCase(uci, _) -> 
        toStr uci.DeclaringType uci.Name
    | expresion -> "unknown_name"


let value = ""
let funcky a = a
let lambdy = fun(x) -> x*2
type WithStatic = 
    | A | B
    with static member StaticMethod a = a
let someIP = System.Net.IPAddress.Parse("10.132.0.48")


getName <@ value @> |> cout
getName <@ funcky @> |> cout
getName <@ lambdy @> |> cout
getName <@ WithStatic.A @> |> cout
getName <@ WithStatic.StaticMethod @> |> cout
getName <@ someIP.MapToIPv4 @> |> cout  
getName <@ System.Net.IPAddress.Parse @> |> cout  
getName <@ System.Net.IPAddress.Broadcast @> |> cout  
于 2017-07-14T09:12:19.000 回答