You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lift the restriction on using variable as the first word (the command word) #564
Currently it's prohibited to use variables as the first word of a command. However this can be useful at times, esp. when implementing functions that take "callbacks". Consider this:
function map
set -l predicate $argv[1]
set -e argv[1]
for a in $argv
$predicate $a
end
end
Several considerations when $predicate
Expands to no word. Using the next word as command is almost always wrong. Better complain and skip this command.
Expands to a list, the head of which is one of builtin or command. Should builtin and command be interpreted?
I would leave the prohibition, because it makes the code look cleaner. But I also lack the possibility to use the command name located in the environment variable. I would propose implementing the builtin 'call' which then would evaluate it's arguments as if it was a normal command line. The difference with 'eval' is that it should respect quoted arguments.
why do you need it? In my approach user should do the check that variable is valid by himself. Moreover in most of usage cases the unexpanded $predicate will lead to a failure because the next arguments are usually not commands. In addition 'call' can issue an error when the arguments are empty.
you right (:
now I can not choose between more friendly code and more friendly error
messages. Need more opinions.
I actually find "$predicate $a" more inituative than "call $predicate $a" -
it is the natural extension of, say, "echo $a". It's the syntax most other
shells support after all.
Looking at it the other way, why was this behavior implemented in the first case? I see no convincing reason. It's unlikely that someone would accidentally try to execute a variable as a command as variables are clearly marked with an $. So I am in favor of lifting the restriction.
@maxfl: I really don't see how call $foo $bar is cleaner than $foo $bar. Also note that using a variable as a function is possible in nearly all languages. In fact, in Python, it was once not possible and you had to do apply(f, a, b). It was changed and now f(a, b) just works.
Regarding point 2: Why should builtin or command not be interpreted? Not doing so leads to needless complications/restrictions. "Executing a variable runs the command that the variable contains. Except if the contained command is 'builtin' or 'command', you can't do that." The use cases are the same as the normal uses of command and builtin.
Regarding point 1: I don't have a very strong opinion either way. Taking the first argument of the second variable is more consistent with other variable expansion rules, not allowing an empty first probably helps detect more errors. So thinking about it now, I guess I have a slight preference to not allowing it.
Commands cannot be variables because it makes parsing (and, by extension, syntax highlighting) sane. For example, consider this:
$foo echo
Is echo an argument, or a command? If $foo is builtin then echo should be a command; if $foo is cat then echo is an argument. So we can't do syntax checking (or indenting, or highlighting) without actually expanding $foo, which may change in the body of the command...so we can't do it at all. And when you hit return, should this command be accepted as is, or should it go to the next line? If $foo is if, then it needs to go to the next line, and it requires an end block. We can't know until we expand $foo. So allowing variables to be arbitrary commands would be an enormous complication.
Restricting variables to be only functions, external commands, and "certain" builtins is somewhat better. However it muddies the water about functions - now it seems like there's two ways to make user-defined commands (variables and functions), and the user has to understand the rules governing both, and choose between them. The fact that fish does not have aliases, special prompt syntax, etc. is one of its great victories of simplification.
The call proposal is least bad. It is easy to write call as a function:
function call
eval "function call_inner; $argv[1] \$argv; end"
set -e argv[1]
call_inner $argv
end
However we should resist adding support for functional programming idioms, higher order functions, maps, folds, filters, etc. which make fish more flexible but compromise its simplicity. fish is a primarily a user-facing shell, and its scripting features are there to support its user interactions, not to be a general purpose programming language.
That call implementation doesn't always work. Try it with a command containing spaces or any other shell metacharacter.
I see no problem parsing a variable for execution. Just replace it with its contents the same way as variables in argument positions are expanded. Syntax highlighting can never be 100% accurate in any dynamic language. In this case
begin
function newfunc
true
end
newfunc
end
the call to newfunc is highlighted red because the highlighter thinks it doesn't exist, but of course the invocation executes fine. But this is no reason to ban function definition from fish. Executing a variable that contains e.g. 'while' or any other block starting command should be an error, but anything else can work. Any words following the initial variable can be highlighted as arguments. I haven't seen any complaints that the first argument to sudo is highlighted as an argument and not like a command like with if.
Saying executable variables introduce a different way to do functions that would confuse users sounds a bit strange to me. If only non-syntactic commands are allowed in executable variables they are much less powerful than functions. You could make the exact same argument that eval muddies the waters w.r.t. functions because now strings can be like functions too. The rules in fact become simpler: now it is (ignoring complex expansions) "variables are replaced with their contents and the result is executed, except if the line starts with a variable". The new rule is just "variables are replaced with their contents and the result is executed".
@JanKanis , bacause of '$' at the beginning of the line (:
@ridiculousfish, there is no simple way to do call function that doesn't break quoting using fish script. I've already posted my attempt somewhere, but it breaks redirection:
functioncall --description 'Run a builtin or program' --no-scope-shadowing
set -l redir
switch "$argv[1]"case -r --redirection
set redir $argv[2]
set -e argv[1 2]
end
set -l cmd $argv[1]
set -q cmd[1]; or return 1
set -e argv[1]
## Set job control for interactive mode#set -l __call_recover_mode
if status --is-interactive-job-control
set __call_recover_mode interactive
elseif status --is-full-job-control
set __call_recover_mode full
elseset __call_recover_mode none
end
end
if status --is-interactive
status --job-control full
end
## Define main function and call#echo"function -S __run_internal_function_name; $cmd\$argv $redir; end"|.set -e cmd
set -e redir
__run_internal_function_name $argv## Clear and return #set -l stat $status
functions -e __run_internal_function_name
status --job-control $__call_recover_modereturn$stat
end
Commands cannot be variables because it makes parsing (and, by extension,
syntax highlighting) sane. For example, consider this:
$foo echo
Is echo an argument, or a command? If $foo is builtin then echo should be
a command; if $foo is cat then echo is an argument. So we can't do syntax
checking (or indenting, or highlighting) without actually expanding $foo,
which may change in the body of the command...so we can't do it at all. And
when you hit return, should this command be accepted as is, or should it go
to the next line? If $foo is if, then it needs to go to the next line, and
it requires an end block. We can't know until we expand $foo. So allowing
variables to be arbitrary commands would be an enormous complication.
I would add "and", "or" and "not" to the list. :)
Rather than seeing this as a problem, I see it as manifesting a deeper
problem - there are several classes of commands in fish without a formal
distinction:
a) Block commands - "if", "while", etc.. They are actually part of the
syntax, but by treating them as commands you can put them inside a pipeline
which is a big convenience.
b) Job modifiers - "and", "or" and "not". They modify the whole job, e.g.
"and" causes the whole job to be skipped when $status is nonzero.
c) Command name qualifiers - "builtin", "command". They restrict the lookup
of the command name that follows.
d) Everything else.
Restricting variables to be only functions, external commands, and
"certain" builtins is somewhat better. However it muddies the water about
functions - now it seems like there's two ways to make user-defined
commands (variables and functions), and the user has to understand the
rules governing both, and choose between them.
Restricting to class d) commands should be okay.
Since the forms of function calls and variable expansions are visually
different, I don't think there is any confusion here...
The call proposal is least bad. It is easy to write call as a function:
function call
eval "function call_inner; $argv[1] $argv; end"
set -e argv[1]
call_inner $argv
end
However we should resist adding support for functional programming
idioms, higher order functions, maps, folds, filters, etc. which make fish
more flexible but compromise its simplicity. fish is a primarily a
user-facing shell, and its scripting features are there to support its user
interactions, not to be a general purpose programming language.
Certainly fish should't go at great costs to support more advanced
programming techniques. But why not when it comes at only a small price? :)
I ran into this when attempting to invoke a command whose path contains a variable, e.g. $__fish_bin_path/set_color. eval is imperfect because it expands twice, e.g. eval echo "'hi'" outputs hi instead of 'hi'. So I may be coming around.
I've filed #1541 on the more general case of command/builtin/exec being invoked as builtins, and it suggests a middle ground, which is command $foo bar could work while still leaving $foo bar as illegal. That would preserve the cleanliness of the parser, at the cost of teaching the command and exec shim-bulitins how to actually perform their normal job.
Activity
maxfl commentedon Feb 7, 2013
I would leave the prohibition, because it makes the code look cleaner. But I also lack the possibility to use the command name located in the environment variable. I would propose implementing the builtin 'call' which then would evaluate it's arguments as if it was a normal command line. The difference with 'eval' is that it should respect quoted arguments.
xiaq commentedon Feb 7, 2013
@maxfl That would make it harder to detect occasions where the variable expands to no word without making expansion rules for
callspecial.maxfl commentedon Feb 7, 2013
why do you need it? In my approach user should do the check that variable is valid by himself. Moreover in most of usage cases the unexpanded $predicate will lead to a failure because the next arguments are usually not commands. In addition 'call' can issue an error when the arguments are empty.
xiaq commentedon Feb 7, 2013
@maxfl This can result in clearer - thus more friendly - error messages. :)
maxfl commentedon Feb 7, 2013
you right (:
now I can not choose between more friendly code and more friendly error messages. Need more opinions.
xiaq commentedon Feb 7, 2013
在 2013-2-7 下午9:56,"maxfl" [email protected]写道:
I actually find "$predicate $a" more inituative than "call $predicate $a" -
it is the natural extension of, say, "echo $a". It's the syntax most other
shells support after all.
JanKanis commentedon Feb 7, 2013
Looking at it the other way, why was this behavior implemented in the first case? I see no convincing reason. It's unlikely that someone would accidentally try to execute a variable as a command as variables are clearly marked with an $. So I am in favor of lifting the restriction.
@maxfl: I really don't see how
call $foo $baris cleaner than$foo $bar. Also note that using a variable as a function is possible in nearly all languages. In fact, in Python, it was once not possible and you had to doapply(f, a, b). It was changed and nowf(a, b)just works.Regarding point 2: Why should
builtinorcommandnot be interpreted? Not doing so leads to needless complications/restrictions. "Executing a variable runs the command that the variable contains. Except if the contained command is 'builtin' or 'command', you can't do that." The use cases are the same as the normal uses ofcommandandbuiltin.Regarding point 1: I don't have a very strong opinion either way. Taking the first argument of the second variable is more consistent with other variable expansion rules, not allowing an empty first probably helps detect more errors. So thinking about it now, I guess I have a slight preference to not allowing it.
ridiculousfish commentedon Feb 7, 2013
Commands cannot be variables because it makes parsing (and, by extension, syntax highlighting) sane. For example, consider this:
Is
echoan argument, or a command? If $foo isbuiltinthen echo should be a command; if $foo iscatthenechois an argument. So we can't do syntax checking (or indenting, or highlighting) without actually expanding $foo, which may change in the body of the command...so we can't do it at all. And when you hit return, should this command be accepted as is, or should it go to the next line? If $foo isif, then it needs to go to the next line, and it requires an end block. We can't know until we expand $foo. So allowing variables to be arbitrary commands would be an enormous complication.Restricting variables to be only functions, external commands, and "certain" builtins is somewhat better. However it muddies the water about functions - now it seems like there's two ways to make user-defined commands (variables and functions), and the user has to understand the rules governing both, and choose between them. The fact that fish does not have aliases, special prompt syntax, etc. is one of its great victories of simplification.
The
callproposal is least bad. It is easy to write call as a function:However we should resist adding support for functional programming idioms, higher order functions, maps, folds, filters, etc. which make fish more flexible but compromise its simplicity. fish is a primarily a user-facing shell, and its scripting features are there to support its user interactions, not to be a general purpose programming language.
JanKanis commentedon Feb 8, 2013
That call implementation doesn't always work. Try it with a command containing spaces or any other shell metacharacter.
I see no problem parsing a variable for execution. Just replace it with its contents the same way as variables in argument positions are expanded. Syntax highlighting can never be 100% accurate in any dynamic language. In this case
the call to
newfuncis highlighted red because the highlighter thinks it doesn't exist, but of course the invocation executes fine. But this is no reason to ban function definition from fish. Executing a variable that contains e.g. 'while' or any other block starting command should be an error, but anything else can work. Any words following the initial variable can be highlighted as arguments. I haven't seen any complaints that the first argument tosudois highlighted as an argument and not like a command like withif.Saying executable variables introduce a different way to do functions that would confuse users sounds a bit strange to me. If only non-syntactic commands are allowed in executable variables they are much less powerful than functions. You could make the exact same argument that
evalmuddies the waters w.r.t. functions because now strings can be like functions too. The rules in fact become simpler: now it is (ignoring complex expansions) "variables are replaced with their contents and the result is executed, except if the line starts with a variable". The new rule is just "variables are replaced with their contents and the result is executed".maxfl commentedon Feb 8, 2013
@JanKanis , bacause of '$' at the beginning of the line (:
@ridiculousfish, there is no simple way to do call function that doesn't break quoting using fish script. I've already posted my attempt somewhere, but it breaks redirection:
xiaq commentedon Feb 8, 2013
在 2013-2-8 上午5:48,"ridiculousfish" [email protected]写道:
I would add "and", "or" and "not" to the list. :)
Rather than seeing this as a problem, I see it as manifesting a deeper
problem - there are several classes of commands in fish without a formal
distinction:
a) Block commands - "if", "while", etc.. They are actually part of the
syntax, but by treating them as commands you can put them inside a pipeline
which is a big convenience.
b) Job modifiers - "and", "or" and "not". They modify the whole job, e.g.
"and" causes the whole job to be skipped when $status is nonzero.
c) Command name qualifiers - "builtin", "command". They restrict the lookup
of the command name that follows.
d) Everything else.
Restricting to class d) commands should be okay.
Since the forms of function calls and variable expansions are visually
different, I don't think there is any confusion here...
Certainly fish should't go at great costs to support more advanced
programming techniques. But why not when it comes at only a small price? :)
ridiculousfish commentedon Feb 13, 2013
I ran into this when attempting to invoke a command whose path contains a variable, e.g.
$__fish_bin_path/set_color.evalis imperfect because it expands twice, e.g.eval echo "'hi'"outputs hi instead of 'hi'. So I may be coming around.lilyball commentedon Jul 11, 2014
I've filed #1541 on the more general case of
command/builtin/execbeing invoked as builtins, and it suggests a middle ground, which iscommand $foo barcould work while still leaving$foo baras illegal. That would preserve the cleanliness of the parser, at the cost of teaching thecommandandexecshim-bulitins how to actually perform their normal job.krader1961 commentedon Apr 8, 2016
This is fundamentally a duplicate of issue #154.