Videos
Arrays in php are meant to be both dictionaries and vectors. That's very confusing. Also in arrays, there is key coercion to integer. Which is more confusing.
Hack dicts are meant to remove the ambiguity and fix the problem with the keys.
To expand a bit:
- Hack is diverging from PHP - ultimately, this is likely to mean that PHP arrays are either removed from the language at some point, or become rather inconvenient. Ultimately, if you're writing Hack, you should prefer Hack dict/vec/keyset over PHP arrays.
- on Pablo's point,
array<string, Tv>is a lie, and breaks Hack's type system.array_keys(['123' => 'bar'])[0]is anint, not astring. This isn't the case fordictorkeyset. - the Hack Standard Library provides a consistent API for Hack Arrays. While most of the container functions can take PHP arrays as input, they will produce Hack arrays.
A more interesting question is "Hack arrays" (vec, dict, keyset), vs "Hack collections" (Map, Set, Vector) and their const/immutable relatives. This is pretty contentious.
The main differences are that they are objects, not values; this effectively means that functions you pass them to can mutate them, whereas vec/dict/keyset act as if they are copy-on-write. The copy-on-write behavior is usually desired, but occasionally, object behavior is desired.
This is where it becomes contentious:
- some argue that if you want object-like semantics, you should use Hack Collections
- I personally think it's better to wrap it in a 'Ref' class: e.g. class Ref<T> { public function __construct(public T $value) {} - and operate on $ref->value using the standard API; this lets you use the same API (the HSL) for both, rather than the slightly different one that collection objects have
The recommendation from the HHVM/HackLang guys is to use C\contains() as you can read here but you need to install the HSL package (hhvm/hsl) using composer. This library contains loads of functions to deal with the new array-type structures: vec, dict, keyset. Those functions in the HSL Library are prefixed with C, Vec, Dict, Keyset and you'll find also Math, Str, etc., very useful for other needs.
After installing that package it becomes available but typically it is more handy to add use namespace to avoid the long prefix:
use namespace HH\Lib\C;
This is how you can do it:
$exists = C\contains($occupiedNumbers, $newItemNumber);
If you are really looking for the longest continuous interval starting from count($occupiedNumbers) then it may be much faster to find its initial index in the sorted list (if it exists), then use direct indexing from there:
// `use namespace HH\Lib\{C, Vec};` at top level
$occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16];
$sorted = Vec\sort($occupiedNumbers); // if not sorted in general
$size = count($sorted);
$v = $size;
$i = C\find_key($sorted, $w ==> $w === $v);
if($i !== null) {
for(; $i < $size - 1 && $sorted[$i + 1] === $v + 1; $i++, $v++) {
}
}
// $i is null | <index of largest number in continuous interval>