Widen anonymous placeholder to 2 chars

Additional two-chars-width __ placeholder would be useful for multiline continuous blocks with indentation of 2.

Current situation:

_.placeholder // awkward
  .increaseWidth(2)

Proposal:

__.now
  .itsFine(_.old(1).isStillAvailable)
1 Like

I don’t think I’ve ever needed this. Once the lambda becomes less than trivial I write a function with a named parameter.

2 Likes

It’s for those who are too lazy to invent new val/def names that looks nice in code and are meaningful…

sheet map { row =>
  row.doSomething()
    .doSomethingAgain()

// vs.

sheet map {
  __.doSomething()
    .doSomethingAgain()
}

I tend to never use a _ in a curly brace block. If I need to do that it’s also likely a sign that naming it would improve the code.

1 Like

As much as the ragged indentation irks me at times, I believe this would have the potential to break quite a bit of Play code, as their JSON library defines __ as an alias:

The play.api.libs.json package defines an alias for JsPath : __ (double underscore). You can use this if you prefer:

val longPath = __ \ "location" \ "long"

Source

2 Likes

Scala 3 for breaking changes :smiley:

True, but it’s worth acknowledging what will break and weighing the cost to make sure it’s worth breaking.

I’m a bit dubious here, for a couple of reasons:

  1. This would break would break quite a bit of code, and the rewrite would have to take into account whatever Play replaces __ with, which complicates things
  2. .map(_.foo) is way more common than the multi-lined chained version (though it does happen), so it appears to be adding a 1 character tax in the common case to help clean up formatting in an uncommon case. That seems a bit much.

Would it be possible to solve this by making the formatter more sophisticated, so that it’ll recognize and indent accordingly?

Your sheet examples could look like this instead (I’d love it if we could get row to indent cleanly as well):

sheet map { row =>
  row.doSomething()
     .doSomethingAgain()

// vs.

sheet map {
  _.doSomething()
   .doSomethingAgain()
}

I wouldn’t mind a token of arbitrarily many underscores replaced by underscore.

"hello" match {
  case "hell" => "o, world"
  case ______ => ______
}

Aligning underscores isn’t really pretty enough to catch on. Maybe a typographer out there could explain why.

"hello" match {
  case "hell" => "o, world"
  case __---> => ""
  case __~~~> => ""
  case __:::: => ""
}

The danger of double underscore is that val __: is not val __ :.

scala> (1 to 10) map (__ =>
     | __.floatValue
     |   .floor
     | )

I’m willing to try anything once.

scala> (1 to 10) map (__ =>
     |                __.floatValue
     |                  .floor
     | )
2 Likes