Toml: why sometime define super-table afterwards is ok, while sometime is not ok?

Created on 12 Apr 2020  路  7Comments  路  Source: toml-lang/toml

[x.y.z.w]
[x] # defining a super-table afterwards is ok

Dotted keys define everything to the left of each dot as a table.
Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed.
Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed.

[fruit]
apple.taste.sweet = true
# [fruit.apple]  # INVALID
# [fruit.apple.taste]  # INVALID

So this is invalid? Why is it different from the first one above?

x.y.z.w = {}
[x]

This is also invalid?

[x.y.z.w]
[x]
y.u.v = {}

...Even if it does eventually become clear, this is too complicated.

Most helpful comment

It makes more sense if you think about the [table.header] syntax as being an indication of category, but dotted.table = syntax as being more traditional value assignment.

Since [table.headers] are, well, headers, it's natural to want to separate your document using them; if redefining super-tables was illegal in this way, you'd have to have your headings in an unnecessarily specific order, making them harder to use.

dotted.table = syntax is much more explicit, hence the mutability restrictions, and also given that it still falls under whatever [table.header] has most recently appeared above it. It's "complicated" if you mix the two as though they were the same, but they aren't, so in practice it's not complicated at all.

All 7 comments

It makes more sense if you think about the [table.header] syntax as being an indication of category, but dotted.table = syntax as being more traditional value assignment.

Since [table.headers] are, well, headers, it's natural to want to separate your document using them; if redefining super-tables was illegal in this way, you'd have to have your headings in an unnecessarily specific order, making them harder to use.

dotted.table = syntax is much more explicit, hence the mutability restrictions, and also given that it still falls under whatever [table.header] has most recently appeared above it. It's "complicated" if you mix the two as though they were the same, but they aren't, so in practice it's not complicated at all.

@marzer Thank you! So could I say like this: a table defined by [table] cannot access by table.key=value, and a table defined by table.key=value cannot access by [table]; but if in same way, table defined by [table.sub] can access by [table]? @pradyunsg @eksortso @ChristianSi

Yup, that's exactly right.

@marzer Great!

@LongTengDao: I'm unsure how to read your table. Does "ahead" refer to the row headers (left) and "latter" to the column headers (top), or is it the other way around? In either case, I don't think all your values are fully correct, but without knowing how to read the table it's hard to make sure.

@ChristianSi "ahead" refer to left, "latter" refer to top. Help to check that, thank you!

@LongTengDao The following is allowed, though you seem to list it as invalid ("redefine"):

[table]
table = {}

This defines a key whose complete name happens to be table.table.

Likewise for the following cases:

[table.1]
table = {}  # full name: table.1.table
table.2 = '...'  # table.1.table.2

[table]
table.2 = '...'  # table.table.2

I guess you know that, just wanted to spell it out so that no misunderstandings may remain.

The rest of your table looks correct to me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jacobconley picture jacobconley  路  4Comments

tamasfe picture tamasfe  路  3Comments

chillum picture chillum  路  4Comments

LongTengDao picture LongTengDao  路  3Comments

jdfergason picture jdfergason  路  4Comments