I don't understand the precedence in crystal

Operator Precedence¶
This list is sorted by precedence, so upper entries bind stronger than lower ones.

Category	Operators
Index accessors	[], []?
Unary	+, &+, -, &-, !, ~
Exponential	**, &**
Multiplicative	*, &*, /, //, %
Additive	+, &+, -, &-
Shift	<<, >>
Binary AND	&
Binary OR/XOR	|,^
Equality and Subsumption	==, !=, =~, !~, ===
Comparison	<, <=, >, >=, <=>
Logical AND	&&
Logical OR	||
Range	.., ...
Conditional	?:
Assignment	=, []=, +=, &+=, -=, &-=, *=, &*=, /=, //=, %=, |=, &=,^=,**=,<<=,>>=, ||=, &&=
Splat	*, **

** has higher precedence than + and * why is it in the middle ?

Are you coming from a different language with an unusual precedence order? As far as I know it’s coming from standard math precedence and most languages follow, having exponentiation (**) higher than multiplication (*) higher than addition (+). For example the python operator precedence is quite similar

but look how they are sorted in the page low precedence and then higher and then low ???

+ here is unary operator, like this: +int

the list is sorted from higher precedence to lower precedence going from top to bottom. Maybe you are confused why ** appears lower than + and * despite having exponentiation having higher precedence? ** can mean two distinct things, exponentiation or splat, exponentiation has higher precedence and splat has lower

yes that what confuses me + higher than ** what !!! that is my confusion

but it should not be on top because ** is higher

i am really confused the precedence list is messed up

Operator precedence can be confusing at first, but this is not any different in crystal than in ruby or python that also have unary + operator and ** as splat operator.
unary + as in +5 meaning just positive 5 is highest
then exponentiation, ie 5**2 == 25
then + as in addition, as in 5 + 2 == 7
then ** meaning splat, as in:

tuple = {chomp: true}
gets(**tuple)
1 Like