Skip to content

Clarify the reason empty keys are allowed - #1095

Open
arp242 wants to merge 1 commit into
mainfrom
empty
Open

Clarify the reason empty keys are allowed#1095
arp242 wants to merge 1 commit into
mainfrom
empty

Conversation

@arp242

@arp242 arp242 commented Jun 29, 2026

Copy link
Copy Markdown
Member

TOML allows empty (quoted) keys:

"" = 123

Over the years I've seen a number of people express surprise that it's allowed.

The reason it's allowed is because TOML tables are intended to map to hash tables/dictionaries in programming languages, and they allow empty keys. So TOML does too.

You can still argue that it's a mistake to allow it in TOML, but we can't change that now and this at least clarifies the thinking behind why it's allowed.

@ChristianSi ChristianSi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may ultimately be better suited for a FAQ or rationale page, but since TOML does not yet have one, a short explanation in the spec itself seems appropriate enough. However, I’d be a bit more explicit about the underlying reasoning; see my suggested change.

Comment thread toml.md Outdated
You cannot use multi-line strings to define quoted keys.

TOML tables are intended to map to hash tables or dictionaries, as a consequence
empty quoted keys are allowed (though discouraged). A bare key must be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rationale feels a bit underspecified. Many languages and data structures that have hash tables or dictionaries also allow numbers, booleans, and other non-string values as keys, but TOML keys are intentionally more restricted than that. So, to prevent further possible confusion, I’d suggest making the rationale a bit more explicit, perhaps along these lines:

TOML tables are intended to map to hash tables or dictionaries, which commonly permit empty string keys. For that reason, empty quoted keys are allowed, though discouraged. A bare key must be non-empty.

@arp242 arp242 Jul 1, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

TOML tables correspond to hash tables or dictionaries which usually permit any string value. For that reason empty quoted keys are allowed, though discouraged. A bare key must be non-empty.

"intended to map to" → "correspond to" as it seems a bit less of a mouthful.

"commonly permit empty string keys" → "usually permit any string value" as that's a bit more general.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arp242 Sure, sounds good!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, changed it with hash tables or dictionarieshash tables (also known as dictionaries) because on re-reading the "or" could be interpreted to mean "one or the other" rather than "also known as".

TOML allows empty (quoted) keys:

	"" = 123

Over the years I've seen a number of people express surprise that it's
allowed.

The reason it's allowed is because TOML tables are intended to map to
hash tables/dictionaries in programming languages, and they allow empty
keys. So TOML does too.

You can still argue that it's a mistake to allow it in TOML, but we
can't change that now and this at least clarifies the thinking behind
why it's allowed.
@cyyynthia

This comment was marked as resolved.

@ChristianSi

Copy link
Copy Markdown
Contributor

@cyyynthia: For just one sentence added, a <details> block seems overkill, though.

@arp242

arp242 commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

Personally I think the "why" of these sort of things should be part of the main specification text. Over the years I've found that knowing why something is the way it is hugely helps actually understanding and remembering things. Otherwise it just become rote memorisation and I kind of suck at that.

Within reason, of course. A potential FAQ could expand on some of the things in the specification. But I do think it's helpful to explain the why, when it might be useful, as concise as possible, regardless of whether we have a FAQ or not.

I would certainly not be in favour of footnotes or <detail> tags. I avoid that in my own writing and generally dislike it as everyone reads them anyway and they give a disjointed reading experience. The exception being Sir Terry's footnotes because they're just so funny (I'd be okay with footnotes if they're sufficiently entertaining).

@dimbleby

Copy link
Copy Markdown

The reason it's allowed is because TOML tables are intended to map to hash tables/dictionaries in programming languages, and they allow empty keys. So TOML does too.

Is there evidence for this as an explanation? Or is it a story we are retro-fitting ten years later?

If it were a good explanation, wouldn't non-string keys also be allowed?

The relevant phrase was I think introduced at #387, which nominally was just moving text around. I don't find any discussion of whether or not to allow empty strings - perhaps something exists elsewhere? - so it is hard at this distance to say what the reasoning was.

FWIW: no explanation was offered at #432, which was nearly contemporary.

@arp242

arp242 commented Jul 19, 2026

Copy link
Copy Markdown
Member Author

It's in the text already: "Tables (also known as hash tables or dictionaries)". Older versions are even more explicit: "TOML is designed to map unambiguously to a hash table".

@dimbleby

Copy link
Copy Markdown

The question was not: is TOML designed to map to a hash table? I agree that it is. But this would hold regardless of whether empty strings were allowed as keys.

The question was more: is this either a historically accurate or currently useful explanation for why empty strings should be allowed as keys? I find it unnecessarily involved, eg as both I and - I now see - ChristianSi have experienced, talk of hash tables raises questions in reader minds about non-string keys.

To my mind a simpler explanation is just: empty strings are allowed because an empty string is a perfectly good string and there was no compelling reason to make an exception for it.

Whether that deserves a sentence in the spec is unclear to me.

@ChristianSi

ChristianSi commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@dimbleby: I agree – that’s a perfectly plausible explanation, and "TOML keys are conceptually strings" is a simpler way to put it than "TOML tables are conceptually hash tables whose keys are arbitrary strings." Both arrive at the same endpoint, but the detour through hash tables seems unnecessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants