Skip to content

Tweaks to indent syntax #7363

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
Oct 10, 2019
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Oct 3, 2019

  • Cleanup syntax.md
  • Allow significant indentation after if.
  • Allow with in front of templates and make indentation significant after it.
  • Allow with in front of type refinements and make indentation significant after it.
  • Simplify syntax of given instances for extension methods

There's one scenario where this breaks:

```
if
  (...
   ...)
  println()
else if ...
```
This will be interpreted as
```
if {
  (...
   ...)
  println()
}
else if ...
```
and will fail with `then` expected at the point of the `else`.

An example like this occurred in `SymDenotations.scala`.
Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

... and make indentation significant after it.
The alternative looks really ugly after switching eo `with` syntax.
Maybe we should disallow it?
@odersky odersky force-pushed the change-indent-with branch from 086979f to edddda1 Compare October 4, 2019 11:09
 - All parameters go in the front
 - remainde consists only of methods
 - indentation is significant

Example:

```scala
  given [T](xs: List[T])
    def second = xs.tail.head
    def third = xs.tail.tail.head
```
A `return` is not necessarily followed by anything. So our general principles
indicate that indentation should not be significant after it, since otherwise
an off-by-one-space error could change meaning.
@odersky odersky force-pushed the change-indent-with branch from 8f1dde9 to 4bb212b Compare October 5, 2019 11:12
Allow multi-variable pattern declarations of the form

    val p1, ..., pn: T
@godenji
Copy link

godenji commented Oct 5, 2019

Where did with come from?

Thought that line ending + indent would suffice for object/trait/class/enum definitions. Seems very strange to end every line with with.

@odersky
Copy link
Contributor Author

odersky commented Oct 5, 2019

See #7136 for a discussion, in particular the last comments.

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM

Some points for future consideration:

Self syntax

Regarding the self syntax, if we indent them as follows (no indent), it does not look bad, and it's easier to parse what's the self requirement:

trait T with
self: C =>
  ...

The syntax for patmat

Given that we have

  enum Option[+T] with
    case Some(x: T) extends Option[T]
    case None       extends Option[Nothing]

Should we change the syntax of patmat to:

  match opt with
    case Some(x) =>
    case None    =>

This change will also align with the patmat syntax of ML-family of languages.

The syntax for given

Programmers will get confused about whether to use with or not when a list of non-local definitions follows:

  given intOrd: Ord[Int] with
    def (x: Int) compareTo (y: Int) =
      if (x < y) -1 else if (x > y) +1 else 0

  given stringOps: (xs: Seq[String])
    def longestStrings: Seq[String] =
      val maxLength = xs.map(_.length).max
      xs.filter(_.length == maxLength)

class A extends B with
C
```
becomes illegal since `C` above would be terated as a nested statement inside `A`. More generally, a `with` that separates parent constructors cannot be at the end of a line. One has to write
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: terated

@liufengyun
Copy link
Contributor

liufengyun commented Oct 8, 2019

Inspired by this PR, I'm thinking a "more natural" syntax for extension methods. The idea is that templates can be augmented with extension clauses, i.e.:

Extension         ::=  ‘extends‘   ‘(‘ id ‘:‘ Type ‘)‘  ‘{’ DefDef { DefDef } ‘}’
ClassDef          ::=  id ClassConstr [Template] {Extension}
ObjectDef         ::=  id [Template] {Extension}

For example, we can write the following code:

trait Ord[T] extends (x: T) with
  def <(y: T) = x.compareTo(y) < 0
  def >(y: T) = x.compareTo(y) > 0
  def compareTo(y: T): Int
end Ord

given IntOps: Ord[Int] extends (x: Int) with
  def compareTo(y: T): Int = x - y

// a bunch of extension methods
given StringOps extends (a: String) with
  ...

// parameterized given
given AnyOps[T] extends (a: T) with
  ...

// Flags example
object Flags with
  ...
extends (x: FlagSet) with
  def bits: Long = opaques.toBits(x)
  def | (y: FlagSet): FlagSet = ...
end Flags

// or, split the definition into a separate trait
trait FlagSetOps extends (x: Flags.FlagSet) with
  def bits: Long = opaques.toBits(x)
  def | (y: Flags.FlagSet): FlagSet = ...

object Flags extends FlagSetOps with
   ...

Advantage:

  1. It justifies the magic implicit resolution rule for extension methods: why an extension method is available when a given instance enclosing the method is present?

  2. It clears any doubt about the referent of this inside extension methods. This is a problem for current syntax, as in template-like context, we expect this to be something different from the outer context.

  3. There is no need for the problematic infix syntax, e.g.,def (self: String) + (that: String): String.

@odersky odersky merged commit dacfba8 into scala:master Oct 10, 2019
@odersky odersky deleted the change-indent-with branch October 10, 2019 15:45
@anatoliykmetyuk anatoliykmetyuk added this to the 0.20 Tech Preview milestone Oct 31, 2019
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.

5 participants