-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
More explicit syntax for temporary assignments #1114
Comments
FWIW, the POSIX semantics for temporary assignments work by instantiating environment variables. Except when it doesn't. That is,
Which is a long winded way of saying that any change to the syntax or behavior of temporary assignments requires careful thought so as not to repeat the mistakes of the POSIX standard. |
This comment is indirectly related to this issue since the problem I'm describing involves permanent, rather than temporary, assignments. Consider this pipeline:
In a POSIX shell (e.g., bash) that will output |
Getting back to this. I'm considering stealing a design from Raku. Introduce a special command (say var x = foo
{
tmp x = bar
# $x is "bar" for the remainder of the scope
command using $x
}
# $x reverts to "foo" The original original var x = foo
var y = lorem
{
tmp x = (some command)
tmp y = (some other command)
command using $x and $y
} One-liner length comparison:
Still more verbose than the current syntax, but same as the original |
Also, if there is already a scope, using For example, to and run something using each subdirectory as the working directory: for d [*/] {
tmp pwd = $d
# do things in $d
} While the current syntax will require an additional level of nesting, unless there's only one command in the body: for d [*/] {
pwd=$d {
# do things in $d
}
} Another nice thing about |
I like the idea, if only I could suggest |
This new construct has no corresponding construct in JavaScript or Perl. It does correspond to Bash's Both JavaScript's |
I don't think referencing Bash's |
I've done a little more research, and did originally misunderstand. Would tmp be dynamically scoped, like
Would this print 1, 2, 1? |
That's how I read it. But I wasn't sure, until I saw the |
@fennewald You're right. Indeed Perl's |
A +1 for |
0.18.0 will deprecate the legacy syntax. After 0.18.0 is released, the legacy syntax will be removed. Moving this to the 0.19.0 milestone. |
I think variables should be limited to their scope, unless specified otherwise, like Python's global or nonlocal. If we support a simple scope with
we write
Also maybe a new command to set multiple variables like
Doesn't make much different in verbosity though. |
@ilius Variables in Elvish are already lexically scoped, so in Anyway this has been implemented; I'll leave this open until the legacy syntax is fully removed. |
I've decided to implement the originally proposed |
The current syntax for temporary assignments is borrowed from POSIX shell, and requires there to be no spaces surrounding a
=
, e.g.foo=bar put $foo pwd=/tmp touch x
The syntax is quite subtle, and is a source of syntactical complexity - for example, the syntax for lvalues have to support braced lists, in order for code like this to work (suppose that
f
outputs two values):It may be a good idea to make the syntax for temporary assignments more explicit, for example using a special command
with
. The examples above can be written as:However, it's not straightforward to generalize this to multiple temporary assignments, which can be done easily using the current syntax, such as
a=foo b=bar put $a $b
. Nestingwith
certainly works, but is quite verbose. One possibility is to use lists to surround the individual assignments:In any case, the
with
command is going to be more verbose than the current syntax, hence the "maybe" tag.The text was updated successfully, but these errors were encountered: