What does the name of a function denote?

I’m trying to provide an answer to my question with an analogy to class names. Do you agree with my conclusion?

In a class declaration, the identifier provided with the class introduces the name of a new class type. The denotation (meaning, purpose) of the class name can be concluded from the context of its use. Since a class name can be used anywhere in the code where a class type is appropriate, a class name represents a type. A class name denotes a type.

The class literal ClassName::class returns an instance of KClass, which represents a runtime reference to the class type.

What about function declarations? The name does not introduce a new type. Functions are instances of Function. The name provided in the function declaration does not refer to an instance of a Function either.

Let’s take an example:

fun inc(n: Int): Int = n + 1

A function name (here inc) is used to create a callable reference (e.g. ::inc) or to call an instance of the function like inc(3); I guess that inc(3) is just syntactic sugar for (::inc)(3). That’s the context of use of function names. The callable reference ::funName creates an instance of KFunction (here KFunction1), which represents a runtime reference to the function declared under this name, very much like ClassName::class represents a runtime reference to the class type. That means that the function name represents a function factory for instances of functions implementing the declared behaviour under that name.

In other words: The name of a function denotes a function factory.

Do you agree?

We are going deep into the woods here, but I’ll explain how I see what happens in reality for the JVM:

if you call inc(3) you are directly invoking the function using invokeVirtual. A function value (for storing or passing as parameter) is actually implemented as a class or object (depending on captures) that has an invoke method that is implemented to call the referenced function. This object/class is reference site specific (there could be multiple equal classes as a result – this is a compiler implementation limitation).

If you were to call (::inc)(3) this could be optimized at compiler level (JVM is another matter), but likely is not. Even conceptually, for a free function the name is not a factory as there only needs to be 1 instance. For a method the object reference must be captured which conceptually leads to a functor (function object) factory that takes the object as reference and then calls the body. (Ignoring the chicken-egg issues with this view).

1 Like

I think I get your point.

Maybe I was overcomplicating things, maybe its as simple as this: A function name is just the name of a function. If used in a call context like in funcName(...) it refers to a function call, if used as ::funcName it refers to a callable function reference. I tried to explain this to myself here: