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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:49:50+00:00 2026-06-04T18:49:50+00:00

Imagine this code: class Foo { println(in Foo) def foo(a: Int) = a +

  • 0

Imagine this code:

class Foo {
  println("in Foo")

  def foo(a: Int) = a + 1
}

Now, if we invoke:

new Foo().foo _

instance of class Foo will get created, as expected:

in Foo
res0: (Int) => Int = <function1>

However, if we invoke this:

new Foo().foo(_)

Foo’s constructor will not get called:

res1: (Int) => Int = <function1>

If we then say:

res1(7)

that is when Foo gets instantiated:

in Foo
res2: Int = 8

Why does Eta expansion versus partial function application make a difference in class instantiation?

  • 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-04T18:49:51+00:00Added an answer on June 4, 2026 at 6:49 pm

    Boy, that’s a subtle one, but as far as I can tell it’s following the Scala spec completely. I’ll quote from version 2.9 of the spec.

    For your first example: as you rightly say, you are seeing eta expansion through a special case of a Method Value (§6.7):

    The expression e _ is well-formed if e is of method type or if e is a call-by-name parameter. If e is a method with parameters, e _ represents e converted to a function type by eta expansion.

    The algorithm for eta expansion is given in §6.26.5 which you can follow to give the following replacement for the expression new Foo().x1 _:

    {
      val x1 = new Foo();
      (y1: Int) => x1.(y1);
    }
    

    This implies that when eta expansion is being used, all sub-expressions are evaluated at the point where the conversion takes place (if I’ve understood the meaning of the phrase “maximal sub-expression” correctly) and the final expression is the creation of an anonymous function.

    In your second example, those extra parentheses mean that the compiler will look at §6.23 (specifically, “Placeholder Syntax for Anonymous Functions) and create an anonymous function directly.

    An expression (of syntactic category Expr) may contain embedded underscore symbols _ at places where identifiers are legal. Such an expression represents an anonymous function where subsequent occurrences of underscores denote successive parameters.

    In that case, and following the algorithm in that section, your expression ends up being this:

    (x1: Int) => new Foo().foo(x1)
    

    The difference is subtle and, as explained very well by @Antoras, only actually shows in the presence of side-effecting code.

    Note that there is a bugfix under way for the case involving call-by-name code blocks (see, for example, this question, this bug and this bug).

    Postscript: In both cases, the anonymous function (x1:Int) => toto gets expanded to

    new scala.Function1[Int, Int] {
      def apply(x1: Int): Int = toto
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine this code: class foo { foo fff; int m_id; f & work() {
Say I have some code like this: class Foo { public: Foo(int v) :
Imagine this: Class A{ private int a; } Class B{ public void fn(){ A
Take this code, for example: /* * foo.h * * Created on: Nov 5,
Take this code: public class MyClass { private final Object _lock = new Object();
A question about c++ design and efficiency... Imagine this code - Database db; class
Imagine this controller: class exampleController{ def action1 = {} def action2 = {} def
Imagine I have the following code: class A { [UsefulAttribute(foo)] B var1; [UsefulAttribute(bar)] B
Imagine this simple piece of code: class Constructor() { var string: String = _
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>

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.