Godot: Any chance to have Array.join(Array)?

Created on 8 Sep 2017  路  15Comments  路  Source: godotengine/godot

archived

Most helpful comment

You know you can do: [1, 2] + [3, 4, 5], right?

All 15 comments

What would it do? Is it like this?: https://www.w3schools.com/jsref/jsref_join.asp

By the name of the issue, it sounds more like OP wants a function more like concat.

A join method I feel would be useful on string, like how python operates.

You know you can do: [1, 2] + [3, 4, 5], right?

For me, I'd assume the + operator would do addition on each element and return [4, 6, 5].

@Noshyaar Sounds like something that APL or Matlab would do

@vnen lol its works, everyday learning something new :3

@raymoo that join is useless in gdscript because we can't override to_string() (so sad about this)
@vnen I didn't know that. But I think it is a little confusing using "+" operator since it is used as "union" operator and in most cases it is used as "or" operator... I would prefer a simple method (listed in the doc) (:

@lorenzobeccaro Union is similar semantically to "or": a value is in a1 + a2 if it is in a1 or a2. In fact this is how "or" is defined on sets when considered as a boolean algebra.

@raymoo got it.
Since there is a way to join two arrays i'll close this issue. But docs must to have some infos about operators working with classes imho (this infos for example should be in Array page).

For me, I'd assume the + operator would do addition on each element and return [4, 6, 5].

Well, by this logic I would expect a compiler error as it would be doing vector math with a Vector3 + a Vector2, which is undefined.

But as in Python, you concatenate lists (in GDScript, arrays) using +, it's quite common.

@akien-mga I use that example just to say that + operator can be interpret differently and it's not really obvious about what it does. + for concat also makes sense. Personally I find [1,2,3].join([4,5]) more understandable.

Well I guess there would be no hurt in adding support for Array.join() too if it's wanted.

Could it be called Array.concat or Array.extend instead of join, as in every other commonly used language join turns an array into a string. Then just make the + operator alias to concat

Having a separate concat might make sense actually. Let's say you have array1 and array2. You want to do something like array1 += array2, because array1 is a big array where you want to add smaller arrays. Well, correct me if I'm wrong, but what happens in this case is that Godot is allocating a third array actually, so array1 = array1 + array2 is actually some array3 = array1 + array2. Concat would be there if you want to add more arrays without allocating anything new.

I agree with KoBeWi. Being able to mutate the original array without creating a new one is an important use-case that should be supported.

Was this page helpful?
0 / 5 - 0 ratings