Search code examples
phpdictionaryvectorhhvmhacklang

array_merge for Maps and Vectors at hacklang


Given the following code

<?hh
$a = Vector {"qux"};
$b = Vector {"foo","bar"};

$a = array_merge($a, $b);

The following error is raised:

Warning: Invalid operand type was used: array_merge expects array(s)

Is there any workaround for array_merge for vectors and maps, without making a foreach by hand, of the vector?


Solution

  • Answering to my self.

    In fact array_merge is not listed under the list of functions with support for hack Collections.

    The best way to merge Vectors is the function addAll.

    <?hh
    $a = Vector {"qux"};
    $b = Vector {"foo","bar"};
    
    $a->addAll($b);
    
    var_dump($a);
    

    In the case of Map, it's a bit more complicated and a combination of foreach and addAll is required to set the Pair <Tk,Tv>