Skip to content
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

Tweaks to indent syntax #7363

merged 16 commits into from Oct 10, 2019


Copy link

odersky commented Oct 3, 2019

  • Cleanup
  • 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
odersky added 4 commits Oct 3, 2019
There's one scenario where this breaks:

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

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

dotty-bot left a comment

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

Have an awesome day! ☀️

odersky added 4 commits Oct 3, 2019
... 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 dotty-staging:change-indent-with branch from 086979f to edddda1 Oct 4, 2019
odersky added 4 commits Oct 4, 2019
 - All parameters go in the front
 - remainde consists only of methods
 - indentation is significant


  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 dotty-staging:change-indent-with branch from 8f1dde9 to 4bb212b Oct 5, 2019
@odersky odersky requested a review from liufengyun Oct 5, 2019
odersky added 2 commits Oct 5, 2019
Allow multi-variable pattern declarations of the form

    val p1, ..., pn: T

This comment has been minimized.

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.


This comment has been minimized.

Copy link
Contributor Author

odersky commented Oct 5, 2019

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

Copy link

liufengyun left a comment


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.filter(_.length == maxLength)
class A extends B with
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

This comment has been minimized.

Copy link

liufengyun Oct 8, 2019


typo: terated


This comment has been minimized.

Copy link

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


  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 lampepfl:master Oct 10, 2019
2 checks passed
2 checks passed
CLA User signed CLA
continuous-integration/drone/pr Build is passing
@odersky odersky deleted the dotty-staging:change-indent-with branch Oct 10, 2019
@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
None yet
5 participants
You can’t perform that action at this time.