是否可以获得部分应用函数的名称?

发布于 2024-10-24 00:25:01 字数 337 浏览 1 评论 0原文

假设我定义了一个函数:

def hello(name:String, words:String) = println("Hello!" + name + words)

然后我定义了一个部分函数:

def p = hello _

打印p,显示:

(String, String) => Unit = <function2>

没有显示函数名。是否可以从分部函数p中获取原始方法名hello

Suppose I have defined a function:

def hello(name:String, words:String) = println("Hello!" + name + words)

Then I defined a partial function:

def p = hello _

Print p, displayed:

(String, String) => Unit = <function2>

There's no function name displayed. Is it possible to get the original method name hello from the partial function p?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-10-31 00:25:01

最近,邮件列表上有一些关于一种语言构造的讨论,该语言构造允许您识别当前使用的方法。它的名称类似于 thisMethod 对方法的作用基本上与 this 对类实例的作用相同。

我很想知道它如何与函数交互(这是与方法不同的概念)。您的 p 是通过部分应用 hello 方法创建的匿名函数(同样,您在这里需要小心,“部分应用”函数是与 PartialFunction 非常不同),然后调用的实际方法将是在此函数对象上的 apply,并且关于 thisMethod 的方式有多种可能性。 无论发生什么情况

p 只是一个对象引用,不要期望永远能够将其作为名称进行访问。

There has been some discussion on the mailing lists recently about a language construct that allows you to identify the current method that you're in. It would be called something like thisMethod and would essentially do for methods what this does for class instances.

I'd be interested to see how this interacts with functions (which are a different concept from methods). Your p is an anonymous function created from partially applying the hello method (again, you need to be careful here, a "partially applied function is a very different thing from a PartialFunction). The actual method then invoked would be apply on this function object, and there are several possibilities as to how thisMethod could behave in such a case.

Whatever happens, p is just an object reference, don't expect to ever be able to access it as a name.

剩余の解释 2024-10-31 00:25:01

不。

这是我很想拥有的功能,但它存在严重的概念问题,例如,当同一个函数被赋予两个名称时会发生什么……它仍然应该是同一个函数,不是吗?

更新以回应评论:

def p = hello _
def q = p

q 的名字是什么? p?或者你好?或打印?我很难想象一个简单、一致且有用的解决方案。

No.

Its a feature I'd love to have, but it has serious conceptual problems like, what should happen, when the same function is given two names ... it should still be the same function, shouldn't it?

update in response to comment:

def p = hello _
def q = p

What is the name of q? p? or hello? or println? I have a hard time imagining a solution that is simple, consistent and usefull.

弥繁 2024-10-31 00:25:01

...这实际上是一个“部分应用的函数”,它是通过调用 hello 函数而不应用参数来匿名创建的。它没有明确的名称可显示。

... that is actually a "partially applied function" that is created anonymously by calling your hello function without the parameters being applied. It does not have an explicit name to show.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文