NamedTuple

A NamedTuple is typically created with a named tuple literal:

  1. tuple = {name: "Crystal", year: 2011} # NamedTuple(name: String, year: Int32)
  2. tuple[:name] # => "Crystal" (String)
  3. tuple[:year] # => 2011 (Int32)

To denote a named tuple type you can write:

  1. # The type denoting a named tuple of x: Int32, y: String
  2. NamedTuple(x: Int32, y: String)

In type restrictions, generic type arguments and other places where a type is expected, you can use a shorter syntax, as explained in the type grammar:

  1. # An array of named tuples of x: Int32, y: String
  2. Array({x: Int32, y: String})

A named tuple key can also be a string literal:

  1. {"this is a key": 1}