Skip to content

Change fewerbraces to always use a colon, even before lambdas #15273

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 30, 2022

Conversation

odersky
Copy link
Contributor

@odersky odersky commented May 23, 2022

So it's

   xs.map: x =>
       x + 1
     .foldLeft: (x, y) =>
       x + y

But the following will still not work (it is however supported by #15258):

   xs.map: x => x + 1
     .foldLeft: (x, y) => x + y

@odersky odersky force-pushed the change-fewerbraces-1 branch 2 times, most recently from 1028f8a to a668041 Compare May 23, 2022 13:04
@odersky odersky changed the title Change fewerbraces to always require colon Change fewerbraces to allow inline lambdas after : May 23, 2022
@odersky odersky marked this pull request as draft May 23, 2022 15:43
@odersky odersky changed the title Change fewerbraces to allow inline lambdas after : Change fewerbraces always use a colon, even before lambdas. May 23, 2022
@odersky odersky changed the title Change fewerbraces always use a colon, even before lambdas. Change fewerbraces always use a colon, even before lambdas May 23, 2022
@odersky odersky marked this pull request as ready for review May 23, 2022 15:45
@odersky odersky changed the title Change fewerbraces always use a colon, even before lambdas Change fewerbraces to always use a colon, even before lambdas May 23, 2022
@odersky odersky requested a review from tgodzik May 23, 2022 16:07
@odersky odersky added this to the 3.2.0-RC1 milestone May 23, 2022
@odersky odersky removed their assignment May 23, 2022
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

I feel like this proposal looks much cleaner that the previous approach. I left a few comments, but I would say the only blocking one is the issue with no whitespace allowed before :

:
y => y.toString
"hello"
.apply:
Copy link
Contributor

Choose a reason for hiding this comment

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

So instead of single : we need to explicitely call apply in case of curried functions? I think that is sensible

odersky added 7 commits May 24, 2022 17:53
Instead of parsing all formal parameters of a lambda as expressions
and converting to parameters at the end, we now scan ahead the first time
we see a `:` to determine whether the list is followed by `=>` or `?=>`.
If that's the case we parse this parameter and all following ones as
bindings instead of expressions.
 - Drop the criterion of no whitespace.
 - Also, refactor the logic so that tokens are converted to COLONeol
   only on demand.
Since we now convert to COLONeol only on demand, there's no need to
conert back in `isColon`.
@odersky odersky force-pushed the change-fewerbraces-1 branch from 3bc1ff2 to 69c4ad5 Compare May 24, 2022 15:56
Under 3.0-migration

    { x: T =>
      expr
    }

Is legal code but it's forbidden for later versions. If `fewerBraces` was the default, that code
would be ambiguous.
@julienrf
Copy link
Contributor

julienrf commented May 24, 2022

I think the proximity with type ascriptions is too close:

def f(x: Int)(y: Int): Int
def g(x: Int): Int => Int

// type ascription
f(42): Int => Int
// lambda argument
g(42): (x: Int) =>
  x + 1
// inline lambda argument
g(42): (x: Int) => x + 1

// puzzle…
trait X:
  type Y
  def Y: Y

val k: (x: X) => x.Y // type ascription or lambda argument?
// even worse
def z(n: Int): (x: X) => x.Y
z(42): (x: X) => x.Y // type ascription or lambda argument?

@julienrf
Copy link
Contributor

In Scala 2, we could not write:

xs.foldLeft(0)((x, y) =>
  val z = x + y
  z + 1
)

This is one of the reasons why it was convenient to use braces instead of the usual application with parens:

xs.foldLeft(0) { (x, y) =>
  val z = x + y
  z + 1
}

But now, even without fewerBraces, it is possible in Scala 3 to write the first form.

I don’t think we should get rid of the parens, which are anyway required for “non-lambda” arguments.

Co-authored-by: Julien Richard-Foy <[email protected]>
@odersky
Copy link
Contributor Author

odersky commented May 24, 2022

@julienrf Note that lambda arguments in the version must be multi-line with a => at the end of the first line. Furthermore the function type in ascriptions must be in parentheses unless the whole expression appears in parentheses. That means many of the examples are neither type ascription nor lambda argument. I agree we have a problem if we allow single-line lambdas, though.

I have probably hit 200 times this test instead of the real Parsers.scala when trying to open it. Enough!
odersky added 4 commits May 25, 2022 11:05
We already allow `: indent` there. For consistency we should allow
`: params => indent` as well.
Drop strings and quoted idents. That leaves:

 - alphanumeric identifiers, backticked identifiers, this, super, `)`, `]`

This choice was made to keep the extension minimal. Quite a few files in the
intent project have to be changed since they all use an idiom like

  "Formatting":
    ...

with a colon after a string literal. We can discuss separately whether we want to support this,
but IMO there's no pressing need to do so.
Drop fewerbraces.md, it's now fully part of indentation.md.
@odersky odersky force-pushed the change-fewerbraces-1 branch from 6300df2 to b1a6867 Compare May 26, 2022 07:37
odersky added 2 commits May 26, 2022 10:16
There was quite some breakage over time where one version but not the other was edited.
@odersky
Copy link
Contributor Author

odersky commented May 26, 2022

Fixes #15082

@odersky odersky linked an issue May 26, 2022 that may be closed by this pull request
@odersky
Copy link
Contributor Author

odersky commented May 28, 2022

I think this is a clear win over what we had before. If there are no objections I am going to merge this tomorrow.

@odersky odersky merged commit 135500a into scala:main May 30, 2022
@odersky odersky deleted the change-fewerbraces-1 branch May 30, 2022 08:23
@Kordyjan Kordyjan modified the milestones: 3.2.0-RC1, 3.2.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fewerBraces lambdas cannot have _ arguments
4 participants