[Question] The key reason not accept single quotes string in Crystal

I come from ruby, you know, we have many GOOD habit, e.g. i always write string use double quotes, unless, we need interpolate or escape like “\n” or “\t”

After i switch to Crystal, the biggest trouble, is, i always write string like this: (rubocop fans)

string = 'this is a normal string'

It okay anyway, just curious, why do this decision? more simple compiler? or just follow others static language, like go, rust ?

Thank you.

tl;dr to allow single quotes to represent Char type characters, while double quote is for String type values.

EDIT: Is also some discussion in 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.

3 Likes

As an additional note, you can use percent string literals if you don’t want interpolations to be interpreted in a string: String - Crystal

3 Likes

Thank you for help, i read both links, i guess double quote with interpolation no performance penalty because Crystal is compiled instead interpreter directly in runtime.

anyway, this is not a minimal surprise when switch from Ruby to Crystal, that ok, i consider change my rubocop config to force use double quote instead anyway, thank you emacs, i have a function to do this transform with just a hotkey.

2 Likes

i guess double quote with interpolation no performance penalty because Crystal is compiled instead interpreter directly in runtime.

FWIW, In Ruby there’s also no performance difference between single quote and double quote with string literals.

See for example the disassembly in Ruby for double quotes:

> puts RubyVM::InstructionSequence.disasm(->() { "Hi" })
0000 nop                                                              (   5)[Bc]
0001 putstring                              "Hi"[Li]
0003 nop
0004 leave                                                            (   5)[Br]

and single quotes:

> puts RubyVM::InstructionSequence.disasm(->() { 'Hi' })
0000 nop                                                              (   4)[Bc]
0001 putstring                              "Hi"[Li]
0003 nop
0004 leave                                                            (   4)[Br]

I think that if there was a performance different it would come from parsing the single quote string vs. the double quote string. But I recently read an article that showed that there’s no difference at all.

Yes, i think ruby optimize this since some version.