-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type safety for dicts with mixed value types - and the future of json_decode #8653
Comments
This type almost always indicates that a shape sshould be used; for
If potentially unset,
Leave this until
Closing as:
|
json_decode does have a |
The long-term dream (that we've not thought through properly, and I don't think can't be implemented correctly yet) is |
Ah, I see, this sheds light onto the purpose of Shapes, I hadn't used them before; I was also unaware of reified generics and the TypeAssert library, this is all interesting stuff. It seems like TypeAssert is a more robust version of my class, so I'll definitely migrate my code to use it. Cheers for all of the great work. |
Is your feature request related to a problem? Please describe.
Often, it is necessary to map keys to values of mixed types - the legacy
json_decode
function most often returns a legacy array as a mapping from a string to a mixed type (at least in the PHP documentation - I could not find anything referring to the Hack adaptation specifically). Similarly, I've written a parser in Hack for TOML, which canonically represents a mapping between strings and mixed values.Since the legacy PHP object is being replaced with the new type-safe
vec
anddict
types, there is a clear need for a concise type-safe way to extract values from such dicts.Describe the solution you'd like
As a temporary solution for my parser, I made a class that wraps around a
dict<string, nonnull>
to provide the required methods. They're essentially all different versions of this function, which returns a string, for each of the other primitive types:I also included functions whose name begins with an underscore, such as
function _string(string $key) : ?string
, which returnNULL
instead of throwing an exception for a non-existent key.If the team agrees that this type of solution would work best, I'd be happy to close this issue, tidy up that code and submit a pull request to HSL through the
hsl-experimental
repo.Describe alternatives you've considered
Of course, the Hack team could have other plans for this issue, such as directly implementing this functionality in the
dict
class itself.Regardless, the biggest question I have moving forward is: what are the current plans for the future of json_decode and similar functions, that are currently documented to use the legacy array type? I think there should be a clear precedent for projects such as my Hack TOML parser to follow when it comes to handling data access like this, and an elegant solution will be very powerful and time-saving in the long run.
Thanks and cheers.
The text was updated successfully, but these errors were encountered: