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

Improvements to syntax for defining multiple locals #551

Open
ekimekim opened this issue Sep 4, 2018 · 2 comments
Open

Improvements to syntax for defining multiple locals #551

ekimekim opened this issue Sep 4, 2018 · 2 comments

Comments

@ekimekim
Copy link

ekimekim commented Sep 4, 2018

Currently, if I wish to define a large list of locals (eg. "importing libraries" at the top of a file), I have two reasonable ways to format it:

local foo = import "foo.libsonnet";
local bar = import "bar.libsonnet";
local baz = import "baz.libsonnet";
local
  foo = import "foo.libsonnet",
  bar = import "bar.libsonnet",
  baz = import "baz.libsonnet";

The first is annoying due to the repetition, it would be nice to be able to define many locals without saying "local" every time and the syntax local x = 1, y = 2; solves this problem nicely for single-line statements.

The second is annoying when spaced over many lines. You either need to remember to end the last line with a ; instead of a , as above, or you can do a quasi-"closing brace"-style thing like so:

local
  foo = import "foo.libsonnet",
  bar = import "bar.libsonnet",
  baz = import "baz.libsonnet"
;

However, now you need to remember not to include a trailing comma on the final line.

I see two fixes to this problem that I hope should be relatively easy:

  1. Allow a redundant , before the ;, ie. so that an expression like local x = 1, y = 2,; is valid. This allows a "trailing comma" in the quasi-closing-brace-style shown above, which will go a long way to making this not a pain when changing or re-ordering the list. This is the smallest change.

  2. Allow a multiple-definition syntax like local (x = 1, y = 2);, with optional trailing commas. This would make my example:

local (
  foo = import "foo.libsonnet",
  bar = import "bar.libsonnet",
  baz = import "baz.libsonnet",
);

This is the best option in my opinion as the parentheses indicate immediately to the user that the definitions form a "block" of things which the local keyword applies to, and it has a very visible end while allowing re-ordering or modification of the list of definitions without needing to worry about treating any one specially.

It is also reminiscent of the python syntax:

from foo import (
  bar,
  baz,
)
@andreabedini
Copy link
Contributor

local foo = import "foo.libsonnet"
   ,  bar = import "bar.libsonnet"
   ,  baz = import "baz.libsonnet"
   ;

?

there's still an asymmetry between the first import and the following ones but it's right at the front of the line and easy to spot.

@sparkprime
Copy link
Contributor

#307

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

4 participants