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 8572219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:57:34+00:00 2026-06-11T18:57:34+00:00

One of my issue is – if we rely on implicit return, it is

  • 0

One of my issue is – if we rely on implicit return, it is error prone,

eg.

def foo(bar: Int): Int =
{
    if (f1(bar)) 0
    if (f2(bar)) 1 
    else -1
}

Sometimes we just forgot the else statement, in order to fix this issue, curly brace is enforced like, .e.g

def foo(bar: Int): Int =
{
    if (f1(bar)) {
        0
    } else if (f2(bar)) {
        1
    } else {
        -1
    }
}

But the new style is too verbose IMHO, any way to fix?

  • 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-06-11T18:57:35+00:00Added an answer on June 11, 2026 at 6:57 pm

    As a practical matter, I never have this problem, and I make loads of silly mistakes. I suspect that you are entirely capable of learning to not skip the else statement. (Note that braces don’t help anything; you can still skip the else in the middle one.)

    If you really, really find this a problem, you can either abuse the match statement:

    true match {
      case _ if f1(bar) => 0
      case _ if f2(bar) => 1
      case _ => -1
    }
    

    which will prevent you from making any mistakes. Or you can write yourself a utility method:

    trait Conditional[+A] {
      def apply[B >: A](p: => Boolean, b: => B): Conditional[B]
      def or[B >: A](default: => B): B
    }
    class FoundIt[+A](it: A) extends Conditional[A] {
      def apply[B >: A](p: => Boolean, b: => B) = this
      def or[B >: A](default: => B) = it
    }
    class NothingYet[+A] extends Conditional[A] {
      def apply[B >: A](p: => Boolean, b: => B) = {
        if (p) new FoundIt(b) else this
      }
      def or[B >: A](default: => B) = default
    }
    def ifelse[A](p: => Boolean, a: => A) = (new NothingYet[A]).apply(p,a)
    
    ifelse( f1(bar), 0 )( f2(bar), 1 ) or -1 
    

    This is a little messy for long expressions, so you could also (use :paste to stick this in the REPL in one big block if you want it to work there):

    trait Predicate[+A] {
      def Or(p: => Boolean): Loadable[A]
      def Else[B >: A](default: => B): B
    } 
    trait Loadable[+A] {
      def Then[B >: A](b: => B): Predicate[B]
    }
    object NotYetTrue extends Predicate[Nothing] {
      def Or(p: => Boolean) = if (p) ActuallyLoad else SkipLoading
      def Else[B >: Nothing](default: => B) = default
    }
    object SkipLoading extends Loadable[Nothing] {
      def Then[B >: Nothing](b: => B) = NotYetTrue
    }
    object ActuallyLoad extends Loadable[Nothing] {
      def Then[B >: Nothing](b: => B) = new Loaded[B](b)
    }
    class Loaded[+A](a: A) extends Predicate[A] with Loadable[A] {
      def Or(p: => Boolean) = this
      def Else[B >: A](default: => B) = a
      def Then[B >: A](b: => B) = this
    }
    def If(p: => Boolean): Loadable[Nothing] = NotYetTrue.Or(p)
    

    Now the only trick is that you have to use :paste in the REPL to write a multi-line statement, and put the continuation on the end of the previous line:

    If (f1(bar)) Then 0 Or
    (f2(bar)) Then 1 Else
    -1
    

    You can also use Then { 0 on one line and start up again with } Or on the next, or write everything with parens/braces and dots (which is REPL-friendly):

    If (f1(bar)) .Then (0) .
    Or (f2(bar)) .Then (1) .
    Else (-1)
    

    Anyway, all this trickery is a good illustration of how to build sophisticated DSLs with Scala, but not really the best way to solve your problem. You should just learn to be a little more careful with if/else statements; otherwise, people are going to be puzzled as to why you’re not doing things the standard way. (And if it’s performance-critical, it may be slow–match is pretty good, but the If wizardry is not good in a high-performance loop.)

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

Sidebar

Related Questions

I was expreimenting to build a page editor. One issue just drove me crazy
In IE7 browser, I just meet one issue about position. I made a demo
I am very new in iPhone. I have one issue. First Just check my
I faced one issue while working on a C code with Microsoft Visual Studio-2005
I have one issue with Datetime Picker In my firstview i have button.If i
I have one issue with UInavigationcontroller In my firstview i have button.If i click
I am facing one issue and I am not sure if what i would
I have one issue with Default Google Map app in our phone. I m
I am new to MVC and facing one issue. I have a xml file
I have a set of breakpoints which I used for debugging one issue. When

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.