Access to function "arity"
- Dominant language
- Jsonnet
- Stars
- 7.6k
- Forks
- 475
- PR merge metrics
- No merged PRs in 30d
Description
Once we get a [splat operator](https://github.com/google/jsonnet/issues/203), this functionality will be more useful, but a fun feature in Nix (cc @oconnorr) is the ability to ask a function about what arguments it takes (assuming it uses the attribute pattern matching construct). Given that jsonnet now has named arguments, it seems like a similar primop could work. I'd imagine it would tell you the number of positional arguments and the names of the attributes for named arguments. You could then use that to implement a form of pseudo-dependency-injection like [the one we use in nixpkgs](https://github.com/NixOS/nixpkgs/blob/master/lib/customisation.nix#L100-L112).
So basically, what I imagine is something like:
```
local foo(x, y=5, z=y) = ...;
std.functionSpec(foo) => { positional: 1, named: ['y', 'z'] }
```
Ideal would be some way to access defaults as well. I could imagine that since you're lazy, you could have `named` return an actual object whose values reference the other arguments, but I don't know how that dict interacts with positional arguments today so it might be a little tricky.
P.S: the Haskeller in me is screaming at me for wanting such an obviously evil function but it does seem pretty handy.
Contributor guide
Assessment
This issue has not been assessed yet.