This page is no longer maintained — Please continue to the home page at

Support for polyadic operators

No replies
Joined: 2010-02-07,
User offline. Last seen 34 weeks 1 day ago.

Scala lets you define a binary operator as a method in the class of
its first operand; the method has a parameter which acts as the
second operand. Often, binary operators are associative, so they are
effectively polyadic (AKA n-ary) operators. However, I have
encountered a family of uses of non-associative polyadic operators,
that cannot be reduced to binary operators. I would really like to be
able to use infix notation for these polyadic operators in Scala.

Maybe they could well be supported using the var args notation, as in:

def %%(those: Any*) = {var result=this.toString; those.foreach(result
+="%%"+_); result}
def main(args: Array[String]): Unit = println(this %% 1 %% 2)

Allowing this could change the meaning of current programs, though.

My use case: I am currently developing, Subscript, a Process Algebra
based extension to Scala. In Subscript you would typically specify the
syntax of a Comma Separated Value line as
csv = v..","
and use this specification (hopefully) for both reading and writing
such lines. The specification is shorthand for
csv = v;..;","
Just like with numeric multiplication in math, the sequential operator
does not need to be written explicitly.
The ellipsis symbol (..) marks the parent operator expression as a
loop, and at the same time it marks an optional break from the loop.
So the following specifications would be much different:
Subscript has just become available as a DSL. Polyadic operators are
now being written in prefix notation; the CSV example would be
manually compiled to:
_seq(_v, _optionalBreak_loop, symbol(","))
For the DSL an infix notation for polyadic operators would be much
more convenient, as in
v $ _optionalBreak_loop $ symbol(",")
I also want the DSL to become as close as possible to the "real"
Subscript syntax, so that later only a relatively small modification
to the Scala compiler would be needed. Moreover, Subscript would get
the benefits of user defined operators.

So will Scala get these polyadic operators?

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland