wing icon indicating copy to clipboard operation
wing copied to clipboard

Raw strings

Open Chriscbr opened this issue 1 year ago • 1 comments

Use Case

As a user, I would like a way to create strings that allow me to type special characters like backslashes without having them treated as escape sequences. Likewise, I would like to be able to type quotes without having to escape them every time.

Here are some examples where I need to modify my string in order to use it in my Wing source code:

"C:\Development\profile\aboutme.html"
// today, this turns into "C:Developmentprofileaboutme.html"

"{
  "name": "John Doe",
  "age": 43,
  "phones": [
    "+44 1234567",
    "+44 2345678"
  ]
}"
// today this is a syntax error

Proposed Solution

Perhaps we can reuse the non-interpolated string syntax from https://github.com/winglang/wing/pull/6290, and change it so that it requires a trailing #, so we would end up with:

#"C:\Development\profile\aboutme.html"# // prints "C:\Development\profile\aboutme.html"

#"{
  "name": "John Doe",
  "age": 43,
  "phones": [
    "+44 1234567",
    "+44 2345678"
  ]
}"#

This is more or less similar to the design of Swift's raw strings (examples, RFC).

In cases where you want use the closing delimiter "# in your string, then the user can add more #'s:

log(##"hello"#world"##) // hello"#world

Implementation Notes

JavaScript has a way to obtain raw strings that could be useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw. Or maybe it would be easier to do the escaping within Wing's parser - not sure.

Component

Language Design

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Slack.

Chriscbr avatar Apr 23 '24 19:04 Chriscbr

I like this solution, Chris.

I found it quite interesting that by changing the quantity of # at the beginning and end of the string, it allows the use of this character within the string.

marciocadev avatar Apr 24 '24 11:04 marciocadev

@Chriscbr I think this is implemented right?

eladb avatar May 18 '24 12:05 eladb

@eladb Right now we have "non-interpolated strings" which allow you to create strings like "foo{bar}" without interpolating variables for you. But characters like backslashes and quotes are still require escaping if you want to use them.

Chriscbr avatar May 18 '24 23:05 Chriscbr

I see. Any reason not to merge these use cases and make non-interpolated strings into raw strings?

Is it really valuable to 3 types of string literals in the language?

eladb avatar May 19 '24 00:05 eladb

Yup, exactly - I think they can combined 👍

Chriscbr avatar May 19 '24 00:05 Chriscbr

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] avatar Aug 17 '24 06:08 github-actions[bot]