-
Notifications
You must be signed in to change notification settings - Fork 101
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
Comments
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? |
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. |
A nameless tuple would work too, but I'd rather name the fields. |
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. |
Structs have a well-defined set of keys. It's the difference between |
thanks. to test my understanding, is this correct?
..? 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 |
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
In that case your sentence reduces to "structs differ from maps in being structs". |
doh.
is the set of items actually "finite and immutable" ? |
@equalsJeffH the items are mutable, the names associated with them are not. |
@tabatkins brought this up the other day. A syntax that might work is
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. |
I'm happy to add this. Any thoughts on |
I'm happy with either. The latter involves less copy-and-pasting... |
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?) |
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 |
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. |
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" → |
I would be confused... |
So you find "the fooStruct «[ "a" → x, "b" → y ]»" to be confusing to read? |
Yes. |
I think it would be especially weird to markup the names as if they are strings. They're not. |
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 |
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.
The text was updated successfully, but these errors were encountered: