Why does `coerceAtLeast` has more priority then `-...
# announcements
s
Why does
coerceAtLeast
has more priority then
-
?
-2.coerceAtLeast(0) == -2
m
does
val x = -string.length
make sense? functions and vals always have precedence over operators
s
-
is not an operator,
-2
is a signed int
m
in many languages, in grammar, the
-
is simply seen as a negating operator, not part of the actual number.
b
yep, you can press
control
and highlight
-
. It will navigate you to unaryMinus even for
val x = -2
. You can use
(-2).coerceAtLeast(0) == 0
s
I understand that this is the way it works, I just don’t understand why in the context of constant unsigned integer with sign on a left and with inline function on the right Kotlin gives more priority to inline function.
m
because that's not how the grammar and tokenizer works. Making an exception to a literal where suddenly
-
does apply directly to the proceeding token shuffles up the grammar and compiler significantly.
👍 1
s
string.length is an expression, it is not even a inlineable constant value. -2 is. val a = -2 is NOT evalutated in runtime, it will look like
BIPUSH -2
val b = -2.coerceAtLeast(0) looks like
Copy code
INVOKESTATIC kotlin/ranges/RangesKt.coerceAtLeast (II)I  
INEG
i’m not writing any tokenizer or any parser, or any other language tool. I just want to have a reasonable code without such side effects and for me it seems reasonable. I don’t see anything criminal in
else if
combination, as it shows how nice to have mostly returning expressions
m
maybe you should write your own parser to understand why this is done. can't explain it any other way
😄 1
s
can you help me to understand why? is there any major difference between
2.0
,
2.someFunction()
? Will this dot operator and minus sign be analyzed differently by parser and why not? I would be glad to here just your two cents in order to google it myself btw, maybe you know where does it lie in kotlin sources?
b
you can review 4th chapter Grammars and Parsing from kotlin language spec
s
thanks a lot, guys, for your comments and counterarguments, it definitely helped me to understand it I will definitely check out more precisely spec
b
answering your question
is there any major difference between 
2.0
2.someFunction()
 ?
yes, there is a major difference here -
2.someFunction
is
decimal literal . identifier
(identifier must start with letter and underscore) while
2.0
is
decimal literal . decimal literal
☝️ 1
here is a note from the doc above exactly about your case:
4.2.3. Decimal Literals A decimal literal is a sequence of one or more ASCII decimal digits, followed by an optional L (LATIN CAPITAL LETTER L, U+004C). A literal without a suffix represents a constant value of type Int (TODO: it is more complicated). A literal with suffix L represents a constant value of type Long. A decimal literal represents a constant value that results from interpreting the literal in decimal notation. The value of a literal must lie within the range of its type. [Note: Decimal literals always represent non-negative integers. For example, an expression -5 is the unary operator minus applied to an operand that is a decimal literal 5, rather than a single decimal literal. End Note]
s
wow, even more thanks