Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notation for structs #130

Open
jyasskin opened this issue Apr 27, 2017 · 22 comments
Open

Notation for structs #130

jyasskin opened this issue Apr 27, 2017 · 22 comments

Comments

@jyasskin
Copy link
Member

When writing a non-tuple struct value in another spec, it'd be nice to have a notation for it. There are several options, but I haven't gone looking through other specs to figure out which ones are already used.

@domenic
Copy link
Member

domenic commented Apr 27, 2017

The original idea was (x: foo, y: bar), but we cut it from the PR that added structs because nobody seemed to be using it any literal notation at all yet. What are your thoughts?

@jyasskin
Copy link
Member Author

I like (x: foo, y: bar). I'm suggesting an algorithm-local struct in w3c/webauthn#427, where it takes too many words to lay out the names in prose.

@jyasskin
Copy link
Member Author

A nameless tuple would work too, but I'd rather name the fields.

@jyasskin
Copy link
Member Author

I turned out not to suggest the struct in my review after all, so if you want to wait until there's a real use, that's fine.

@equalsJeffH
Copy link
Contributor

I'm curious, how do structs, as defined, functionally differ from maps and Records?

@domenic
Copy link
Member

domenic commented Aug 22, 2017

Structs have a well-defined set of keys. It's the difference between hashmap<k, v> or hashmap<string, value> and struct foo { type1 field1; type2 field2; type3 field3; } in traditional programming languages.

@equalsJeffH
Copy link
Contributor

thanks. to test my understanding, is this correct?

Structs differ from maps and Records in that a struct has a well-defined and immutable set of keys.

..? if so, perhaps it ought to be added to infra's Structs section ?

It seems there is no present way to formally declare something of type struct using WebIDL ?

@domenic
Copy link
Member

domenic commented Aug 22, 2017

Well, we won't mention records, as Infra sits below Web IDL.

For the same reason, struct is not a Web IDL type; it's a type that's useful in writing specifications, often backing some other structure (like an interface).

I'm not sure saying "Structs differ from maps in having a well-defined and immutable set of keys" is useful given that the very definition of structs is

A struct is a specification type consisting of a finite set of items, each of which has a unique and immutable name.

In that case your sentence reduces to "structs differ from maps in being structs".

@equalsJeffH
Copy link
Contributor

doh.

A struct is a specification type consisting of a finite set of items,...

is the set of items actually "finite and immutable" ?

@annevk
Copy link
Member

annevk commented Aug 23, 2017

@equalsJeffH the items are mutable, the names associated with them are not.

@annevk
Copy link
Member

annevk commented May 2, 2022

@tabatkins brought this up the other day. A syntax that might work is

(name → value, name2 → value2)

which makes them look similar to tuples, except that the names of the items are visible and can appear in any order.

As discussed in whatwg/whatwg.org#396 not all structs necessarily want a literal syntax and we should make note of that if we decide to add syntax.

@domenic
Copy link
Member

domenic commented May 2, 2022

I'm happy to add this. Any thoughts on (name → value, name2 → value2) vs. (name: value, name2: value2)? The former is more like an ordered map, which could be good or could be bad.

@annevk
Copy link
Member

annevk commented May 2, 2022

I'm happy with either. The latter involves less copy-and-pasting...

@tabatkins
Copy link
Contributor

I think it's good to be consistent between the syntaxes if possible; structs and maps are very similar and having their key/value separator be different seems confusing.

(But I do like the colon over the arrow, same as @annevk; maybe we could switch (or at least allow) maps to use colons as well?)

@tabatkins
Copy link
Contributor

That said, is there a big reason not to just use the map literal syntax here? We already let map literal syntax be used for IDL record when it's clear from context what's desired; this is Yet Another KV syntax and can probably lean on the same thing? I don't think there's any real chance of confusion.

@annevk
Copy link
Member

annevk commented May 3, 2022

Well, for maps the names are essentially values. For structs the names are immutable identifiers. That is a bit different at least and would visually show they are different, albeit very subtly.

Also, maps and IDL records are conceptually very much the same thing. Just like lists and IDL sequences. Maps and structs are not.

@tabatkins
Copy link
Contributor

Well, for maps the names are essentially values. For structs the names are immutable identifiers. That is a bit different at least and would visually show they are different, albeit very subtly.

Right, they're different, but do we need to specially guard that distinction via syntax? Do we believe it would cause confusion to not have them syntactically distinguished? If someone is going to write spec text that builds a struct with arbitrary keys, do we think that seeing () vs «[]» is the thing that's going to make them stop and realize they're doing something wrong? (I'm not suggesting letting maps and structs be interchangeable as values, just that you can use the same literal syntax to say things like "the struct «[ "a" → x, "b" → y ]»" as distinct from "the map «[ "a" → x, "b" → y ]»".)

@domenic
Copy link
Member

domenic commented May 3, 2022

Do we believe it would cause confusion to not have them syntactically distinguished?

I would be confused...

@tabatkins
Copy link
Contributor

So you find "the fooStruct «[ "a" → x, "b" → y ]»" to be confusing to read?

@domenic
Copy link
Member

domenic commented May 3, 2022

Yes.

@annevk
Copy link
Member

annevk commented May 3, 2022

I think it would be especially weird to markup the names as if they are strings. They're not.

@tabatkins
Copy link
Contributor

All right, I disagree but I'm outnumbered. ^_^ In that case, still keeping consistent kv separation characters with map literal is good imo, so my vote goes to (foo → val, bar → val)

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

No branches or pull requests

5 participants