question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Anonymous functions defined in anonymous classes are emitted on containing object instead of being emitted on anonymous class

See original GitHub issue

This may be expected behavior / an optimization but I wanted to open an issue to determine if it’s intended or not. This behavior resulted in a bug in FS2: https://github.com/typelevel/fs2/issues/2623

Compiler version

3.0.2

Minimized code

trait Foo {
  def foo(x: Option[Int]): Option[Int]
}

object Bar {
  def mk = new Foo {
    def foo(x: Option[Int]) = x.map(_ + 1)
  }
}

Output

➜  javap -c -cp . Bar\$
Compiled from "foo.scala"
public final class Bar$ implements java.io.Serializable {
  public static final Bar$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class Bar$
       3: dup
       4: invokespecial #18                 // Method "<init>":()V
       7: putstatic     #20                 // Field MODULE$:LBar$;
      10: return

  public Foo mk();
    Code:
       0: new           #9                  // class Bar$$anon$1
       3: dup
       4: invokespecial #30                 // Method Bar$$anon$1."<init>":()V
       7: areturn

  public static final int Bar$$anon$1$$_$foo$$anonfun$1(int);
    Code:
       0: iload_0
       1: iconst_1
       2: iadd
       3: ireturn
}

Note the final member: public static final int Bar$$anon$1$$_$foo$$anonfun$1(int);

Expectation

javap -c -cp . Bar\$
Compiled from "foo.scala"
public final class Bar$ {
  public static final Bar$ MODULE$;

  public static {};
    Code:
       0: new           #2                  // class Bar$
       3: dup
       4: invokespecial #14                 // Method "<init>":()V
       7: putstatic     #16                 // Field MODULE$:LBar$;
      10: return

  public Foo mk();
    Code:
       0: new           #7                  // class Bar$$anon$1
       3: dup
       4: invokespecial #19                 // Method Bar$$anon$1."<init>":()V
       7: areturn
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Feb 28, 2022

I don’t have any specific idea of what to do here but:

Scala 3.0, 3.1 and 3.2 would need to be supported

I think it’s better to dedicate resources towards improved support for future compiler versions rather than trying to support obsolete versions at all cost. We’re releasing new minor releases at a rapid pace and doing everything we can to make it easy for people to upgrade. This should be good news for tooling authors since they can fix things in the compiler (as might need to be done here?) and have these fixes delivered to users quickly rather than maintaining workarounds forever.

However, the debugger is broken for Scala 3, and precisely because of this change.

Metals is also working on having a debugger that supports expression evaluation with Scala 3, this sounds like an area where you could collaborate.

0reactions
vasilmkdcommented, Feb 28, 2022

Thanks for the input @smarter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java8 Lambdas vs Anonymous classes - Stack Overflow
An anonymous inner class is a class, which means that it has scope for variable defined inside the inner class. Whereas,lambda expression is...
Read more >
Difference between Anonymous Inner Class and Lambda ...
Anonymous Inner Class: It is an inner class without a name and for which only a single object is created. An anonymous inner...
Read more >
Anonymous function - Wikipedia
In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being...
Read more >
Java static code analysis - SonarSource Rules
Anonymous inner classes containing only one method should become lambdas. Code Smell ... Classes that don't define "hashCode()" should not be used in...
Read more >
On Lambdas, Anonymous Classes and Serialization in Java
Java 8 introduced lambdas, which have replaced anonymous classes in many scenarios. Java's lambdas do not have a type associated with them.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found