V: String vs Array concatenation

Created on 2 Jan 2020  路  6Comments  路  Source: vlang/v

Hello,

This is more of a question regarding rationale: Why are the array and string concatenation operators different, i.e. << vs +? In my mind it would make more sense for them to be the same but I am sure there has to be a reason?

Thanks!

Feature Request

Most helpful comment

I think it is ok like it is now.
Different operators for different semantics:

mut a := [1,2,3]
a << 4 // pushes a value, modifying the first operand

b := "abc"
c := b + "def" // doesn't modify the operands


Besides that, the operator '<<' is very intuitive and is not a special syntax for the push method:

mut a := [1,2,3]
b := [4]
a.push(b) // would be [1,2,3,[4]] // type error

All 6 comments

Glance value and the one way philosophy.

I think it'd be nice to have a unique operator for string concatenation like in Perl, OCaml etc, but + is just too popular these days.

In my mind strings usually are just an array of characters and hence somewhat of the same thing. While I get the glance value argument, it scales weirdly for different data types (i.e. what about concatenating maps, linked lists, hash maps etc., should those all have different looking operators?). Then again, I get that a string is somewhat of a more basic type so maybe its a good thing. Just thinking out loud :)

I think it is ok like it is now.
Different operators for different semantics:

mut a := [1,2,3]
a << 4 // pushes a value, modifying the first operand

b := "abc"
c := b + "def" // doesn't modify the operands


Besides that, the operator '<<' is very intuitive and is not a special syntax for the push method:

mut a := [1,2,3]
b := [4]
a.push(b) // would be [1,2,3,[4]] // type error

Indeed, strings are immutable, unlike arrays.

Ah, I missed the part that strings are immutable, so I guess it makes sense from that perspective. That said, I think it might be worth considering to concatenate arrays (and possibly other data types where similar behavior makes sense) the same way if you want to make a copy and thus not modify the operands, i.e.:

a := [1,2,3]
b := [3, 4, 5]
c := a + b

On another note (this is somewhat off topic, but I don't think worth an extra issue): How do you write efficient string code for strings that need to be dynamically allocated at runtime and possibly changed if they are immutable? Do you just have to trust the compiler that it does string allocation efficiently?

Thanks!

a := [1,2,3]
b := [3, 4, 5]
c := a + b

Python does this way.

In V I have to do:

fn main () {
    a := [1,2,3]
    b := [4,5,6]
    mut c := a
    c << b
    d := c // if I need the result to be immutable
    //
    println(d)
}

I think you should open a specific issue about this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArcDrake picture ArcDrake  路  3Comments

radare picture radare  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

aurora picture aurora  路  3Comments

vtereshkov picture vtereshkov  路  3Comments