Array Literals

Shane Brinkman-Davis edited this page Mar 12, 2017 · 1 revision

CaffeineScript makes structured literals lightweight and fun! Examples:

# Array of 3-element Arrays
a = 
  "Bob"    1976  :male
  "Alice"  1978  :female

# Array of 3 objects
b = 
  foo: 1
  foo: 2
  foo: 3

Arrays Literals

# JavaScript-style - 19 tokens
a = [
  ["Bob",   1976, "male"],
  ["Alice", 1978, "female"]
]

# matchless: blocks and one-liners
a = []
  [] "Bob",   1976, :male
  [] "Alice", 1978, :female

# implicit arrays
a = 
  "Bob",   1976, :male
  "Alice", 1978, :female

# implicit commas - 8 tokens
a = 
  "Bob"    1976  :male
  "Alice"  1978  :female

All Array Literal Forms for 1D Arrays

# array one-liners
a =   [1, 2, 3]
a = [] 1, 2, 3  # one-liner array without bracket-matching
a =    1, 2, 3  # array is implicit when there are 2 or more elements
a =    1  2  3  # implicit commas after number and string literals

# array block
a = []
  1
  2
  3

# implicit array block
a = 
  1
  2
  3
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.