Can someone explain to me, why <this> behaves as i...
# getting-started
k
Can someone explain to me, why this behaves as it behaves?
y
+2
is valid Kotlin code. The code produces a warning btw, which is good. It should be even better with the return value checker, since this whole class of "bugs" gets completely elminated
k
why is it valid?
I am not at pc right now, but I don't remember having warning in IDE.
what is on "left hand side" of operator?
y
I see warning in playground, but maybe it doesn't show in IDE. It'd definitely show up using the return value checker though It's valid because unary plus is valid. It has some use cases with DSLs and such. It's just like
-2
, which is not a constant, but the unary minus operator applied to
2
k
hm, I see. again I am not sure I remember correctly, but I think when I went to definition via ctrl + click on plus, I didn't end up on unaryPlus
k
It's just like
-2
, which is not a constant, but the unary minus operator applied to
2
This bring some interesting behaviour. For example, the literal
-2147483648
is of type
Int
, even though
2147483648
doesn't fit in an
Int
and is therefore of type
Long
. In fact, even
-(<tel:2147483648|2147483648>)
is of type
Int
. And the value
Long.MIN_VALUE
is
-9223372036854775808
but if you try to use that as a literal you'll get a compiler error because the literal without the sign overflows
Long.MAX_VALUE
. In some other languages, the unary minus is taken as part of the literal itself, so this kind of behaviour doesn't arise.
🤯 1