Library of Assembled Shared Sources
|
Wrapper to type-hint return values in Python that maybe None
but not likely.
More...
#include <maybe_none.h>
Public Member Functions | |
T & | reference () |
Return reference to wrapped value. | |
const T & | reference () const |
Return reference to wrapped value. | |
Wrapper to type-hint return values in Python that maybe None
but not likely.
Use this class to wrap a type T that could be None in Python (for example, a pointer type), but it is not likely to be None and forcing the user to expliclitly check for None
can more painful than helpful. Annotating the type as T | MaybeNone
instead of T | None
will make static type-checkers like mypy ignore the None
case, while still allowing the user to check for None
if they want to. See The Any Trick for more information.
None
values from being passed or returned, and it does NOT provide any runtime or compile-time checks whatsoever. It only alters the type-hint in Python for static type-checkers.MaybeNone<T> types can only be returned from C++ to Python, it doesn't make sense to have them as function parameters in C++.
Built-in support for MaybeNone is provided for raw pointers T*
, util::SharedPtr<T>, std::shared_ptr<T>
, and std::optional<T>
. To support your own types, you must specialize PyExportTraits, see PyExportTraitsMaybeNone for more information.
Definition at line 81 of file maybe_none.h.