Add HPHP::Optional
commitef8aff5397495a94c2a5e1a25fcc83b646d0b86b
authorRick Lavoie <rlavoie@fb.com>
Wed, 23 Jun 2021 04:43:57 +0000 (22 21:43 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 23 Jun 2021 04:45:29 +0000 (22 21:45 -0700)
tree6f4fd6b502f62359308cd62e6109733a9fbb984a
parent77ea9b764a5084a941e4292eba63d7a3b2d039c7
Add HPHP::Optional

Summary:
folly::Optional has the undesirable property that attempting to access
the optional when it is disengaged will throw an exception. This makes
such bugs difficult to diagnose, since the exception unwinds the
stack. In theory we could catch the exception and try to recover from
it, but such behavior is always a bug.

We could use std::optional instead, but that simply defines such an
access as undefined behavior (current implementation seems to do no
checks). So, that's hardly better.

Instead provide a drop in replacement for std::optional called
HPHP::Optional. This has (almost) the exact interface of
std::optional, but accessing value when disengaged will always
assert. Going forward, this is meant to be used instead of
std::optional or folly::Optional.

The implementation just wraps a std::optional, but inserts the
necessary checks.

Reviewed By: mofarrell

Differential Revision: D29291162

fbshipit-source-id: ab8dc39cd967fafe9cefe633af078c99c67fc9dd
hphp/util/optional.h [new file with mode: 0644]