typelevel/spire

Operator precedence *: and :*

lJoublanc opened this issue · 2 comments

I've found some weird behaviour when working with a vector space. The following equality doesn't hold: x + 3 *: x =!= x + x :* 3, where x is a vector.

A full example is here: https://scastie.scala-lang.org/bJBOQJ9wRzCjJx01dLlzPQ

This is probably because of scala's operator precedence (from the language spec):

Infix Operations

An infix operator can be an arbitrary identifier. Infix operators have precedence and associativity defined as follows:

The precedence of an infix operator is determined by the operator's first character. Characters are listed below in increasing order of precedence, with characters on the same line having the same precedence.

(all letters)
|
^
&
= !
< >
:
+ -
* / %
(all other special characters)

That is, operators starting with a letter have lowest precedence, followed by operators starting with ‘|’, etc.

I appreciate there is not much that can be done about this, but it's horribly counter-intuitive.

So, just to make this completely clear, *: is higher precedence than + but :* is not. This explains the weird behaviour.

I think this is not a Spire issue. It may be counterintuitive but there's nothing we can do about this here.