The goal of the Hack Standard Library is to provide a consistent, centralized, well-typed set of APIs for Hack programmers. We aim to achieve this by implementing the library according to codified design principles.
This library is especially useful for working with the Hack arrays (vec
,
keyset
, and dict
).
For future APIs, see the experimental repository.
<?hh // strict
use namespace HH\Lib\{Vec,Dict,Keyset,Str,Math};
function main(vec<?int> $foo): vec<string> {
return $foo
|> Vec\filter_nulls($$)
|> Vec\map($$, $it ==> (string) $it);
}
Functions in the HSL are organized into namespaces according to the following rule:
If a function returns a particular type or only operates on that type, it belongs in that namespace.
Here are some examples:
Based on the vec
return type, you'd look in the Vec namespace and come across
Vec\keys
. Instead, if you wanted a keyset, you'd look in the Keyset namespace
and find Keyset\keys
.
Because the function is a math operation, you'd look in
the Math namespace and find Math\max
and Math\max_by
.
Automatically-generated documentation is available on docs.hhvm.com.
This project uses function autoloading, so requires that your projects use hhvm-autoload instead of Composer's built-in autoloading; if you are not already using hhvm-autoload, you will need to add an hh_autoload.json to your project first.
$ composer require hhvm/hsl
- All functions should be typed as strictly as possible in Hack
- The library should be internally consistent
- Arguments should be as general as possible. For example, for Hack array
functions, prefer
Traversable
/KeyedTraversable
inputs where practical, falling back toContainer
/KeyedContainer
when needed - Return types should be as specific as possible
- All files should be
<?hh // strict
This is not an exhaustive list.
- Functions argument order should be consistent within the library
- All container-related functions take the container as the first argument
(e.g.
Vec\map()
andVec\filter()
) $haystack
,$needle
, and$pattern
are in the same order for all functions that take them
- All container-related functions take the container as the first argument
(e.g.
- Functions should be consistently named
- If an operation can conceivably operate on either keys or values, the default
is to operate on the values - the version that operates on keys should have
a
_key
suffix (e.g.C\find()
,C\find_key()
,C\contains()
,C\contains_key()
) - Find-like operations that can fail should return
?T
; a second function should be added with anx
suffix that uses an invariant to returnT
(e.g.C\first()
,C\firstx()
) - Container functions that do an operation based on a user-supplied keying
function for each element should be suffixed with
_by
(e.g.Vec\sort_by()
,Dict\group_by()
,Math\max_by()
)
See CONTRIBUTING.md; in particular, new features should be contributed to the experimental repository instead.
The Hack Standard Library is MIT-licensed.