News for hackers. Stuff that matters.
Re:Ask HN: Why use semicolons to terminate statements in mod... by axod on Monday November 24, 2008 @04:16AM (#374612)
Leaving out semicolons leads to errors. Ambiguity etc...

return

[1,2,3]

What should that do? Should it return, or return [1,2,3]? There are many other examples. Use semicolons and there is no debate over what should happen. Making them optional in javascript was a ridiculous idea.

Re:Ask HN: Why use semicolons to terminate statements in mod... by cabalamat on Monday November 24, 2008 @05:27AM (#374655)
I agree. You often get multi-line statements and in many languages/APIs they're a nature way of doing things. Here's an example (in no particular language):

   win = Window.new()
      .setTitle("A new window")
      .setForeground(Red)
      .setBackground(White)
      .setMenuBar(fileMenu, viewMenu, helpMenu);
I think this would be uglier and more error-prone if instead you had to use \ or some other character to indicate they are all part of the same expression:

   win = Window.new()\
      .setTitle("A new window")\
      .setForeground(Red)\
      .setBackground(White)\
      .setMenuBar(fileMenu, viewMenu, helpMenu);
Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @05:45AM (#374668)
Interesting. I hadn't come across that idiom before. In Python, you'll have things like:

  myLongList = [1, 2, 3,
                4, 5, 6,
                7, 8, 9]
No continuation character is needed, because the continuation is implied.

However, I can't think of a direct parallel to your example above. It looks handy. What API/language are you alluding to?

I guess the Pythonic way would be something like:

  win = Window(title = "A new window",
               foreground = "Red",
               background = "White",
               menuBar = [fileMenu, viewMenu, helpMenu])
But I disgress. :^)
Re:Ask HN: Why use semicolons to terminate statements in mod... by sc on Monday November 24, 2008 @08:13AM (#374754)
JavaScript can just as easily attain this way of using named parameters:

  win = Window.new({title: "A new window",
                    foreground: Red,
                    background: White,
                    menuBar: [fileMenu, viewMenu, helpMenu]});
Re:Ask HN: Why use semicolons to terminate statements in mod... by cabalamat on Tuesday November 25, 2008 @03:13AM (#376161)
What API/language are you alluding to?

As i said before: no particular language.

Re:Ask HN: Why use semicolons to terminate statements in mod... by sc on Monday November 24, 2008 @08:15AM (#374755)
The dot notation itself indicates continuation:

  win = new Window().
        setTitle("A new window").
        setForeground(Red).
        setBackground(White).
        setMenuBar(fileMenu, viewMenu, helpMenu);
Re:Ask HN: Why use semicolons to terminate statements in mod... by lacker on Monday November 24, 2008 @07:21PM (#375728)
Here's a more plausible example. Let's say you are trying to call one function out of a list of functions. What do you think this code does?

  function getIndex() {
    return 0  
  } 
  function f1() {
    alert("foo") 
  } 
  function f2() {
    alert("bar") 
  } 

  var intIndex = getIndex() 
  [f1, f2][intIndex]()

It looks like it should alert "foo", but run it and you get an "intIndex is undefined" error. Good luck debugging that!

The problem is, the javascript rules for continuing lines are that whenever the next character is any of [({+-, the line is assumed to continue. This is generally correct but not always. For comparison, the analogous Python rule is that a line continues either with an explicit \ or when one of [({ has not yet been closed with its pair. The Python rule doesn't lead to any ambiguous cases that I am aware of, because unclosed parentheses would just be invalid syntax.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Tuesday November 25, 2008 @01:06AM (#376075)
Interesting. I've encountered errors in a similar vein, which in retrospect must have been caused by using an anonymous list in a similar way to your example:

  [f1, f2][intIndex]()
The 'solution' seems to be something like

  var functions = [f1, f2]
  functions[intIndex]()
but that's a big loss for conciseness.
Re:Ask HN: Why use semicolons to terminate statements in mod... by lacker on Tuesday November 25, 2008 @04:45PM (#376857)
The most convenient solution is just using a semicolon. If the line continuation rules were the Python ones, this would "just work"....
Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @04:51AM (#374637)
Keep in mind that I come from a mainly Python and Ruby background; both of those languages have always had newline-terminated statements. Yes, neither Python nor Ruby nor newline-terminated Javascript would return the desired result. But neither language seems to be suffering hugely as a result. (As far as I know.)

Why would you write a return statement like that in the first place? (Not intended as flamebait - I am genuinely curious to know.) If it is a simple typo that separates the 'return' from the '[1,2,3]', then isn't the negligent typist just as likely to leave off the all-important semicolon? Or is there a genuine reason you might want to put the [1,2,3] on a line by itself?

Re:Ask HN: Why use semicolons to terminate statements in mod... by halo on Monday November 24, 2008 @06:04AM (#374682)
JavaScript syntax lets you write statements over multiple lines - if the first line cannot be parsed correctly it assumes you meant to continue over to the second line. This leads to odd error messages and JavaScript being harder to debug without semicolons.

Note that this is different to, say, Python, where newline explicitly indicates termination of a statement.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @06:28AM (#374698)
Well, Python will let certain statements continue over multiple lines as well, e.g.

  someFunction(parameterOne,
               parameterTwo)
I think maybe the difference is that Python has always treated newlines the way it does now. JavaScript, on the other hand, has developed idioms based on the semicolon model. So has the rug has been snatched out from underneith its users by suddenly making them optional? Does the change make a lot of useful idioms harder to implement?
Re:Ask HN: Why use semicolons to terminate statements in mod... by axod on Monday November 24, 2008 @06:03AM (#374680)
ok here's a more usual situation in which the programmer is blindly using their favorite bracketing style, and come across the issue...

function getObject()

{

    return       // returns undefined, not the object.

    {

        age: 30,

        name: "axod"

    }
}

Personally I hate that style of bracketing, but some swear by it.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @06:21AM (#374695)
Yeah, not really my favourite bracketing style either. Good example though - it is conceivable that this could happen.
Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @08:16AM (#374758)
Making them optional in javascript was a ridiculous idea.

But since they are optional now, using them throughout our code won't help us for the example above: it would still wrongly return undefined because of the implied statement end. So it could be argued that semicolons provide a false sense of security in this new, semicolon-optional world. :^)