- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Emacs scala-mode indentation of if statement
Thu, 2009-03-26, 11:16
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
Thu, 2009-03-26, 11:57
#2
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
>
Thu, 2009-03-26, 12:17
#3
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:
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
>
Thu, 2009-03-26, 13:07
#4
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
Thu, 2009-03-26, 14:07
#5
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:
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
>> >
>
>
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: