-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Tweaks to indent syntax #7363
Conversation
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`.
There was a problem hiding this 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:
- Separate subject from body with a blank line
- When fixing an issue, start your commit message with
Fix #<ISSUE-NBR>:
- Limit the subject line to 72 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line ("Add" instead of "Added")
- Wrap the body at 80 characters
- 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?
086979f
to
edddda1
Compare
- 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.
8f1dde9
to
4bb212b
Compare
Crashed in pickler before.
Allow multi-variable pattern declarations of the form val p1, ..., pn: T
Where did Thought that line ending + indent would suffice for object/trait/class/enum definitions. Seems very strange to end every line with |
See #7136 for a discussion, in particular the last comments. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: terated
Inspired by this PR, I'm thinking a "more natural" syntax for extension methods. The idea is that templates can be augmented with
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:
|
if
.with
in front of templates and make indentation significant after it.with
in front of type refinements and make indentation significant after it.