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

Access to function "arity" #226

Open
copumpkin opened this issue Aug 1, 2016 · 2 comments
Open

Access to function "arity" #226

copumpkin opened this issue Aug 1, 2016 · 2 comments

Comments

@copumpkin
Copy link

Once we get a splat operator, 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.

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.

@sparkprime
Copy link
Member

Other than dependency injection, can you give some use cases? :)

@fare
Copy link

fare commented Sep 6, 2016

One use I have is for adding type/contract-checking wrappers around the arguments to a function. In practice, I already have the length of the list of contracts, so I can dispatch on that for all function of less than N arguments (with N=5) and error out if there are too many arguments. A bit ugly, though.

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