0

I'm looking for the most efficient method to append a dynamic array to an existing dynamic array.

I know I could do:

bytes4[] private _array1 = [1,2,3];   
bytes4[] private _array2 = [4,5,6];

for( i = 0; _array2.length > i ; i++ ){
  _array1.push(_array2[i]);
}

But would something like this work?

bytes4[] private _array1 = [1,2,3];   
bytes4[] private _array2 = [4,5,6];

_array1.push(_array2);

If there a different more efficient method then just iterating over one array pushing each entry into another array?

4
  • No, there isn't. Also, you may want to explain why you even want to do something like that to begin with, so somebody here might be able to suggest a better approach. Note that the real hit (gas cost) is actually not for the number of iterations, but rather for the storage reads and writes. So if anything, this is what you should be trying to optimize. – goodvibration Dec 16 '20 at 21:35
  • I have lots of uses for this. But the primary one is developing a workflow delegation process by storing an array of selectors for a contract. This way a contract can query for a contract that can execute a workflow. – cyotee doge Dec 16 '20 at 23:10
  • But why merge one array into the other? – goodvibration Dec 16 '20 at 23:18
  • As part of an contract instantiation process. I'm basing this on ERC165/ERC1820, and forking OpenZeppelin. I've added to each of the contract I'm inheriting from an initialization method where it stores the function selectors it's implemented. Then a contract can register itself as a executor for a step in a workflow. I know that you would know the selectors during development because you simply get the function selector from the interface. But only interfaces you've imported. This process enables interaction with unknown interfaces by requiring contracts to report their function selectors. – cyotee doge Dec 20 '20 at 13:59
0

No it is not possible.

The only way to concatenate arrays is by using code similar to this.

bytes4[] private _array1 = [1,2,3];   
bytes4[] private _array2 = [4,5,6];

for( i = 0; _array2.length > i ; i++ ){
  _array1.push(_array2[i]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.