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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:10:31+00:00 2026-06-15T04:10:31+00:00

My colleagues and I are a bit stumped over the following code’s behavior. def

  • 0

My colleagues and I are a bit stumped over the following code’s behavior.

def a: String = {
  None.foreach(return "1")
  return "2"
}

def b: String = {
  None.foreach(x => return "1")
  return "2"
}

As expected, invoking b does return “2”. However, invoking a returns “1”. When exactly is return "1" being evaluated when a is executed?

  • 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-15T04:10:32+00:00Added an answer on June 15, 2026 at 4:10 am

    All* function evaluations of the form

    f({code})
    

    are equivalent to

    val temp = { code }
    f(temp)
    

    So, in the first case,

    val temp = return "1"
    None.foreach(temp)   // Never reach this point!
    

    While in the second,

    val temp = (x: Nothing) => return 1
      // Equivalent: new Function1[Nothing,String]{ def apply(x: Nothing) = return "1" }
    None.foreach(temp)   // Never call that weird function!
    

    so everything is okay.

    But, wait, foreach takes an A => Unit. How is return "1" such a function? Well, Scala starts with the most specific type possible (Nothing, which is a subclass of anything, and therefore promises to do anything you ask of it, except it can’t exist). And, then, since no values is produced by the statement (control escapes via a return), it never modifies it from Nothing. So, indeed, Nothing is a subclass of Function1[A,Unit].

    And to produce that Nothing–well, to pretend to produce it–you actually run the code, and return.

    * Actually, if the parameter is passed by name, it’s secretly converted to () => { Code } and passed in without evaluation.

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

Sidebar

Related Questions

I had an argument with my colleagues over the difference between 32-bit and 64-bit
A colleague and I are having a bit of an argument over multiple inheritance.
Colleagues, Using JPA I need resolve following issue: at database level exits 3 entities
I would like to understand a bit better how to debug my iOS code
I'm writing some code in VB.Net which I hope demonstrate to colleagues (not to
Colleagues have been touting the wonders of maven and its magical dependency stuff but
Colleagues, When I try to use X509 certificate with public key in .cer file,
My colleagues are using Visual Studio 2002 and uses the C++ MFC. I am
My colleagues and I were having a discussion regarding logic in enums. My personal
One of my colleagues has totally messed up the contents of a directory in

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.