How to overload the unary operators of `Int`?
# stdlib
s
How to overload the unary operators of
Int
?
d
You can't Operator functions on primitives are member functions, which always wins over extensions
1
j
I guess that's a good thing too. It would be quite unexpected that using
+42
anywhere would be different from
42
- and that's not even considering the heresy of overriding
-42
(by the way, in those cases it would be called overriding rather than overloading, since these operators already exist for these types)
k
A related thing is that you can't write the literal for Int.MIN_VALUE because
-2147483648
means
-(<tel:2147483648|2147483648>)
, i.e. it gets parsed as an Int to which the unary operator
-
is applied, and
2147483648
is not a valid Int. On the other hand, other languages treat negative numbers as literals, i.e. the "-" is itself part of the literal. But not in Kotlin.
🙂 1