steve_gallagher
steve_gallagher

Reputation: 3898

Coffeescript: Unexpected '{' when I try to create an object literal of object literals

For this syntax:

  BASE_RUNNERS = {  basesLoaded:      { first: "manned",  second: "manned", third: "manned" },
                    firstAndSecond:   { first: "manned",  second: "manned", third: "empty"  },
                    firstAndThird:    { first: "manned",  second: "empty",  third: "manned" },
                    secondAndThird:   { first: "empty",   second: "manned", third: "manned" },
                    first:            { first: "manned",  second: "empty",  third: "empty"  },
                    second:           { first: "empty",   second: "manned", third: "empty"  },
                    third:            { first: "empty",   second: "empty",  third: "manned" },
                    empty:            { first: "empty",   second: "empty",  third: "empty"  }
                  }

I receive the error:

[stdin]:154:27: error: unexpected {
                          firstAndSecond:   { first: "manned",  second: "manned", third: "empty",   addedScore: 0 },
                          ^

Not sure why, it looks legal to me.

Upvotes: 0

Views: 569

Answers (2)

mu is too short
mu is too short

Reputation: 434745

The braces are not the problem, the problem is the non-CoffeeScript indentation. CoffeeScript is very sensitive to whitespace, even when you supply the optional braces you still need to be careful that your indentation matches the desired block structure. The confusion goes away if you write it this way:

BASE_RUNNERS = {
  basesLoaded:      { first: "manned",  second: "manned", third: "manned" },
  firstAndSecond:   { first: "manned",  second: "manned", third: "empty"  },
  firstAndThird:    { first: "manned",  second: "empty",  third: "manned" },
  secondAndThird:   { first: "empty",   second: "manned", third: "manned" },
  first:            { first: "manned",  second: "empty",  third: "empty"  },
  second:           { first: "empty",   second: "manned", third: "empty"  },
  third:            { first: "empty",   second: "empty",  third: "manned" },
  empty:            { first: "empty",   second: "empty",  third: "empty"  }
}

The source of your difficulty was a non-indented basesLoaded combined with the indentation for the rest of the keys.

Upvotes: 3

steve_gallagher
steve_gallagher

Reputation: 3898

Coffeescript doesn't accept the valid Javascript syntax, I had to rewrite it to this:

 BASE_RUNNERS =
      basesLoaded:    first: "manned", second: "manned", third: "manned"
      firstAndSecond: first: "manned", second: "manned", third: "empty"
      firstAndThird:  first: "manned", second: "empty", third: "manned"
      secondAndThird: first: "empty", second: "manned", third: "manned"
      first:          first: "manned", second: "empty", third: "empty"
      second:         first: "empty", second: "manned", third: "empty"
      third:          first: "empty", second: "empty", third: "manned"
      empty:          first: "empty", second: "empty", third: "empty"

which I'm not sure is better; looks a little worse IMHO.

Upvotes: 1

Related Questions