Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The former example, T m 0, is an example of "operator notation". Scala has three types of operator notations, prefix (called unary), infix and postfix. Let's see examples of all three in action here:</p> <pre><code>class MyByte(val n : Int) { require(n &gt;= 0 &amp;&amp; n &lt;= 255) def unary_! = new MyByte(n ^ 0xff) def +(m : MyByte) = new MyByte(n + m.n) def bits = (math.log(n) / math.log(2) + 1).toInt override def toString = "0" * (8 - bits) + n.toBinaryString } </code></pre> <p>An here it is in use:</p> <pre><code>scala&gt; val a = new MyByte(5) a: MyByte = 00000101 scala&gt; val b = new MyByte(10) b: MyByte = 00001010 scala&gt; ! a // Prefix/Unary res58: MyByte = 11111010 scala&gt; a + b // Infix res59: MyByte = 00001111 scala&gt; b bits // Postfix res60: Int = 4 </code></pre> <p>While infix and postfix notations accept any valid Scala identifier, though there is talk of restricting postfix notation, only four identifiers can be used as prefix: ~, !, - and +.</p> <p>Now, when you try "m 0", Scala discards it being a unary operator, on the grounds of not being a valid one (~, !, - and +). It finds that "m" is a valid object -- it is a function, not a method, and all functions are objects.</p> <p>As "0" is not a valid Scala identifier, it cannot be neither an infix nor a postfix operator. Therefore, Scala complains that it expected ";" -- which would separate two (almost) valid expressions: "m" and "0". If you inserted it, then it would complain that m requires either an argument, or, failing that, a "_" to turn it into a partially applied function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload