Parentheses instead of curly-braces around case-lambdas?

115 views
Skip to first unread message

Luke Vilnis

unread,
May 25, 2012, 8:25:56 AM5/25/12
to scala-debate
Apologies if this has been asked before - I did a brief search and couldn't find anything.

Would it be possible to allow case-lambdas within parentheses (instead of braces) in a future version of Scala?

The idea is just to allow "(case pat (if cond)? => expr)" to mean the same as "{ case pat (if cond)? => expr }", at least when inside an argument list (but hopefully anywhere inside parentheses). So we could write things like:

Map("asd" -> 123).map(case (k, v) => (k, v + 1))

or even lose the parentheses when in a multi-argument list:

multiArgumentMethod(case (x, hd::tl) => x::tl, case (x, Nil) => x::Nil)

or whatever. For ambiguity's sake, we couldn't allow it outside of parentheses, and maybe not outside of argument lists at all (but I don't think we'd have to go that far):

val f = case (a, b) => a + b // No!
val f = (case (a, b) => a + b) // Maybe?

As far as I can see, this shouldn't be ambiguous in the grammar, but I could definitely be missing something.

Thanks,
Luke 

P.S. Justification for additional syntax: I find it kind of irritating that every lambda syntax besides case-lambdas allows both paren- and brace-surrounded forms. Additionally, you can't use curly braces to surround multiple-argument-lists. This creates a nasty tension where it seems like there is no way to have consistent code style when passing arguments in Scala - sometimes you need curly braces, sometimes you need parens, which requires (the horror!) _thinking_. Obviously I would prefer to minimize thinking as much as possible (I'm sort of kidding but I have found that the extra cognitive load of all the formatting decisions you're allowed to make in Scala can be distracting, even though I appreciate the flexibility).

Erik Post

unread,
May 25, 2012, 2:03:08 PM5/25/12
to scala-...@googlegroups.com
+1 

I used to be bothered by this in my very early days of using Scala. I don't really notice it anymore now, but explaining things like this to my colleagues was a royal pain in the you-know-what. For me personally, it would be a nice to have, but I think it would be a big win for all the Scala newbies. it might also might fit in nicely with Martin's suggestions for cleaning up if/else and similar constructs.

Cheers,
Erik
Reply all
Reply to author
Forward
0 new messages