Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any chance to have Array.join(Array)? #11073

Closed
spaghiajoeojo opened this issue Sep 8, 2017 · 15 comments
Closed

Any chance to have Array.join(Array)? #11073

spaghiajoeojo opened this issue Sep 8, 2017 · 15 comments
Labels

Comments

@spaghiajoeojo
Copy link

No description provided.

@raymoo
Copy link
Contributor

raymoo commented Sep 8, 2017

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

@erodozer
Copy link

erodozer commented Sep 8, 2017

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.

@vnen
Copy link
Member

vnen commented Sep 8, 2017

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

@ghost
Copy link

ghost commented Sep 8, 2017

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

@raymoo
Copy link
Contributor

raymoo commented Sep 9, 2017

@Noshyaar Sounds like something that APL or Matlab would do

@sezit
Copy link

sezit commented Sep 9, 2017

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

@spaghiajoeojo
Copy link
Author

spaghiajoeojo commented Sep 9, 2017

@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) (:

@raymoo
Copy link
Contributor

raymoo commented Sep 9, 2017

@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.

@spaghiajoeojo
Copy link
Author

@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).

@akien-mga
Copy link
Member

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.

@ghost
Copy link

ghost commented Sep 13, 2017

@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.

@akien-mga
Copy link
Member

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

@erodozer
Copy link

erodozer commented Sep 13, 2017

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

@KoBeWi
Copy link
Member

KoBeWi commented Jun 2, 2018

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.

@levilindsey
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants