This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Emacs scala-mode indentation of if statement

5 replies
j.romildo
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.

Hello.

This is how the Emacs scala mode indents the body of the if statement
when putting the braces on their own line:

if (args.length == 0)
{
print("input file name: ")
readLine
}
else
args(0)

I would like to have the braces automatically aligned with the if
keyword, as I have on my C programs.

if (args.length == 0)
{
print("input file name: ")
readLine
}
else
args(0)

Can that be "fixed" in the source code for scala-mode?

By the way, I do not like the common placement of braces the Scala
community seems to have adopted (the open brace is put on the end of the
line of the last token). I think that braces that are put on their own
line, aligned on the same column, makes the code much easier to read.

Regards,

José Romildo
--
Computer Science Department
Federal University of Ouro Preto - Brazil

daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 15 hours ago.
Re: Emacs scala-mode indentation of if statement
The brace style chosen by the Scala community reflects the de facto standard used in Java.  While Sun's official style guidelines are silent on this point, most Java programmers will place the opening brace on the same line as its control structure (rather than aligned with its closing brace).  In Java, this is just a stylistic issue, but in Scala, certain syntactic constructs (such as closures) may look a little odd with K&R or GNU style.  Further (and more seriously), Scala's semi-colon inference makes next-line braces problematic.  I believe that sufficient hacks have been devised to avoid anything truly bizarre, but the fact remains that certain constructs in next-brace style rely a little too heavily on parser magic.  For example:

def withLock(lock: Lock)(f: =>Unit) = ...

withLock(lock)
{
  ...
}

So, are the braces following withLock representative of its by-name parameter, or are they a separate block expression?  Both interpretations are legal in Scala.  It is only because the Scala parser will attempt to greedily consume the next line that the K&R folks don't experience more problems (such as this one).  For the record, Scala's parser will interpret the above as the by-name parameter, which is likely how it was intended.  The point is that there are some legitimate linguistic reasons to choose same-line style over next-line in Scala.

Daniel

On Thu, Mar 26, 2009 at 4:35 AM, <j.romildo@gmail.com> wrote:
Hello.

This is how the Emacs scala mode indents the body of the if statement
when putting the braces on their own line:

 if (args.length == 0)
   {
     print("input file name: ")
     readLine
   }
 else
   args(0)

I would like to have the braces automatically aligned with the if
keyword, as I have on my C programs.

 if (args.length == 0)
 {
   print("input file name: ")
   readLine
 }
 else
   args(0)

Can that be "fixed" in the source code for scala-mode?

By the way, I do not like the common placement of braces the Scala
community seems to have adopted (the open brace is put on the end of the
line of the last token). I think that braces that are put on their own
line, aligned on the same column, makes the code much easier to read.

Regards,

José Romildo
--
Computer Science Department
Federal University of Ouro Preto - Brazil

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Emacs scala-mode indentation of if statement

Adjust to the Scala way, or you will cause unwarranted bugs and
confusing compile errors.

Code with next-line braces, as I wrote for years, focuses too much on
structure and not enough on intent. A spell of Common Lisp cured me.

2009/3/26 :
> Hello.
>
> This is how the Emacs scala mode indents the body of the if statement
> when putting the braces on their own line:
>
>  if (args.length == 0)
>    {
>      print("input file name: ")
>      readLine
>    }
>  else
>    args(0)
>
> I would like to have the braces automatically aligned with the if
> keyword, as I have on my C programs.
>
>  if (args.length == 0)
>  {
>    print("input file name: ")
>    readLine
>  }
>  else
>    args(0)
>
> Can that be "fixed" in the source code for scala-mode?
>
> By the way, I do not like the common placement of braces the Scala
> community seems to have adopted (the open brace is put on the end of the
> line of the last token). I think that braces that are put on their own
> line, aligned on the same column, makes the code much easier to read.
>
> Regards,
>
> José Romildo
> --
> Computer Science Department
> Federal University of Ouro Preto - Brazil
>

daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 15 hours ago.
Re: Emacs scala-mode indentation of if statement
Just to be clear -- because I know this sounds like pointless style-pushing -- the Scala language spec really doesn't like the next-line brace style, mainly because of the semi-colon inference.  We're not trying to force the use of our preferred style over yours, just trying to save you a lot of time in the long run.  :-)

Daniel

P.S. Ricky wrote an article about exactly this issue: http://rickyclarkson.blogspot.com/2008/09/my-scala-coding-style.html

On Thu, Mar 26, 2009 at 5:54 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
Adjust to the Scala way, or you will cause unwarranted bugs and
confusing compile errors.

Code with next-line braces, as I wrote for years, focuses too much on
structure and not enough on intent.  A spell of Common Lisp cured me.

2009/3/26  <j.romildo@gmail.com>:
> Hello.
>
> This is how the Emacs scala mode indents the body of the if statement
> when putting the braces on their own line:
>
>  if (args.length == 0)
>    {
>      print("input file name: ")
>      readLine
>    }
>  else
>    args(0)
>
> I would like to have the braces automatically aligned with the if
> keyword, as I have on my C programs.
>
>  if (args.length == 0)
>  {
>    print("input file name: ")
>    readLine
>  }
>  else
>    args(0)
>
> Can that be "fixed" in the source code for scala-mode?
>
> By the way, I do not like the common placement of braces the Scala
> community seems to have adopted (the open brace is put on the end of the
> line of the last token). I think that braces that are put on their own
> line, aligned on the same column, makes the code much easier to read.
>
> Regards,
>
> José Romildo
> --
> Computer Science Department
> Federal University of Ouro Preto - Brazil
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Emacs scala-mode indentation of if statement

From that article:

"and never place opening OR closing parens on their own lines"

I have relented and now place closing parens on their own lines if the
opening paren was the last token on its line.

for {

}

but:

for { x <- xs
y <- ys }

[proportional fonts probably screwed that up]

2009/3/26 Daniel Spiewak :
> Just to be clear -- because I know this sounds like pointless style-pushing

daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 15 hours ago.
Re: Emacs scala-mode indentation of if statement
Example of closing paren (and another example of circumventing Scala's semicolon inference):

def e = t e

def t = (
    "fn" ~ ID ~ "=>" ~ e
  | ID
)

Notice that `t` is using parentheses and not braces.

For the most part though, I do prefer the Lisp style of parens at the end of the last line they enclose.  Braces I send to the next line (so a closing brace always has its own line).

Daniel

On Thu, Mar 26, 2009 at 6:57 AM, Ricky Clarkson <ricky.clarkson@gmail.com> wrote:
From that article:

"and never place opening OR closing parens on their own lines"

I have relented and now place closing parens on their own lines if the
opening paren was the last token on its line.

for {

}

but:

for { x <- xs
     y <- ys }

[proportional fonts probably screwed that up]

2009/3/26 Daniel Spiewak <djspiewak@gmail.com>:
> Just to be clear -- because I know this sounds like pointless style-pushing
> -- the Scala language spec really doesn't like the next-line brace style,
> mainly because of the semi-colon inference.  We're not trying to force the
> use of our preferred style over yours, just trying to save you a lot of time
> in the long run.  :-)
>
> Daniel
>
> P.S. Ricky wrote an article about exactly this issue:
> http://rickyclarkson.blogspot.com/2008/09/my-scala-coding-style.html
>
> On Thu, Mar 26, 2009 at 5:54 AM, Ricky Clarkson <ricky.clarkson@gmail.com>
> wrote:
>>
>> Adjust to the Scala way, or you will cause unwarranted bugs and
>> confusing compile errors.
>>
>> Code with next-line braces, as I wrote for years, focuses too much on
>> structure and not enough on intent.  A spell of Common Lisp cured me.
>>
>> 2009/3/26  <j.romildo@gmail.com>:
>> > Hello.
>> >
>> > This is how the Emacs scala mode indents the body of the if statement
>> > when putting the braces on their own line:
>> >
>> >  if (args.length == 0)
>> >    {
>> >      print("input file name: ")
>> >      readLine
>> >    }
>> >  else
>> >    args(0)
>> >
>> > I would like to have the braces automatically aligned with the if
>> > keyword, as I have on my C programs.
>> >
>> >  if (args.length == 0)
>> >  {
>> >    print("input file name: ")
>> >    readLine
>> >  }
>> >  else
>> >    args(0)
>> >
>> > Can that be "fixed" in the source code for scala-mode?
>> >
>> > By the way, I do not like the common placement of braces the Scala
>> > community seems to have adopted (the open brace is put on the end of the
>> > line of the last token). I think that braces that are put on their own
>> > line, aligned on the same column, makes the code much easier to read.
>> >
>> > Regards,
>> >
>> > José Romildo
>> > --
>> > Computer Science Department
>> > Federal University of Ouro Preto - Brazil
>> >
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland