为什么YAML规范要求在冒号后加空格? [英] Why does the YAML spec mandate a space after the colon?

查看:83
本文介绍了为什么YAML规范要求在冒号后加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YAML规范明确指出:

映射使用冒号和空格(:")标记每个键:值对.

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

这是合法的:

foo: bar

但是,这不是

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?

推荐答案

很容易遗漏,因为该规范使用了仅突出显示内部链接的最后一个字符但使用:" 的奇怪约定.您引用的部分中的代码"实际上是规范,它可以回答您的问题:

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:

通常,YAML坚持使用空格将:"映射值指示符与值分隔开.此限制的好处是,:"字符可以在普通标量内使用,只要后面没有空格即可.这允许使用未引用的URL和时间戳.由于"a:1"是简单的标量,而不是键:值对,因此它也是造成混淆的潜在原因.

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.

旁边::以下是JS的一个片段,用于减少该文档上的链接格式的使用.

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);

这篇关于为什么YAML规范要求在冒号后加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆