Download the PHP package hershel-theodore-layton/expr-dump without Composer
On this page you can find all versions of the php package hershel-theodore-layton/expr-dump. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hershel-theodore-layton/expr-dump
More information about hershel-theodore-layton/expr-dump
Files in hershel-theodore-layton/expr-dump
Package expr-dump
Short Description Dump runtime values to typed Hack source code.
License MIT
Informations about the package expr-dump
expr-dump
Dump runtime values to typed Hack source code.
Why do you need this?
Run code during build-time, codegen the values you were interested in back to Hack code, write it to a Hack source file, and you have build-time compute.
The following snippet[^1] embodies the essence of this library perfectly.
All data that enters your program at runtime is typed as mixed
, which is why
you must cast it to a typed value in order to process it. If you do this safely,
this incurs runtime costs. Doing it using an unsafe mechanism, such as
HH\FIXME\UNSAFE_CAST
or HH_FIXME[4110]
is trading correctness for performance.
But with burn_a_value_to_constant
, the data doesn't enter the program at runtime.
This data is already typed, so there is no need for casting.
Why the existing tools can not meet this need
In Hack, the same runtime value can represent two different types.
HHVM is unable to distinguish between these types, but Hack is, and will emit errors if you initialize a value with the wrong type, even if the runtime value would be exactly the same as the correct initializer.
const (int, int) A_TUPLE = vec[1, 2]; // type error
ExprDump\dump<reify T>(T $value): string
takes a type and a value.
This gives it enough information to represent shapes as shapes, tuples as tuples,
enums as enums, whilst not confusing them for dicts, vecs, and arraykeys.
Usage
Note about the stability of this api
This library depends on HTL\TypeVisitor
to provide its functionality. TypeVisitor
depends on unstable Hack apis,
TypeStructure<T>
and \HH\ReifiedGenerics\get_type_structure<T>()
. For more
details, see stability.
This api has been unstable since 2016, so take this with a grain of salt.
In order to minimize the potential impact of a removal of these apis, you should
not use this library in places where the performance of bootstrapping the dumper
is critical. A far less performant variant of TypeVisitor
could be written, even
without these api affordances.
[^1]: This snippet burn_a_value_to_constant
is excempt from the MIT license
of this library. It is licensed to you under MIT-0 (MIT No Attribution).
This excemption does not apply to the code called by this snippet. The MIT
license still covers all other parts of this program.