-
Notifications
You must be signed in to change notification settings - Fork 440
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
Richer functions and more principled top-level parameterization #147
Comments
@roconnor @davidzchen @mikedanese feedback is appreciated :) |
This seems like a lot of command line flags. I think it would be better if we could cut down on the number of flags rather than increasing them. Can we use the same set of |
Yeah I originally was going to get rid of the -env ones on the grounds that you can just use --ext-str VAR=${VAR}, but then I remembered that environment variables are more secure than commandline arguments because they're hidden from other users on the system. So each of those unfortunately has a unique purpose that is not subsumed by any of the others. Explicit vars are convenient, env vars are safe, and file is for large data. How about we do --ext-str FOO to mean --ext-str-env FOO, i.e. if there is no = (due to K=V) then treat it as that mode of entry. What you suggest is interesting. I'd like to be able to keep the extVar, both for backwards compatibility and because in some cases I think it would be useful for things that should be omnipotent, like the version of the software you're configuring, or whatever. So it'd be necessary to have both top level arguments and extVar in flight at once. Maybe we could do this though, with only 4 functions:
We could also default the T: mode of this. And the environment variable versions (power user feature):
|
Another simplification is to drop the str forms and do everything with code. But that requires double escaping in bash, which is annoying. |
/cc @ghodss and @huggsboson a bunch of stuff here you might find interesting |
also /cc @jbeda @chrisleck |
we have a few places where named args and default args would improve things. Right now we're using single arg functions that take objects and a "getOrElse" function for optional args with defaults. Would love to replace them. |
Good to hear! With this proposal you can't have the default value of one param depend on the others, which you would be able to do with your current approach. This is because I based it on Python:
Would that be painful? It is something I'm on the fence about right now. |
All of our current uses merely specify a default literal, so it wouldn't be painful given our existing cases. |
OK, will stick with that semantics then. C++ is the same:
|
speaking of int. Any interest in statical or dynamically checked type assertions on parameters? |
I can't speak for current uses of jsonnet, but in Nix at least we have several cases where it was beneficial to have parameter defaults refer to other parameter values. I can try to dig up some examples if helpful. Overall I like the proposal though. |
Diverting type assertions discussion to #153 |
@copumpkin Yes please! |
The one thing that changed from this design in the implementation is that the default arguments can refer to other params (forwards and backwards). This turned out to be easy to both define and implement, as well as useful in some cases. The semantics is equivalent to mutually recursive let:
Compare to:
both of which produce:
|
Context
Jsonnet's original design used object inheritance (mixins) as the primary abstraction mechanism. Since then, users have indicated that in many cases they prefer abstracting via functions. Therefore, this proposal adds several features to make Jsonnet functions more productive. These new features are intended to be conceptually and syntactically compatible with Python. Additionally, an alternative to std.extVar() is proposed which allows top-level parameterization of configurations without creating the equivalent of a global variable.
#1) Add default arguments
Function parameters can have default values. To the function body, such parameters are no different to regular parameters.
The default expression is bound in the outer scope. E.g.:
#2) Named arguments
Function call syntax is extended to allow calling a function with named arguments. All positional arguments must be given before all named arguments.
If no parameter exists by that name, an error is raised:
#3) Top-level functions
Previously a Jsonnet file evaluating to a function would be rejected:
In this proposal such a function would be called with no arguments.
This only works if the function is callable with no arguments:
And, only one level of function expansion is performed:
To provide an alternative to std.extVar() that is not globally scoped (i.e., requires top-level parameters to be threaded through a large config), new CLI flags are added to provide arguments to top-level functions.
Top-level functions can therefore have parameters without default arguments, and if no argument is provided on the CLI then an error is raised:
#4) CLI flag cleanup
The CLI flags have grown organically and are now somewhat messy. Here is a the current state:
The meaning of the flags is not clearly indicated by their names. Thus, the long forms of these flags is renamed along with the addition of a new dimension to the space (top-level func vs extvar). The result is longer but more organized:
The text was updated successfully, but these errors were encountered: