yamllanguage-design

Why does the YAML spec mandate a space after the colon?


The YAML spec clearly states:

Mappings use a colon and space (“: ”) to mark each key: value pair.

So this is legal:

foo: bar

But this, Ain't:

foo:bar

I see many people online that are ranting about the space. I think they have a point. I got burned by it several times myself.

Why is the space mandatory? What was the design consideration behind it?


Solution

  • It's easy to miss, because that specification uses the bizarre convention of only highlighting the last character of an internal link, but the “: ” in the section you quote is actually a link to another section of the specification which answers your question:

    Normally, YAML insists the “:” mapping value indicator be separated from the value by white space. A benefit of this restriction is that the “:” character can be used inside plain scalars, as long as it is not followed by white space. This allows for unquoted URLs and timestamps. It is also a potential source for confusion as “a:1” is a plain scalar and not a key: value pair.

    So the motivation is that you can write lists such as this without requiring any quoting:

    useful_values:
    - 2:30
    - http://example.com
    - localhost:8080
    

    If the space was optional, this could end up being ambiguous, and interpreted as a set of key-value pairs.

    Aside: Here's a snippet of JS to make the link formatting on that document less useless.

    document.styleSheets[0].insertRule('a[href^="#"] { color: #00A !important; text-decoration: underline !important; background: none !important; }', 0);
    

    Update: The revised "1.2.2" specification, published in 2021, uses more conventional link formatting, but that particular hyperlink is no longer present. The wording quoted is unchanged, however.