On 02/05/2017 11:53, Giovanni Giacobbi wrote:
Wouldn't be better to introduce a new native type "vector"? I know it
sounds obnoxious introducing a new basic data type, but I think having
a workaround like is_vectorlike() wouldn't completely address the problem.
...
function do_something_ex(vector $a) {
// do something with $a
}
I can certainly sympathise with the desire to have this built into the
type hint, but I think it leads to awkward questions about what kind of
"type" this would be.
If it was a "real" type, that would imply users could explicitly
construct it in some way, presumably with a new keyword. Indeed, in
strict_types mode, they would presumably be *obliged* to do so, or cast
their values, leading to noise like do_something_ex( (vector)$some_array );
It would be better in that case to have generics and a built-in List<T>
type, so we could type hint for List<int> without the performance hit of
checking the member types on every function call.
If, on the other hand, it was a pseudo-type only available in type
hints, then it raises the question of why this particular check gets a
keyword but others don't. For instance, positiveInt, nonNegativeInt,
nonEmptyString, finiteFloat, etc, would all be similar in
implementation: "assert type is X and condition Y is true". If not a
real type, then "vector" is fundamentally a constrained domain of
"array" values.
I also wouldn't call is_vectorlike() a workaround, as it serves a
different use case and would be necessary even if we had a vector type
hint. You can't choose a code path based on a type hint, since we don't
have function overloading (which is probably sensible in a dynamic type
system). We can always add the type hint later, as happened with the
"callable" type hint.
$a = [ "a", "b", "c" ];
is_vector($a); // true
is_array($a); // true
unset($a[1]);
is_vector($a); // false
is_array($a); // true
Other than the name of the function, this is exactly how the proposed
function would behave. Note that in this example, "vector" doesn't
behave like a type, since is_array() returns true, and gettype() would
still return 'array'.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php