开发者

Is it possible to get the name of a partially applied function?

开发者 https://www.devze.com 2023-02-17 23:35 出处:网络
Suppose I have defined a function: def hello(name:String, words:String) = println(\"Hello!\" + name + words)

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?


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.


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.


... 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消