V: Escape double quote character in double quoted string

Created on 3 Oct 2019  Â·  7Comments  Â·  Source: vlang/v

V version: 1.3.0
OS: MacOS

What did you do?

fn main() {
    a := 10 
    println("\"$a")
}

Compiler error:

error: use of undeclared identifier 'd'
printf( "\\"%d\n", a ) ;
             ^

What did you expect to see?
Compile successfully and run with output "10

What did you see instead?

Bug Mac

Most helpful comment

True, the correct example which is a bug of compiler is

fn main() {
    a := 10 
    println("\"$a")
}

Thanks for correcting. And yes, I would go against allowing 2 types of quoting.

All 7 comments

println(""$a')

Seems to me you mismatched quote strings here.

TBH, I knew allowing 2 types of quoting would be trouble.

True, the correct example which is a bug of compiler is

fn main() {
    a := 10 
    println("\"$a")
}

Thanks for correcting. And yes, I would go against allowing 2 types of quoting.

I think the reason @medvednikov preferred ' as first was that you are able to enter it without bothering with SHIFT or ALT - if I remember correctly the auto-code-formatter will convert all " enclosed strings to ' (and then only when no ' is occurring - which I don't understand, as the parser could easily escape them and keep all string consistent...)

You should prefer using single quotes for strings in V.
The way you should do you example is :

fn main() {
        a := 10
        println('"$a')
}

I kind of prefered to only using single quotes, no confusion.

You should prefer using single quotes for strings in V.
The way you should do you example is :

fn main() {
        a := 10
        println('"$a')
}

I kind of prefered to only using single quotes, no confusion.

It's not about preference, if V accepts double quote but shows compile error for example above, clearly it's compiler bug. Agree we should embrace single quote only.

yeah, but multiple quoting types breaks down when you need to use all of them in the string.
Some sort of escaping is needed unless you have raw strings.
It is escaping a quote in general that is not allowed in V.

V 0.1.23 68b4dab
Use Ctrl-C or `exit` to exit
>>> a := "foo"
>>> b := "foo\"bar"
/tmp/v/.vrepl_temp.tmp.c: In function ‘main’:
/tmp/v/.vrepl_temp.tmp.c:3257:24: error: expected ‘)’ before ‘bar’
 3257 |  string b= tos3("foo\\"bar") ;
      |                        ^~~
      |      ...
(Use `v -g` to print the entire error message)
V error: C error. This should never happen. 
Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
>>>
>>> c := "foo\cbar"
>>> c
foocbar
>>>

The documentation does not mention escaping quotes.
https://vlang.io/docs#strings

However, with string interpolation doing a lot of escaping is not as needed.

$ v
V 0.1.23 68b4dab
Use Ctrl-C or `exit` to exit
>>> q := '"'
>>> d := 'dog'
>>> a := '$q$d$q'
>>> a
"dog"
>>>  d
dog
>>>

Fixed by #6474

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arg2das picture arg2das  Â·  3Comments

radare picture radare  Â·  3Comments

aurora picture aurora  Â·  3Comments

oleg-kachan picture oleg-kachan  Â·  3Comments

ArcDrake picture ArcDrake  Â·  3Comments