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

  • Home
  • SEARCH
  • 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 775147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:14:36+00:00 2026-05-14T19:14:36+00:00

I’m writing a code generator which produces Scala output. I need to emulate a

  • 0

I’m writing a code generator which produces Scala output.

I need to emulate a ternary operator in such a way that the tokens leading up to ‘?’ remain intact.

e.g. convert the expression c ? p : q to c something. The simple if(c) p else q fails my criteria, as it requires putting if( before c.

My first attempt (still using c/p/q as above) is

c match { case(true) => p; case _ => q }

another option I found was:

class ternary(val g: Boolean => Any) { def |: (b:Boolean) = g(b) }

implicit def autoTernary (g: Boolean => Any): ternary = new ternary(g)

which allows me to write:

c |: { b: Boolean => if(b) p else q }

I like the overall look of the second option, but is there a way to make it less verbose?

Thanks

  • 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-14T19:14:37+00:00Added an answer on May 14, 2026 at 7:14 pm

    Even though the syntax doesn’t evaluate in the expected order–it binds the conditional to the first option!–you can make your own ternary operator like this:

    class IfTrue[A](b: => Boolean, t: => A) { def |(f: => A) = if (b) t else f }
    class MakeIfTrue(b: => Boolean) { def ?[A](t: => A) = new IfTrue[A](b,t) }
    implicit def autoMakeIfTrue(b: => Boolean) = new MakeIfTrue(b)
    

    The trick is to interpret ? as a method on a MakeIfTrue object that binds the condition to the object to return in the “true” case. The resulting IfTrue object now uses the | method as a request to evaluate the condition, returning the stored true option if the condition is true, or the just-passed-in one if it’s false.

    Note that I’ve used stuff like => A instead of just A–by-name parameters–in order to not evaluate the expression unless it’s actually used. Thus, you’ll only evaluate the side that you actually need (just like an if statement).

    Let’s see it in action:

    scala> List(1,3,2).isEmpty ? "Empty" | "Nonempty"
    res0: java.lang.String = Nonempty
    
    scala> (4*4 > 14) ? true | false
    res1: Boolean = true
    
    scala> class Scream(s: String) { println(s.toUpperCase + "!!!!") }
    defined class Scream
    
    scala> true ? new Scream("true") | new Scream("false")
    TRUE!!!!
    res3: Scream = Scream@1ccbdf7
    

    (P.S. To avoid confusion with the Actor library ?, you probably ought to call it something else like |?.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.