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

Re: curly-brackets

No replies
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
On Thu, Feb 3, 2011 at 5:51 AM, Matthew Pocock <turingatemyhamster [at] gmail [dot] com> wrote:
Hi,
I've been using scala for about a year now, and find it a great language for actually getting stuff done. However, sometimes it suffers from java-esque death-by-curly--brackets. I've noticed that case statements seem to be able to infer blocks, removing our need to put in curly brackets. Is it possible to extend this to other contexts, based upon indentation? As an example that arises commonly in my code, would it cause ambiguities in parsing if curlies after the match, if andelse keywords could be inferred?
val a = x match // no curlies needed  case Option(a) => a match     case "Bob" => ...    case "Mark" => ...
  case b =>    if      ...       ...    else       ...println(a)
This seems much less cluttered than this:
val a = x match {  case Option(a) => a match {       case "Bob" => ...      case "Mark" => ...     }
  case b =>     if(bob) {      ...       ...    } else {       ...    }}
println(a)

If you want to reduce "clutter," I suggest you indent your braces to the level of the block, as in

if (foo)
    {
    bar1
    bar2
    }

or, better yet,

if (foo) {
    bar1
    bar2
    }

That way, the braces don't "stick out," and the indentation structure is the same as it would be without braces. IIRC, this is know as Whitesmith style. The more complicated the structure, the cleaner and clearer this structure is compared to the conventional style. I have never understood the conventional style of putting the braces at the header level rather than the block level.

A while back, I suggested that an optional Python-style syntax be considered for Scala. (I think Haskell has this feature too.) Well, apparently that is not quite technically feasible. The next best thing, I think, is to have the compiler check for inconsistency between the logical structure and the indentation structure, as in

if (foo)
    bar1
    bar2

or

if (foo) {
    bar1

bar2
}

This would eliminate a class of bugs, and it would also make it much simpler to find the location of a missing or extra brace in a large file.

--Russ P.

--
http://RussP.us

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