Hhvm: Type hinting variadic arguments

Created on 18 Feb 2015  路  12Comments  路  Source: facebook/hhvm

If we don't type hint variadic arguments we get this following type checker error.

Was expecting a type hint (what about: array)

If we do type hint them, the type checker error goes away, but then we get this runtime error.

Fatal error: Parameter $args is variadic and has a type constraint (array); variadic params with type constraints are not supported

What's the best way to go about this?

hack probably easy

Most helpful comment

If PHP 7 supports it, you have to support it eventually.

All 12 comments

The runtime doesn't have full support for variadics yet (they can't be typed). The main concern is that in PHP 5.6 type-checking a variadic requires looping over the variadic array to check each argument. We plan to add support for this, but in the meantime if you want to use strict mode with variadics you'll need to add an HH_FIXME,

function foo(
  int $arg1,
  bool $arg2,
  /* HH_FIXME[4033]: variadic + strict */ ...$args
): string {
  // ...
}

Had a chat with @dlreeves and @sgolemon; we're likely to:

  • at runtime, ignore the type in Hack files and trust the typechecker (this is consistent with other array parameters, if we just consider variadics to be syntactic sugar over them)
  • keep on banning them in PHP files as (1) we've not came across _any_ real-world examples of people typing variadics in PHP code (2) the runtime verification that PHP requires is very expensive (3) it's better to loudly complain than to silently allow bad parameters

I'm curious what's expensive about it? It seems like a basic O(N) problem, which _shouldn't_ be too large, usually under 10 items.

an O(n) problem at runtime is a large perf issue in the context of HHVM.

True, but it seems like variadic arguments would be so sparse, that the cost incursion would be relatively minor. Coming from an outside perspective at least.

It's also almost unnecessary in Hack (same exceptions as generics), and it would be weird to have different validation behavior for Hack and PHP.

If PHP 7 supports it, you have to support it eventually.

HH_FIXME[4033] doesn't work for type hinted variadics:

$ cat test.php 
<?php

function test(/* HH_FIXME[4033] */ int ...$numbers) {
    echo array_sum($numbers);
}

test(1, 2, 3);

$ hhvm test.php 
Fatal error: Parameter $numbers is variadic and has a type constraint (int); variadic params with type constraints are not supported in /path/to/test.php on line 3

$ php test.php 
6

Any thoughts on how to remedy this?

Closing - supported in '

@mschwager, @lstrojny : #6954

@mschwager : HH_FIXME has no runtime affects; it only affects the Hack typechecker, and your code isn't hack.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zkiiito picture zkiiito  路  7Comments

octmoraru picture octmoraru  路  5Comments

tslettebo picture tslettebo  路  5Comments

doublecompile picture doublecompile  路  3Comments

fredemmott picture fredemmott  路  3Comments