Scala supports only unary_-
, unary_+
, unary_!
and unary_~
for prefix operators.
Is there a specific reason that only these 4 symbols are supported and not any special character combination like infix expression support?
Is anyone interested in Scala to support a generic unary_XXX
definition?
Iâm willing to submit a SIP and implementation if more people are interested in expanding the prefix symbol vocabulary.
I have a vague recollection that this was mentioned in a SIP meeting a couple months ago (while discussing the prefix types SIP), and that @odersky said something along the lines of âsupporting those 4 prefix operators was a necessary evil, but we shouldnât make the language worse by adding more of themâ. Iâm not sure whether that was picked up in the minutes, but maybe it was.
Infix expressions are supported with every method, not just symbolic ones. Part of the appeal is that the rule is actually very simple: symbolic operators donât work any differently than method names (save with precedence).
Unless there is a very compelling need, I do not favor increasing the distinction between symbolic and non-symbolic methods.
This has a clear use case, too. Suppose we have a condition that we want to express:
p
Now, suppose we want to say ânot pâ, i.e. invert the logic of the predicate. Well,
not p
would be a pretty obvious way to do it, wouldnât it? Likewise, the English language order for a bunch of methods is opposite the order we need to invoke them, e.g. compare
"fish".reverse
reverse "fish"
But I think the advantages in parse regularity (for humans!) of a no-prefix-method scheme outweigh the advantage in being able to play word order typology games.
(Note that going from functions to methods is already a VSO to SVO transformation.)
Thatâs interesting. Why would those 4 be more necessary than others?
People like to use them with numbers (arithmetic and bitwise negation) and booleans (logical negation).
(Iâm actually not sure what the point of unary_+
is. The spec says it is an identity function (section 12.2.1), but Iâve never seen anyone use it, and itâs not even implemented for BigInt
ÂŻ\_(ă)_/ÂŻ )
Well, Iâm guessing !
and -
have clear arithmetical and logical usages. +
maybe was introduced to complement -
. I have no idea why ~
was introduced as a prefix operator. If we need ~
why donât we need %
or ~%
or any other special character combo? I donât have any special use cases for more prefix symbols, but I just want the language to be more consistent.
I think the story basically is that since existing languages like Java
support those 4, they wanted scala to support them to, but like everything
in scala, it canât be âspecial-casedâ so they made it a general-purpose
mechanism. I assume thatâs what Odersky meant by a ânecessary evil.â
A lot of features in scala follow this thought process. For example, Java
has implicit conversions â between certain predetermined things. Scala
opens that up to be programmable. Java has type inference â in very
limited ways (e.g.,
http://james-iry.blogspot.com/2009/04/java-has-type-inference-and-refinement.html).
Scala opens it up to be general. Java has try catch. Scala unifies the
catch block with partial functions / pattern matching. (In fact, in scala
the âargumentâ to catch doesnât have to be a partial function literal â
you can just refer to a named partial function!) And so on and so forth.
In the beginning of Programming in Scala they discuss this design
philosophy, using the analogy of the cathedral vs. the bazaar.
~
is bitwise negation.
This wasnât possible until 2.12 (the Scala version, not the short month of 2012):
scala> val - = 42
-: Int = 42
scala> val i = -
i: Int = 42
The âSigns of Troubleâ puzzler warns, âDonât take everything you read literally.â
Not exactly relevant, but âUnary Quandaryâ is a pretty good puzzler title.
Whatever happened to the puzzling guy, and why isnât there a puzzler forum?