Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Pretty-print lambdas #21846

Merged
merged 1 commit into from
Oct 27, 2024
Merged

Pretty-print lambdas #21846

merged 1 commit into from
Oct 27, 2024

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented Oct 25, 2024

Before:

[[syntax trees at end of                     typer]] // tests/printing/lambdas.scala
package <empty> {
  final lazy module val Main: Main = new Main()
  final module class Main() extends Object() { this: Main.type =>
    val f1: Int => Int =
      {
        def $anonfun(x: Int): Int = x.+(1)
        closure($anonfun)
      }
    val f2: (Int, Int) => Int =
      {
        def $anonfun(x: Int, y: Int): Int = x.+(y)
        closure($anonfun)
      }
    val f3: Int => Int => Int =
      {
        def $anonfun(x: Int): Int => Int =
          {
            def $anonfun(y: Int): Int = x.+(y)
            closure($anonfun)
          }
        closure($anonfun)
      }
    val f4: [T] => (x: Int) => Int =
      {
        def $anonfun[T >: Nothing <: Any](x: Int): Int = x.+(1)
        closure($anonfun)
      }
    val f5: [T] => (x: Int) => Int => Int =
      {
        def $anonfun[T >: Nothing <: Any](x: Int): Int => Int =
          {
            def $anonfun(y: Int): Int = x.+(y)
            closure($anonfun)
          }
        closure($anonfun)
      }
    val f6: Int => Int =
      {
        def $anonfun(x: Int): Int =
          {
            val x2: Int = x.+(1)
            x2.+(1)
          }
        closure($anonfun)
      }
    def f7(x: Int): Int = x.+(1)
    val f8: Int => Int =
      {
        def $anonfun(x: Int): Int = Main.f7(x)
        closure($anonfun)
      }
    val l: List[Int] = List.apply[Int]([1,2,3 : Int]*)
    Main.l.map[Int](
      {
        def $anonfun(_$1: Int): Int = _$1.+(1)
        closure($anonfun)
      }
    )
    Main.l.map[Int](
      {
        def $anonfun(x: Int): Int = x.+(1)
        closure($anonfun)
      }
    )
    Main.l.map[Int](
      {
        def $anonfun(x: Int): Int =
          {
            val x2: Int = x.+(1)
            x2.+(1)
          }
        closure($anonfun)
      }
    )
    Main.l.map[Int](
      {
        def $anonfun(x: Int): Int = Main.f7(x)
        closure($anonfun)
      }
    )
  }
}

After:

[[syntax trees at end of                     typer]] // tests/printing/lambdas.scala
package <empty> {
  final lazy module val Main: Main = new Main()
  final module class Main() extends Object() { this: Main.type =>
    val f1: Int => Int = (x: Int) => x.+(1)
    val f2: (Int, Int) => Int = (x: Int, y: Int) => x.+(y)
    val f3: Int => Int => Int = (x: Int) => (y: Int) => x.+(y)
    val f4: [T] => (x: Int) => Int = [T >: Nothing <: Any] => (x: Int) => x.+(1)
    val f5: [T] => (x: Int) => Int => Int = [T >: Nothing <: Any] => (x: Int)
       => (y: Int) => x.+(y)
    val f6: Int => Int = (x: Int) =>
      {
        val x2: Int = x.+(1)
        x2.+(1)
      }
    def f7(x: Int): Int = x.+(1)
    val f8: Int => Int = (x: Int) => Main.f7(x)
    val l: List[Int] = List.apply[Int]([1,2,3 : Int]*)
    Main.l.map[Int]((_$1: Int) => _$1.+(1))
    Main.l.map[Int]((x: Int) => x.+(1))
    Main.l.map[Int]((x: Int) =>
      {
        val x2: Int = x.+(1)
        x2.+(1)
      }
    )
    Main.l.map[Int]((x: Int) => Main.f7(x))
  }
}

The ugly thing can still be displayed with -Yprint-debug.

@som-snytt
Copy link
Contributor

The ugly thing can still be displayed with -Yprint-debug.

Maybe it should be called -Yprint-debugly.

@mbovel mbovel marked this pull request as ready for review October 25, 2024 20:55
@mbovel mbovel added the fasttrack Simple fix. Reviewer should merge or apply additional changes directly. label Oct 25, 2024
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! We should have had this long ago.

@mbovel mbovel merged commit ede1261 into scala:main Oct 27, 2024
29 checks passed
@mbovel mbovel deleted the mb/pretty-print-lambdas branch October 27, 2024 11:58
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Dec 9, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
fasttrack Simple fix. Reviewer should merge or apply additional changes directly.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants