Operator overloading
Kotlin allows us to provide implementations for a predefined set of operators on our types. These operators have fixed symbolic representation (like
+
or *
) and fixed precedence. To implement an operator, we provide a member function or an extension function with a fixed name, for the corresponding type, i.e. left-hand side type for binary operations and argument type for unary ones. Functions that overload operators need to be marked with the operator
modifier.
Further we describe the conventions that regulate operator overloading for different operators.
Unary operations
Unary prefix operators
This table says that when the compiler processes, for example, an expression
+a
, it performs the following steps:- Determines the type of
a
, let it beT
. - Looks up a function
unaryPlus()
with theoperator
modifier and no parameters for the receiverT
, i.e. a member function or an extension function. - If the function is absent or ambiguous, it is a compilation error.
- If the function is present and its return type is
R
, the expression+a
has typeR
.
Note that these operations, as well as all the others, are optimized for Basic types and do not introduce overhead of function calls for them.
Increments and decrements
The
inc()
and dec()
functions must return a value, which will be assigned to the variable on which the ++
or --
operation was used. They shouldn't mutate the object on which the inc
or dec
was invoked.
The compiler performs the following steps for resolution of an operator in the postfix form, e.g.
a++
:- Determines the type of
a
, let it beT
. - Looks up a function
inc()
with theoperator
modifier and no parameters, applicable to the receiver of typeT
. - Checks that the return type of the function is a subtype of
T
.
The effect of computing the expression is:
- Store the initial value of
a
to a temporary storagea0
, - Assign the result of
a.inc()
toa
, - Return
a0
as a result of the expression.
For
a--
the steps are completely analogous.
For the prefix forms
++a
and --a
resolution works the same way, and the effect is:- Assign the result of
a.inc()
toa
, - Return the new value of
a
as a result of the expression.
Binary operations
Arithmetic operators
For the operations in this table, the compiler just resolves the expression in the Translated to column.
Note that the
rem
operator is supported since Kotlin 1.1. Kotlin 1.0 uses the mod
operator, which is deprecated in Kotlin 1.1.'In' operator
For
in
and !in
the procedure is the same, but the order of arguments is reversed.Indexed access operator
Square brackets are translated to calls to
get
and set
with appropriate numbers of arguments.Invoke operator
Parentheses are translated to calls to
invoke
with appropriate number of arguments.Augmented assignments
For the assignment operations, e.g.
a += b
, the compiler performs the following steps:- If the function from the right column is available
- If the corresponding binary function (i.e.
plus()
forplusAssign()
) is available too, report error (ambiguity). - Make sure its return type is
Unit
, and report an error otherwise. - Generate code for
a.plusAssign(b)
- If the corresponding binary function (i.e.
- Otherwise, try to generate code for
a = a + b
(this includes a type check: the type ofa + b
must be a subtype ofa
).
Note: assignments are NOT expressions in Kotlin.
Equality and inequality operators
Note:
===
and !==
(identity checks) are not overloadable, so no conventions exist for them
The
==
operation is special: it is translated to a complex expression that screens for null
's. null == null
is always true, and x == null
for a non-null x
is always false and won't invoke x.equals()
.Comparison operators
All comparisons are translated into calls to
compareTo
, that is required to return Int
.Infix calls for named functions
We can simulate custom infix operations by using infix function calls.
Kotlin operator overloading:- Geniusofstudent
Reviewed by Network security
on
June 14, 2019
Rating:
No comments: