haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Allow pushing multiple values at once with arrays.

Open NeeEoo opened this issue 1 year ago • 2 comments

Currently you can only push a single value at a time.

However some targets like JavaScript support pushing multiple values at once, and this functionality could be easily implemented into hxcpp too.

Allowing multiple values to be pushed at once would improve performance, as platforms could preallocate the required space for the requested amount of items, rather than reallocating each time a value is pushed

This feature seems like a good candidate to add to Haxe 5.x.x

If we don't want to risk breaking programs by making it push(...val:T), we could make a new function named pushAll(...val:T)

Heres how to do it for different targets:

Javascript:

array.push(...values); // takes up to 65535 arguments

Python:

array.extend(values) # takes an array

Java:

// if ArrayList is used internally
array.addAll(values); // takes an array

PHP:

array_push($array, ...$values); // takes any amount of arguments

NeeEoo avatar Sep 05 '24 22:09 NeeEoo

I think I would propose an insertMany(pos:Int, args:Array<T>):Void; and then you can still have your pushAll as a special case of that, via static extension.

back2dos avatar Sep 10 '24 09:09 back2dos

No other target supports insert multiple values at once, to my knowledge and more targets support pushing multiple. If we want i would think it would be better to have pushMultiple/pushMany and insertMultiple/insertMany

NeeEoo avatar Sep 10 '24 14:09 NeeEoo

I'm not against this, but it's the kind of thing somebody would have to start implementing.

Simn avatar Nov 19 '24 05:11 Simn