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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:50:01+00:00 2026-05-30T05:50:01+00:00

First. Consider the following code scala> val fail = (x: Any) => { throw

  • 0

First. Consider the following code

scala> val fail = (x: Any) => { throw new RuntimeException }
fail: Any => Nothing = <function1>

scala> List(1).foreach(fail)
java.lang.RuntimeException
    at $anonfun$1.apply(<console>:7)
    at $anonfun$1.apply(<console>:7)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)

There is additional anonfun between foreach and exception. One is expected to be a value of fail itself (object of a class Function1[]), but where is the second comes from?

foreach signature takes this function:

def foreach[U](f: A => U): Unit 

So, what is the purpose of the second one?

Second, consider the following code:

scala> def outer() {
     |   def innerFail(x: Any) = { throw new RuntimeException("inner fail") }
     | 
     |   Set(1) foreach innerFail
     | }
outer: ()Unit

scala> outer()
java.lang.RuntimeException: inner fail
    at .innerFail$1(<console>:8)
    at $anonfun$outer$1.apply(<console>:10)
    at $anonfun$outer$1.apply(<console>:10)
    at scala.collection.immutable.Set$Set1.foreach(Set.scala:86)

There are two additional anonfuns… do they really needed? :-E

  • 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-30T05:50:02+00:00Added an answer on May 30, 2026 at 5:50 am

    Let’s look at the bytecode.

    object ExtraClosure {
      val fail = (x: Any) => { throw new RuntimeException }
      List(1).foreach(fail)
    }
    

    We find, inside the (single) anonymous function:

    public final scala.runtime.Nothing$ apply(java.lang.Object);
      Code:
       0:   new #15; //class java/lang/RuntimeException
       3:   dup
       4:   invokespecial   #19; //Method java/lang/RuntimeException."<init>":()V
       7:   athrow
    
    public final java.lang.Object apply(java.lang.Object);
      Code:
       0:   aload_0
       1:   aload_1
       2:   invokevirtual   #27; //Method apply:(Ljava/lang/Object;)Lscala/runtime/Nothing$;
       5:   athrow
    

    So it’s actually not an extra closure after all. We have one method overloaded with two different return values (which is perfectly okay for the JVM since it treats the type of all parameters as part of the function signature). Function is generic, so it has to take the object return, but the code you wrote returns specifically Nothing, it also creates a method that returns the type you’d expect.

    There are various ways around this, but none are without their flaws. This is the type of thing that JVMs are pretty good at eliding, however, so I wouldn’t worry about it too much.

    Edit: And of course in your second example, you used a def, and the anonfun is the class that wraps that def in a function object. That is of course needed since foreach takes a Function1. You have to generate that Function1 somehow.

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

Sidebar

Related Questions

Consider the following Scala code def NOTImplementedIn[T<: AnyRef](t:T):String = throw new Exception(t.getClass.getName+: Input type
Consider the following code: Dim arr1 As New List(Of Double) Dim arr2 As New
Consider the following code: string propertyName; var dateList = new List<DateTime>() { DateTime.Now };
Consider the following code snippet within a class private static Object _syncroot = new
Consider the following code, the first demonstrates that the cleanup executes when we're finished
Let's consider the following code in Java. package obj; final class First { public
Consider the following code: static void Main(string[] args) { using (MemoryStream memoryStream = new
Consider the following simple code to create a typesafe equals. This first section allows
Consider the following code: #include <iostream> struct X{ X(){ throw 0; } }; void
Let's consider the following code: SimpleDateFormat sdf = new SimpleDateFormat(HH:mm:ss dd/MM/yyyy, Locale.US); long start

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.