vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.8k stars 2.17k forks source link

Cannot concatenate chars or runes into strings #17202

Open trufae opened 1 year ago

trufae commented 1 year ago

Describe the bug

Imho it should be easy for V to append chars or runes into strings. but i couldnt find an easy way to do that. This results in different errors i end up just using string interpolations to achieve the same, but definitvely it shouldnt perform that well.

Expected Behavior

This code should be valid imho:

mut s := 'hello'
s += ` `
s += 'world'

concatenating two runes should be also valid and must result into a string:

s := `h` + `e`

how can i convert a char into a rune? i didnt find any safe way to do that that doesnt involve complicated constructions things like this should work imho:

a := char(0x41)
b := a + "AAAAA";

also, a char, should be also valid for 1 byte runes. aka ascii

>>> println(a)
error: `println` cannot print type `char` directly, print its address or cast it to an integer instead
    6 | 
    7 | a := char(33)
    8 | println(a)
      | ~~~~~~~~~~
>>> println('$a')
error: expression returning type `char` cannot be used in string interpolation directly, print its address or cast it to an integer instead
    6 | 
    7 | a := char(33)
    8 | println('$a')
      |   

I coudlnt find the way to get the numeric value of a rune or a char, and viceversa. This is not what itoa/atoi is for, but to basically decompose a striung into an array of bytes, aka chars

Current Behavior

See above, none of this works and its not intuitive

Reproduction Steps

compile the snippets

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 e70848a

Environment details (OS name and version, etc.)

OS: linux, Linux version 5.15.90-0-lts (buildozer@build-3-17-x86_64) (gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924, GNU ld (GNU Binutils) 2.39) #1-Alpine SMP Wed, 25 Jan 2023 08:18:30 +0000 Processor: 4 cpus, 64bit, little endian, Intel(R) Celeron(R) N4120 CPU @ 1.10GHz CC version: cc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924

getwd: /home/pancake/prg/v-sax vmodules: /home/pancake/.vmodules vroot: /home/pancake/prg/v vexe: /home/pancake/prg/v/v vexe mtime: 2023-02-01 17:02:38 is vroot writable: true is vmodules writable: true V full version: V 0.3.3 e70848a

Git version: git version 2.38.3 Git vroot status: 0.3.3-20-g90ae3c82 (2 commit(s) behind V master) .git/config present: true thirdparty/tcc status: thirdparty-linuxmusl-amd64 6b2bc802

impopular-guy commented 1 year ago

decompose a striung into an array of bytes, aka chars

You can get string to byte(u8) array and vice versa:

a := 'qwerty β›”πŸ˜ŠπŸ€£πŸ˜βœ…πŸ•πŸ”πŸŸ'
b := a.bytes()
c := b.bytestr()

println(a)
println(typeof(b).name)
println(b.len)
println(c)

Output

qwerty β›”πŸ˜ŠπŸ€£πŸ˜βœ…πŸ•πŸ”πŸŸ
[]u8
37
qwerty β›”πŸ˜ŠπŸ€£πŸ˜βœ…πŸ•πŸ”πŸŸ
OK (0.002 sec real, 0.001 sec wall)
JalonSolov commented 1 year ago

While I agree it would be nice if

s += ` `

would work, this works now

s += ` `.str()

char on the other hand, is mainly for C interop. In V, the type should be u8, so

a := u8(0x41)
b := a.ascii_str() + "AAAAA";

will assign B the string 'AAAAAA' as expected.