-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
parse command interpolations at parse time #3150
Comments
the following already works without ambiguity, and doesn't require changing string parsing based on context:
Is there an example where this isn't quite so straightforward? |
What about something like macro example()
f="a"
esc(quote
g="b"
run(`echo $f $g`)
end)
end You could do instead macro example()
f="a"
esc(quote
g="b"
h=$(f)
run(`echo $h $g`)
end)
end but it's a little weird to be forced to create a temporary variable, since I'm not sure there is any other situation in Julia in which replacing a reference to a variable with its known value will change the semantics of the program. While here, changing (edit: fix the variable in the second example) |
That's tricky, but you don't exactly see it until you try to expand the macro. In the first example, f only exists at macro expansion time and g only exists at run time. Assigning the value of f to h seems to be necessary to convert the value into a variable. For strings, it is easy to use
(edit: I forgot |
The variable-to-value substitution you mention would be:
to
You can't drop the extra |
this would be a new feature and thus not appropriate for the v0.4.x bugfix series. i'm going to tentatively move this to v0.5, please comment if you think that is not appropriate. |
Makes interpolation in backquotes work the same as it does for strings. For example, after this change: julia> macro example() f = "a" esc(quote g = "b" `echo $($f) $g` end) end julia> @example `echo a b` Before, this produced the error "unsupported or misplaced expression $".
Since we're not doing #12139 we can do this. |
From triage: can live without this for 1.0. |
Removing from milestone, noting that this may change post 1.0, which won't affect normal code but could affect macros that deal with commands. |
Would changing the parsing of |
This is an interesting approach particularly because it separates the detailed shell parsing rules from the rules for interpolating values. I suppose this means the parser changes will be much more minimal and completely consistent with the rules for string interpolation which is welcome. I think this would be an excellent thing, provided it's sound! But I can think of at least one case where interpolation into normal strings vs interpolation into cmd strings behaves quite differently: julia> `hi '$a'`
`hi '$a'`
julia> "hi '$a'"
ERROR: UndefVarError: a not defined
Stacktrace:
[1] top-level scope at REPL[5]:1 Looking at the source of |
That is quite intentional as it mirrors what the shell does. You often use single quotes in a shell command when you need to write some code that uses A more general observation here is that the current arrangement is strictly more flexible: the macros get to decide exactly how and if they do interpolation. Yes, that makes implementing interpolation harder, but that's the trade off. Given that implementing macros like this is generally considered an advanced activity, that feels like the right trade off to me. The main downside is that user string expressions can't allow nested quotation like normal strings can; I haven't found that limitation to be especially bothersome—if you're putting quotes in the things you're interpolating into quotes, I think you're better off doing it in separate expressions anyway. |
I just ran into the quoted Cmd vs String substitution inconsistency. My use case was looping over some variables to repeatedly call a giant ugly curl command with nested quoting generated from Chrome dev tools. It was a pain to break it up into an array, so would have been nice to have variable substitution working as I had expected (how it works for regular Strings). |
Why was this closed as completed? If we're not going to fix this we should close it as "won't fix"? Clearly fixing this would be breaking. But it's legitimately a rather annoying and surprising inconsistency. |
It would be nice to have a way to "double interpolate" a variable with respects to nested possible interpolation contexts to make it easier to write quoted code blocks that wants to interpolate a variable inside a string. e.g., so the below code echos "hi". Not sure what the best way to go about that is.
The text was updated successfully, but these errors were encountered: