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

Allow objects without braces around them inside arrays #7

Open
pditommaso opened this issue Mar 14, 2012 · 3 comments
Open

Allow objects without braces around them inside arrays #7

pditommaso opened this issue Mar 14, 2012 · 3 comments

Comments

@pditommaso
Copy link

I would propose to extend the HOCON syntax to simplify declaration of an array of objects.

Currently it is possible to declare an array of primitive types or objects like the following:

[ 3, 6, 9 ]

[ alpha, beta, delta ]

[
   { a : 42, c : 5 }
   { b : 43, c : 6 }
]

But take in consideration the declaration of a list of assignments, for example: alpha = 1, beta = 2, delta = 3. Currently it is required the following definition:

 [  { alpha = 1 } , { beta = 2 }, { delta = 3 } ]

It is much nicer if it would be possible to use the following syntax

[  alpha =  1, beta = 2, delta = 3   ] 

The benefit is more evident for more complex declaration like this

[  alpha =  { a:1 }, beta = { b: 2 }, delta = { c: 3, d: 4 )   ] 

instead of

[  { alpha =  { a:1 } }, { beta = { b: 2 } }, { delta = { c: 3, d: 4 } }  ] 

The underlying would not change but the syntax would be much more readable in the spirit of the project.

Thanks

@havocp
Copy link
Collaborator

havocp commented Mar 14, 2012

It is an interesting idea. Would have to play with the parser and see if it's workable. It creates some ambiguity; note that outside of [], "a=1, b=2" is a single object, but here "a=1,b=1" would be two objects in a list. Also it requires the parser to "look ahead" because in all existing cases, if you see "alpha" in "alpha=1" you know that "alpha" is an object key already. But in this case, you don't know whether "alpha" is an object key or just a string, until you get to the "=" or ":" - so that's a look-ahead to the next token that is never (if I remember right) currently done. Anyway those are just some possible issues I thought of offhand, it would need more digging in.

@pditommaso
Copy link
Author

I don't think that it will create an ambiguity, on the contrary in my opinion it would be even more coherent with the JSON array semantic. In fact an array can hold a list of primitive types or objects, so the assignment notation can only represent an object since it is not a primitive types.

My real use case it the following: I have a list of components, for each of them I have to provide a configuration. I know that I could use the object notation instead of the array, for example:

components { alpha: { a:1, b:2 }, beta: { x:3, y:4} } 

But the main problem with this approach is that I cannot have different configuration for the same "component" because the last will override the previous ones. So here, the array is the best choice:

components [ {alpha: { a:1, b:2 }}, {beta: { x:3, y:4}}, {beta: { x:5, y:6}} ]  

But the syntax it is not so clean as it would be expected. Much better would be:

components [ alpha = { a:1, b:2 }, beta = { x:3, y:4 }, beta = { x:5, y:6 } ]   

@ericacm
Copy link

ericacm commented Jul 16, 2013

+1

@havocp havocp changed the title Simplified notation to declare an array of objects Allow objects without braces around them inside arrays Mar 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants