Search code examples
hacklang

Function or method as value in collection


Is it possible to pass a function or method call as value in a collection of any type?

$collection = Vector {
    function(){}
};

The code above throws Fatal error: syntax error, unexpected T_FUNCTION, expecting '}'


Solution

  • Oops, this looks like a limitation of the HHVM parser. Below are a few options for working around it using locals. Feel free to file an issue on GitHub to support the literal syntax in your original question if it's important to you.

    Option 1:

    $v = Vector {};
    $f = function () {};
    $v[] = $f;
    

    Option 2:

    $f = function () {};
    $v = Vector {$f};
    

    etc.