Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can emulate this with implicits and custom operators. Here is a short example:</p> <pre><code>implicit class FloatWrapper(n:Float) { def :&lt;(that:Float) = ResultAndLastItem(n&lt;that, that) def :&gt;(that:Float) = ResultAndLastItem(n&gt;that, that) def :=(that:Float) = ResultAndLastItem(n==that, that) // insert more comparison operations here } case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) { // insert boolean operations here // boolean operations should return another instance of ResultAndLastItem (?) } implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result </code></pre> <p>Here is an example usage in the REPL:</p> <pre><code>Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45). Type in expressions to have them evaluated. Type :help for more information. scala&gt; :paste // Entering paste mode (ctrl-D to finish) implicit class FloatWrapper(n:Float) { def :&lt;(that:Float) = ResultAndLastItem(n&lt;that, that) def :&gt;(that:Float) = ResultAndLastItem(n&gt;that, that) def :=(that:Float) = ResultAndLastItem(n==that, that) // insert more comparison operations here } case class ResultAndLastItem(result:Boolean, lastItem:Float) extends FloatWrapper(lastItem) { // insert boolean operations here // boolean operations should return another instance of ResultAndLastItem (?) } implicit def toBoolean(r:ResultAndLastItem):Boolean = r.result // Exiting paste mode, now interpreting. warning: there were 1 feature warnings; re-run with -feature for details defined class FloatWrapper defined class ResultAndLastItem toBoolean: (r: ResultAndLastItem)Boolean scala&gt; if(2 :&lt; 3 :&lt; 4) "yay" else "nope" res0: String = yay scala&gt; if(2 :&lt; 3 :&lt; 3) "nope" else "yay" res1: String = yay </code></pre> <p>Remarks:</p> <ul> <li><p>You can easily add more comparison operators, such as <code>:&lt;=</code>.</p></li> <li><p>The usage of custom operators is required because this forces the compiler to use the <code>implicit class</code> above, instead of the built-in default operator for <code>Float</code>s.</p></li> <li><p>It is hard to make my example more generic, in order to allow for all comparable types, such as <code>Double</code>, <code>Int</code>, or custom types. The reason why this is hard is because the <code>implicit class FloatWrapper</code> cannot require further <code>implicit</code> arguments - implicits cannot be nested. (Or more precisely, they can, syntactically, but then the compiler won't pick them for implicit resolution.)</p></li> <li><p>As an optimization, you might want to consider adding boolean shortcuts, i.e. when an expression is already known to be <code>false</code>, it is not necessary to evaluate the remaining comparisons.</p> <ul> <li>This becomes tricky when you also add boolean operators, such as <code>||</code> or <code>&amp;&amp;</code>.</li> </ul></li> </ul>
 

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