Skip to content

Commit

Permalink
hclsyntax: Fix incorrect examples of object expressions in the spec
Browse files Browse the repository at this point in the history
In the text talking about how to resolve the ambiguity between "for"
expressions and values/keys called "for" there was previously an incorrect
statement about the behavior of "(for)" as an object key.

That situation causes HCL to take the value of a variable named "for".
To get the literal string "for" we must instead write the name in quotes,
similarly to how we'd represent a key string that includes characters
which HCL doesn't admit into an identifier.
  • Loading branch information
apparentlymart committed Jun 24, 2021
1 parent 327f3ce commit a97795a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions hclsyntax/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,20 @@ Between the open and closing delimiters of these sequences, newline sequences
are ignored as whitespace.

There is a syntax ambiguity between _for expressions_ and collection values
whose first element is a reference to a variable named `for`. The
_for expression_ interpretation has priority, so to produce a tuple whose
first element is the value of a variable named `for`, or an object with a
key named `for`, use parentheses to disambiguate:
whose first element starts with an identifier named `for`. The _for expression_
interpretation has priority, so to write a key literally named `for`
or an expression derived from a variable named `for` you must use parentheses
or quotes to disambiguate:

- `[for, foo, baz]` is a syntax error.
- `[(for), foo, baz]` is a tuple whose first element is the value of variable
`for`.
- `{for: 1, baz: 2}` is a syntax error.
- `{(for): 1, baz: 2}` is an object with an attribute literally named `for`.
- `{baz: 2, for: 1}` is equivalent to the previous example, and resolves the
- `{for = 1, baz = 2}` is a syntax error.
- `{"for" = 1, baz = 2}` is an object with an attribute literally named `for`.
- `{baz = 2, for = 1}` is equivalent to the previous example, and resolves the
ambiguity by reordering.
- `{(for) = 1, baz = 2}` is an object with a key with the same value as the
variable `for`.

### Template Expressions

Expand Down

0 comments on commit a97795a

Please sign in to comment.