[Propose] Add single quote string(just like most modern language instead of C), use ?A for Char `A` (just like Ruby)

After use Crystal since two years ago, Still not used to not having single quote strings.

Especially when you copy string from others modern language, like Ruby, BASH, which is really troublesome.

Sure we can use percent string literals, but I thought It’s more about personal style, i always prefer to use “” or ‘’ unless there is a string includes both single quote and double quote.

I think most Crystal users in the future will come from other modern languages, not C, which uses single quotes as Char.

As said by matz(the author of Ruby programming), the principle of least surprise is always desirable

No one(new user) wants to be taught to use double quotation for just puts a hello world.

icr:1> puts 'hello world'
syntax error in :1
Error: unterminated char literal, use double quotes for strings

It’s a bad first impression (at least when i use Crystal first time)

so, i consider we should create a new RFC to talk on this?

See Would it be possible for '' to be accepted for Strings as well, as in ruby, or was that already rejected before? · Issue #10640 · crystal-lang/crystal · GitHub.

At first, it annoyed me, as I’m used to using ’ per default.

But having seen the amount of energy that’s been invested in the selection/fixing/discussion of either, in other languages, I’m pretty happy there’s only one choice in Crystal.

“Ah, you have to use single quotes because of performance… Ah, but if you decide you want to add an interpolation to your string, you have to change the quotes…”

It still bites me once in a while, and do like that ’ is an unshifted key, but it’s a sacrifice I’m willing to make (to quote Lord Farquaad).

8 Likes

May I assume that this conclusion has not been adequately discussed by the community?

Yes, you are right, I also think it’s a little tricky to make double quote or single quote different based on performance or interpolotion.

Can we just provide the equivalent of single or double quotation marks?

I think there have been enough expressions from members of the community, among them many Core Team members.
Invalidating char literals is a huge cost and it’s not even offset by any hard benefits, except introducing another syntax to do something that’s already possible (which may even be considered a non-goal).
So I don’t see this is going to happen. Certainly not in any forseeable future.

5 Likes

That principle of least surprise depends on the language you’re coming from.

In fact, while the vast majority of languages use double quotes for string literals, regardless of their support for single quoted strings, a non negligible number of languages use single quotes to denote chars: C, Go, Java, Rust, Crystal, …

Realizing that, I changed my habit, and I now always using double quotes for strings, whatever the language. That solved everything.

10 Likes

I actually always thought that the differences between single quoted strings and double quoted strings in languages like Ruby and Python were counterintuitive and violated the principle of least surprise in some way - but maybe that’s just my BASIC/C++ background.

Well, nice if you can, but might not be an option due to coding style guides.

The most pathological example is mandating single because of performance, unless the string contains a single quote, in which case you have to use double. Not because you can’t escape it, but because the translation guide mandates double quotes for strings with single quotes as the translation tools copies it verbatim, and it might trip up the translators…

Not to mention that having both throws you a curveball when you want to search for a particular string usage.

I have not paid much attention to this difference.
However, I don’t like so much the fact that there are more characters that have to be escaped in a double-quote string than in a single-quote string. (Though % literals are provided for this purpose.)

The single quoted strings in Ruby are one of the more annoying aspects of it IMO, and I really like that Crystal didn’t carry that over from Ruby. When I write Ruby code (which is rare for me - mostly Rakefiles), I just use double-quotes or the %{ style everywhere since, in my mind, a single quote is for characters, double is for strings.

The only other languages I’ve encountered where single quotes can be used for strings are shell scripts and Pascal (and they annoy me in Pascal for the same reasons, too lol).

1 Like

Thanks for so many explanation.

Make sense for me.

Based on my background, i wrote a lot of Ruby and BASH shell scripts, although i used to using double quotes under Crystal, based on personal habits, I rarely use %q or % (i consider it ugly), , I’d rather use a plain string or HEREDOC(both work on BASH shell too), it is kind of annoying occasionally because could not use single quotes write strings.