-
Notifications
You must be signed in to change notification settings - Fork 448
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
Destructuring objects within other objects #847
Comments
The "for" part in object comprehension doesn't have access to object locals (they are scope "inside" the for and have access to the variable you're iterating over). This is why you get the error. The problem with a flattening operator like you describe, is that objects in Jsonnet are more than dicts in Python. In Python dicts are just mapping from key to value and here we have objects with OOP sense with self and super. I think you can achieve your goal with just plain inheritance (
Please let me know if that works for you. |
Hi thanks for the info, maybe I should have added it to the initial description but this example reproduces the syntax error in the trivial case: function(a, b) {
local thisA = {a: a},
local thisB = {b: b},
thisA + thisB
}
|
Yes. Please take a look at where the curly braces are placed. This works: local fun(a, b) =
local thisA = {a: a};
local thisB = {b: b};
thisA + thisB;
fun(1, 2) The curly braces are not a part of the function syntax – they are a part of the object literal syntax. You have no enclosing object here, so no curly braces. |
Thanks for the quick reply. It seems it is not possible to apply this syntax to a top level function. Omitting the function(a, b) =
local thisA = { a: a };
local thisB = { b: b };
thisA + thisB;
edit: solved it, thanks for your help function(a, b)
local thisA = { a: a };
local thisB = { b: b };
thisA + thisB |
There seems to be a 'feature hole' for destructuring objects within other objects. In python:
This is useful if using top-level functions for your definitions. Say I'd like to create a TLA that dynamically composes other objects:
The alternative (some dictionary expression) doesn't work.
Originally posted by @arlyon in #307 (comment)
The text was updated successfully, but these errors were encountered: