4 CHAPTER 11. OVERLOAD RESOLUTION
• A fully-qualified call without receiver: package.foo();
• A call with an explicit receiver: a.foo();
• An infix function call: a foo b;
• An overloaded operator call: a + b;
• A call without an explicit receiver: foo().
Although syntactically similar, there is a difference between the first two kinds
of calls: in the first case,
package
is a name of a Kotlin package, while in the
second case a is a value or a type.
For each of these cases, a compiler should first pick a number of overload
candidates, which form a set of possibly intended callables (overload candidate
set, OCS), and then choose the most specific function to call based on the types
of the function and the call arguments.
Important: the overload candidates are picked before the most
specific function is chosen.
11.1.3 Callables and invoke convention
A callable X for the purpose of this section is one of the following:
• Function-like callables:
– A function named X at its declaration site;
– A constructor of a type named X at its declaration site;
–
Any of the above named
Y
at its declaration site, but imported into
the current scope using a renaming import as X.
•
Property-like callables with an operator function
invoke
available as a
member or an extension in the current scope:
– A property named X at its declaration site;
– An object or a companion object named X at its declaration site;
–
A companion object of a classifier type named
X
at its declaration
site;
– An enum entry named X at its declaration site;
–
Any of the above named
Y
at its declaration site, but imported into
the current scope using a renaming import as X.
For property-like callables, a call
X
(
Y
0
, . . . , Y
N
) is an overloadable operator which
is expanded to
X.invoke
(
Y
0
, . . . , Y
N
). The call may contain type parameters,
named parameters, variable argument parameter expansion and trailing lambda
parameters, all of which are forwarded as-is to the corresponding
invoke
function.
The set of implicit receivers itself (denoted by
this
expression) may also be used
as a property-like callable using
this
as the left-hand side of the call expression.
As with normal property-like callables,
this@A
(
Y
0
, . . . , Y
N
) is an overloadable
0
, . . . , Y
N
).
A member callable is one of the following:
• a member function-like callable (including constructors);