Proposal: new operator for exponentiation

I’d like to add an operator for exponentiation to Scala.

In maths, exponentiation is expressed by layout rather than by a symbol. We can’t do that.

I propose the symbol \. It suggests the traditional layout. For example 2 \ 3 = 8. You can imagine that if the two arguments were squashed a little, the right argument might slide up a little. It is not used already. It has the right precedence.

Other languages use ^ or **. In Scala, we can’t use ^ because it’s XOR and it has wrong precedence. We can’t use ** because it has wrong precedence.

Somebody already suggests it in https://www.scala-lang.org/old/node/4142.

1 Like

AFAIK, Scala doesn’t allow mixing both left and right precedence in the same expression.
Regarding precedence, theoretically in dotty we have the @infix annotation meant for infix operations. So if ** isn’t marked as infix, then the user should invoke x.**(y), which will have the proper precedence.
In any case, IMO, the lack of dedicated symbol for exponentiation does not hold enough merit for language intervention, where a library can suffice.

2 Likes

Thanks, soronpo.

I edited my post, to make the backslash symbol visible: 3\2 = 9.

I took the liberty of making a PR: https://github.com/scala/scala/pull/9093