title: Ternary Operator Similar To ?:
body: <p>I am trying to avoid constructs like this:</p>
  
  <pre><code>val result = this.getClass.getSimpleName
  if (result.endsWith("$")) result.init else result
  </code></pre>
  
  <p>Ok, in this example the <code>then</code> and <code>else</code> branch are simple, but you can image complex ones.
  I built the following:</p>
  
  <pre><code>object TernaryOp {
    class Ternary[T](t: T) {
      def is[R](bte: BranchThenElse[T,R]) = if (bte.branch(t)) bte.then(t) else bte.elze(t)
    }
    class Branch[T](branch: T =&gt; Boolean) {
      def ?[R] (then: T =&gt; R) = new BranchThen(branch,then)
    }
    class BranchThen[T,R](val branch: T =&gt; Boolean, val then: T =&gt; R)
    class Elze[T,R](elze: T =&gt; R) {
      def :: (bt: BranchThen[T,R]) = new BranchThenElse(bt.branch,bt.then,elze)
    }
    class BranchThenElse[T,R](val branch: T =&gt; Boolean, val then: T =&gt; R, val elze: T =&gt; R)
    implicit def any2Ternary[T](t: T) = new Ternary(t)
    implicit def fct2Branch[T](branch: T =&gt; Boolean) = new Branch(branch)
    implicit def fct2Elze[T,R](elze: T =&gt; R) = new Elze(elze)
  }
  </code></pre>
  
  <p>Defined that, I can replace the above simple example with:</p>
  
  <pre><code>this.getClass.getSimpleName is {s: String =&gt; s.endsWith("$")} ? {s: String =&gt; s.init} :: {s: String =&gt; s}
  </code></pre>
  
  <p>But how can I get rid of the <code>s: String =&gt;</code>? I want something like that:</p>
  
  <pre><code>this.getClass.getSimpleName is {_.endsWith("$")} ? {_.init} :: {identity}
  </code></pre>
  
  <p>I guess the compiler needs the extra stuff to infer types.</p>