Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I do this all the time in Groovy and don't use semicolons...


> I do this all the time in Groovy and don't use semicolons

Groovy's semicolon-less mode has lots of syntax restrictions. E.g.this doesn't compile...

  println true
   ? "yes" : "no"


> Groovy's semicolon-less mode has lots of syntax restrictions. E.g.this doesn't compile...

I would't say 'lots'. It pretty much boils down to one thing: 'leave the operator on the previous line'

println true ?

        "yes" :

        "no"
For example is just fine.


This compiles and runs OK in Java...

  class Hello{
    public static void main(String[] args){
      System.out.println( true
       ? "yes" : "no" );
    }
  }
In order to work without semicolons, Groovy's grammar must restrict some things that work in Java. Semicolon-less mode in Groovy has a price.


That compiles just fine in groovy as well, the parathesis of the println call delimit the statement. Same is true for other delimiters:

  reportData = orders.collectMany {it.items}
                     .groupBy {it.product}
                     .collect { key,value -> [product: key,
                                               vendor: key.vendor,
                                                cases: value.sum() {it.actualQty},
                                        productCharge: value.sum() {it.totalProductCharge},
                                       platformCharge: value.sum() {it.portalCharge},
                                       shippingCharge: value.sum() {it.shippingCharge},
                                          dueProducer: value.sum() {it.totalDueProducer()}]}
Is perfectly valid groovy, spread on multiple lines with no semicolons.

You seem to be making this out like its some huge inconvenience. It is very rarely an issue and its not like there are 5000 things you can't do. Its really just line breaks in statements that aren't delimited any other way that are effected and all you have to do it put the operator on the line prior to the break...


Ok, I've always wondered about that '?' operator. What's the point?

It was said (at the time) that C-like syntax meant a program was a series of expression, not statements. So something like

   (void)1;
is valid syntax. Even compound expressions are valid:

   a=5, b=6;
Where I feel abandoned (betrayed?) is this: why then aren't compound statements also expressions? E.g.

   {
   int x=Foo(50);
   Bar(x);
   }
The value returned could just be the value of the final executed expression (Bar yields a value). No need for 'return' at all.

And no need for '?'.

   int x = { if (i > j) 5; else 6; }


You're right about statements being expressions and returning the last executed expression. In fact, since using Clojure I've gotten used to these, e.g.

  (-> (Foo 50) (Bar))
  (def x (if (> i j) 5 6))
I do miss the static typing and more readable syntax, though, and have appreciated Haskell in this regard. Too bad things like powerful macros and convenient syntax don't seem to gel together well in the same language.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: