Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:33:32+00:00 2026-05-26T13:33:32+00:00

I implemented a ternary operator like Java’s <condition> ? <if true> : <if false>

  • 0

I implemented a ternary operator like Java’s <condition> ? <if true> : <if false>, substituting / for :, since : is not a valid identifier:

case class Ternary[T](val o: Option[T]) {
  def / (f: => T) = o getOrElse f
}

implicit def boolToTernary(cond: Boolean) = new {
  def ? [T](f: => T) = if(cond) Ternary(Some(f)) 
                        else    Ternary[T](None)
}

It works fine in general, e.g.

scala> (1 > 2) ? "hi" / "abc"
res9: java.lang.String = abc

but falls down in the following case:

scala> (1 > 2) ? 5 / 6.0
<console>:33: error: type mismatch;
 found   : Double(6.0)
 required: Int
       (1 > 2) ? 5 / 6.0
                     ^

Is there any tweaking I can do to the types in order to get this to work like the built-in if (1 > 2) 5 else 6.0 does? I googled for similar solutions and the implementations I found all exhibited the same behaviour.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:33:33+00:00Added an answer on May 26, 2026 at 1:33 pm

    One thing you can do is change your definition of / to this:

    def /[U >: T](f: => U) = o getOrElse f
    

    It doesn’t work like a regular if (where the inferred type would be Double) — you get AnyVal, but that’s already an improvement with a very small modification to your code.

    Update: I think I’ve found a (slightly more complicated) way to make it behave more like a normal if (e.g., in this case, inferring a Double). Try this code:

    implicit def boolToTernary(cond: Boolean) = new {
      def ?[T](f: => T) = if (cond) Ternary(Some(f))
      else Ternary[T](None)
    }
    
    case class Ternary[T](val o: Option[T]) {
      def /[U, That](f: => U)(implicit conv: BiConverter[T, U, That]): That = o map conv.toThat1 getOrElse (conv toThat2 f)
    }
    
    class BiConverter[T, U, That](val toThat1: T => That, val toThat2: U => That)
    
    trait LowPriorityBiConverterImplicits {
      implicit def subtype[A, T <: A, U <: A]: BiConverter[T, U, A] = new BiConverter[T, U, A](identity[T], identity[U])
    }
    
    object BiConverter extends LowPriorityBiConverterImplicits {
      implicit def identityConverter[T]: BiConverter[T, T, T] = new BiConverter[T, T, T](identity, identity)
      implicit def firstAsSecond[T, U](implicit conv: T => U): BiConverter[T, U, U] = new BiConverter[T, U, U](conv, identity)
      implicit def secondAsFirst[T, U](implicit conv: U => T): BiConverter[T, U, T] = new BiConverter[T, U, T](identity, conv)
    }
    

    Then (some sample code):

    abstract class Fruit
    class Apple extends Fruit
    class Banana extends Fruit
    
    def main(args: Array[String]) {
      val int = (1 > 2) ? 5 / 6 // Int is inferred
      val fruit = (1 > 2) ? new Apple / new Banana // Fruit is inferred
      val double1 = (1 > 2) ? 5 / 5.5 // Double is inferred
      val double2 = (1 > 2) ? 5.5 / 5 // Double is inferred
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a small OOP library in Lua, and two things are not quite
i implemented a small web application to create Rss feeds in java(spring mvc) i
I implemented simple server-client chat in Java. Here the source for the server: public
I implemented a Runnable and it checks correct if a boolean value turns true.
I implemented my Cub3r like this <script type=text/javascript> var flashvars = {}; flashvars.xml =
I implemented an rpc call. It works on local machine but does not work
I want to implement ternary conditional operator in MySQL. I have a table in
Suppose you are using the ternary operator, or the null coalescing operator, or nested
I implemented a Save As dialog in Java that prompts the user if the
I implemented the Pattern class as shown here: http://www.java2s.com/Code/Java/GWT/ImplementjavautilregexPatternwithJavascriptRegExpobject.htm And I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.