如何使用 F# 获取作为参数传入函数的变量名称?
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
Is there any way in F# how to get a name of a variable passed into a function?
Example:
let velocity = 5
let fn v = v.ParentName
let name = fn velocity // this would return "velocity" as a string
Thank you in advance
EDIT:
Why this code does not work? It is matched as value, so I can not retrieve the "variable" name.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完成 Marcelo 的回答,是的,您可以使用引号来完成此任务:
正如您在代码中看到的,F# let-bound 顶部定义值(函数或变量)被实现为类的属性。
我再也找不到显示如何使用 C# 以函数方式重写一段 F# 代码的链接。看到代码,就很明显为什么需要
PropertyGet
模式。现在,如果您也想计算表达式,则需要安装 F# powerpack 并在您的项目中引用
FSharp.PowerPack.Linq
。它在
Expr
类上添加了一个EvalUntyped
方法。此操作,我会这样做:
如果您需要对实例的方法执行 显示
EvalUntyped
用法的代码片段,如果需要,您可以为Expr
添加显式类型参数和向下转型 (:?>
)需要保持类型安全:For completing Marcelo's answer, yes you can use quotations for this task:
As you can see in the code, F# let-bound top definition values (functions or variables) are implemented as properties of a class.
I can't find anymore the link that shows how a piece of F# code could be rewritten in a functional way with C#. Seeing the code, it becomes obvious why you need a
PropertyGet
pattern.Now if you want to evaluate the expression too, you will need to install F# powerpack and reference
FSharp.PowerPack.Linq
in your project.It adds an
EvalUntyped
method onExpr
class..If you need to do it for the method of an instance, here's how I would do it:
Just to come back on the code snippet showing usage of
EvalUntyped
, you can add an explicit type parameter forExpr
and a downcast (:?>
) if you want/need to keep things type-safe:您也许可以通过代码引用来实现此目的:
fn
函数将传递一个Expr
对象,该对象必须转换为Quotations.Var
(仅当您传递单个变量时才会如此)并提取Name
实例成员。You might be able to achieve this with code quotations:
The
fn
function will be passed anExpr
object, which it must cast toQuotations.Var
(which it will only be if you pass a single variable) and extract theName
instance member.基于之前的解决方案,我提出了一个更通用的解决方案,您可以在其中获取函数名称、lambda、值、属性、方法、静态方法、公共字段、联合类型:
Based on the previous solutions I came out with a more generic solution where you can get the name of functions, lambdas, values, properties, methods, static methods, public fields, Union types: