-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Language feature proposal: exclusive 'with' #1361
Comments
Although I understand the rationale behind the behavior of |
(However, this probably should go through an rfc). |
Maybe I don't understand the rationale but to me: { x ? "z" }:
let
a = { x = "y"; };
in
(with a; x) should evaluate to "y" not " |
It should, but changing something that would silently change the meaning of tons of existing code seems difficult. We could force-enable my #1364 warning for a couple of releases, saying that the binding order will change and that you should update your code to not be ambiguous. |
@matthewbauer, wow, i would say that this is a true bug, but the documentation is silent about what the correct behaviour really is. Here is a slightly simpler example: let
x = 1;
a = { x = 42; };
in
(with a; x) evaluates to P.S. In fact, this behaviour makes it easier to understand what the actual binding in the scope of |
I think the low precedence of with pkgs hiding (darwin); That would remove any legitimate use cases of this feature IMO. |
I think this is mostly a security feature, in order to avoid accidental (and invisible) name capture, so |
@regnat The current behaviour is more predictable. You can tell from the lexical scope what variable is going to be used. E.g. in
you don't have to worry that somebody adds an attribute named Adding a second The docs could be improved though. |
However: (with { a = 1; };
(with { a = 2; };
a))
# => 2 (let a = 1; in
(with { a = 2; };
a))
# => 1 After learning all this, i think i will not be using As to the proposed |
@alexeymuranov In general it seems like a bad idea to do You've even provided the example. |
Personally, I like to use EDIT: just to be clear, this is 100% equivalent to the existing |
I definitely like it. But I've stumbled upon this https://nixos.org/releases/nix/nix-0.5/manual/manual.html#id2526745, which says
which means that was a case in Nix 0.5? |
The old let syntax requires a special attribute
|
hmm, that is very close to the syntax I want... the fact that variables are lexically bound in |
It's possible even to preserve pkgs variables in that scenario
```
... = builtins.attrValues { inherit (pkgs) foo bar lolcat; };
```
In fact, it would be great if `*buildInputs` supported attribute set, and
not only a list as input,
so it would be clean usage for `inherit`-style sets. For example, that
would allow /remove/ a build input
in `overrideAttrs`:
```
foo.overrideAttrs (super: {
buildInputsSet = lib.filterAttrs (n: v: n != "bar") super.buildInputsSet;
})
```
But speaking of `with`, it is often convenient to do `with lib;`. Should
this
be deprecated too? In this case it deserves a RFC.
2018-01-05 2:39 GMT+02:00 volth <notifications@github.com>:
… I would totally deprecate with as a language feature.
For syntax-sugar purposes, it could be implemented as a lib-function used
like
lib.With pkgs [ "foo" "bar" ]
or
lib.With pkgs "foo bar"
Currently it is very easy to hit a problem writing the code like
let
lolcat = 0;# ....hundred of lines....in# ....hundred of lines....with pkgs; [ foo bar lolcat ]
It is nowadays normal in nixpkgs to have huge lists after with pkgs; or with
pythonPackages; expecting list of packages, not checking that there could
be a same-name variable defined few scopes upwards which has priority over
with-bindings.
And pkgs has tons of generic names.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1361 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAtWkfbN4CPHlaP2zcn08JeY1uESAcZjks5tHW9agaJpZM4NJ9tq>
.
|
I think the confusion is mitigated if you think of
A further measure to reduce confusion might be to syntactically distinguish |
I just stumbled into this while googling for something else, and thought I'd weigh in since it's concerning to see so many calls to deprecate
Note that I do consider the "low precedence" behaviour of |
A problem with
with
expression in Nix is that to know which values the identifiers in the scope ofwith dict;
refer to, one needs to examine the entire dictionary/setdict
[not quite true, see Edit 1], which could be defined in a different file or composed in a dynamic way. This is especially true if thiswith
expression is itself in the scope of anotherwith
expression (i've got "bitten", or at least surprised, by this). I imagine this could be the reason why the analogouswith
statement was removed from ECMAScript strict mode.How about introducing into Nix an exclusive version of
with
, let's call itonly_with
:What do you think?
Edit 1
From a comment below i discovered that
evaluates to
0
. Is this documented anywhere?While being a bit unexpected, this behaviour allows to determine which values variables in the scope of
with foo;
are bound to without inspectingfoo
... unless there are nestedwith
or optional arguments.The text was updated successfully, but these errors were encountered: