在F#中对对象调用方法的简写

编程入门 行业动态 更新时间:2024-10-08 08:23:03
本文介绍了在F#中对对象调用方法的简写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以像这样生成函数":

Is there a way to "generate" functions like this one:

fun x -> x.ToString

我希望能够将实例方法转换为以"this"为参数的静态方法,例如:

I would like to be able to turn an instance method into a static method which takes "this" as a parameter, like so:

items |> Seq.filter my_predicate |> Seq.map Object.ToString

推荐答案

在F#集线器上已对此进行了多次讨论.参见例如实例方法作为函数.据我所知,这是一个非常棘手的问题,因此,没有计划在F#的第一个版本中有这样的东西,但是最终拥有这样的东西将是很棒的:-).

this has been discussed several times on the F# hub. See for example instance methods as functions. This is quite tricky problem, so there are no plans to have something like this in the first version of F# as far as I know, but it would be great to have something like that eventually :-).

您可以采取的另一种解决方法是将静态成员添加为F#中的扩展方法:

Another workaround that you could do is to add static member as an extension method in F#:

type System.Object with static member ObjToString(o:obj) = o.ToString() open System [ 1 .. 10 ] |> Seq.map Object.ObjToString;;

但这有点丑陋.另外,似乎仅当您对方法使用不同的名称时,此方法才有效.我想F#不允许您使用扩展方法重载现有方法,而总是偏向于固有方法.

But that is a bit ugly. Also, it seems that this works only if you use different name for the method. I guess that F# doesn't allow you to overload existing method with an extension method and always prefer the intrinsic one.

更多推荐

在F#中对对象调用方法的简写

本文发布于:2023-10-19 01:54:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简写   中对   对象   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!