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

Simplify string interpolation #3401

Merged
merged 3 commits into from May 12, 2021
Merged

Conversation

sophiajt
Copy link
Member

This is a draft proposal to simplify string interpolation.

Currently, string interpolation looks like this:

echo `This is my name {{$name}}`

It also can handle columns passed to it:

echo [[name]; [foo]] | each { echo `{{name}}` }

This proposal uses a different syntax and aligns the block with Nushell blocks, removing some of the surprises and streamlining the experience of using it.

Without #3389, this PR will look like:

echo $"this is my name {= $name}"

With #3389, this PR will look like:

echo $"this is my name {$name}"

The block is assume to be a full Nushell block, and I suspect this will mean the column shorthand behavior will be removed. Instead, you'll need to use the format command.

@sophiajt sophiajt marked this pull request as draft May 10, 2021 02:12
@fdncred
Copy link
Collaborator

fdncred commented May 10, 2021

is this supposed to work?

echo [[name]; [foo]] | each { echo {= $name} }

i get this

───┬───────
 0 │ block
───┴───────

or perhaps this

echo [[names]; [foo]] | each { echo $"this is {= $names}" }

@sophiajt
Copy link
Member Author

In the first one you're echoing a block.

In the second one you're echoing a variable that's not in scope. We could explore expanding $it's fields into their own variables but I think that might have some surprise behavior if people aren't expecting it.

@fdncred
Copy link
Collaborator

fdncred commented May 10, 2021

I was trying to mimic what you listed in the PR description:

It also can handle columns passed to it:

echo [[name]; [foo]] | each { echo `{{name}}` }

Does something like that still work? If so, what is the syntax?

@sophiajt
Copy link
Member Author

@fdncred that's removed now. The bottom text talks about this: "The block is assume to be a full Nushell block, and I suspect this will mean the column shorthand behavior will be removed. Instead, you'll need to use the format command."

So you'd need to use format instead of string interpolation

@jmoore34
Copy link
Contributor

jmoore34 commented May 11, 2021

What about

echo "this is my $name"

and for chained variable accesses

echo "this is my os ${nu.env.OS}"

and for command calls

echo "hello $(echo world)"

I feel like interpolation would be a more common use case than literal dollar signs, and for that, perhaps backticks could be repurposed to signify string literals. This would also allow users to use both single and double quotes in a string, e.g.

echo `<button onclick="alert('hello')" />` | save index.html

Command substitution could be used in cases where the user wants both string literals and interpolation, e.g. echo "Your balance is $(echo `$`)$balance" or echo "Your balance is $(echo `$`)$balance" And of course, things like a dollar sign followed by whitespace or numbers could automatically be treated as literals.

I feel like this would be a bit more natural for users coming from other shells, but I'm happy to hear your thoughts.

@fdncred
Copy link
Collaborator

fdncred commented May 11, 2021

I'd really prefer to be consistent with how we handle this. I would push back on, "when it looks like x you need to do y", that's just too confusing. I'd rather have, "always do x with string interpolation, no matter what", although I don't know if these changes work in all circumstances. I'd really like to land this the way it is and adjust if needed.

@jmoore34
Copy link
Contributor

That's a good point. I was thinking of revising my comment to:

echo "this is my os $($nu.env.OS)"

for consistency, but looking at it, I think JT's syntax looks cleaner. 😁

@sophiajt sophiajt changed the title WIP: simplify string interpolation Simplify string interpolation May 12, 2021
@sophiajt sophiajt marked this pull request as ready for review May 12, 2021 01:21
@sophiajt
Copy link
Member Author

@jmoore34 - ah good. and thanks for investigating 🙂

Looking forward to using this!

@sophiajt sophiajt merged commit 311c0e3 into nushell:main May 12, 2021
@sophiajt sophiajt deleted the simplify_string_interp branch May 12, 2021 01:54
@fdncred fdncred added this to Release 0.32 in Release PRs May 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Release PRs
  
Release 0.32
Development

Successfully merging this pull request may close these issues.

None yet

3 participants