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

Error with at-pattern and optional params #334

Closed
lucabrunox opened this issue Aug 30, 2014 · 10 comments
Closed

Error with at-pattern and optional params #334

lucabrunox opened this issue Aug 30, 2014 · 10 comments

Comments

@lucabrunox
Copy link
Contributor

$ echo 'let f = o@{x ? "x"}: o.x; in f {}'|nix-instantiate -
error: attribute `x' missing, at (string):1:20

It only happens with optional parameters.

@lucabrunox lucabrunox changed the title Evaluation error with at-pattern Error with at-pattern and optional params Aug 30, 2014
@shlevy
Copy link
Member

shlevy commented Aug 30, 2014

@-patterns bind the actual argument passed, not the optional arguments. This also applies to ...: If you pass an argument to a function that has a ... param, any extra attributes will still be bound by the @-pattern

@shlevy
Copy link
Member

shlevy commented Aug 30, 2014

Think of foo@{ params... }: body as sugar for foo: ({ params...}: body) foo

@lucabrunox
Copy link
Contributor Author

Didn't understand much the semantics, but it's very inconvenient. Now I have to write:

{ opt1 ? "def1", opt2 ? "def2" }: 
let opts = { inherit opt1 opt2; };
...

@edolstra
Copy link
Member

edolstra commented Sep 2, 2014

You can just write x instead of o.x, right?

Anyway, closing since it's intended behaviour (and the alternative is less efficient to implement, because it would require constructing a new attrset).

@edolstra edolstra closed this as completed Sep 2, 2014
@lucabrunox
Copy link
Contributor Author

@edolstra no because I want to use o as a set for union ecc., as explained in the above comment, otherwise I wouldn't use the @-pattern no? That's the purpose of @-pattern, to reuse the argument set as set.
Also I don't think choosing performance over semantics is a sane approach, sorry.

@edolstra
Copy link
Member

edolstra commented Sep 2, 2014

Given that plenty of people complain about the performance and memory consumption of the evaluator, it's a pretty relevant argument.

@lucabrunox
Copy link
Contributor Author

If you want this semantics as you said being intended, I really dislike this choice because it makes @-pattern useless with optional arguments, and deserves a big warning in the docs. Currenly it's written as follows:

  args@{ x, y, z, ... }: z + y + x + args.a
Here args is bound to the **entire argument**

Which for me means that I can use o.x even if it's optional, unless I'm missing some other relevant line about argument sets.

I don't know the implementation details of nix and I don't know why another set is needed to be created, but if another set can even remotely (theoretically) be avoided, please keep this issue open.

@shlevy
Copy link
Member

shlevy commented Sep 2, 2014

@lethalman Are you prepared to go through all existing uses of this and ensure they don't depend on the current semantics?

@lucabrunox
Copy link
Contributor Author

@shlevy yes. I'd like to highlight this line: https://github.com/NixOS/nixpkgs/blob/master/pkgs/desktops/kde-4.12/kde-package/default.nix#L57

Apparently, who wrote that line had my same wrong thought, that meta, name and cmakeFlags are part of a.

@edolstra
Copy link
Member

edolstra commented Sep 3, 2014

Well, the performance aspect is moot anyway, since the current behaviour is intended. The @ in Nix is just like the @ in Haskell: it's an alias for a pattern-matched argument. It's not intended to construct a new value.

zolodev pushed a commit to zolodev/nix that referenced this issue Jan 1, 2024
* tutorial: nixos configurations on vm


Co-authored-by: Benoit de Chezelles <bew@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
Co-authored-by: Henrik <i97henka@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants