123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051open!ImportincludeBase.Optiontype'at='aoption[@@derivingbin_io,typerep]includeComparator.Derived(structtypenonrec'at='at[@@derivingsexp_of,compare]end)letvalidate~none~somet=letmoduleV=Validateinmatchtwith|None->V.name"none"(V.protectnone())|Somex->V.name"some"(V.protectsomex);;letquickcheck_generator=Base_quickcheck.Generator.optionletquickcheck_observer=Base_quickcheck.Observer.optionletquickcheck_shrinker=Base_quickcheck.Shrinker.optionmoduleStable=structmoduleV1=structtypenonrec'at='at[@@derivingbin_io,compare,equal,sexp]endendmoduleOptional_syntax=structmoduleOptional_syntax=structletis_none=is_none(* [unsafe_value] is only safe to call when [is_none] returns [false]. To avoid
repeating the [is_none] check, we declare [Unchecked_some]. [Unchecked_some x]
has the same representation as [Some x], but the type has no [None] clause.
We make sure all this works with tests of [unsafe_value] in test_option.ml.
We tried using [Obj.field] instead. It generates much worse native code due to
float array representations. *)moduleUnchecked_some=struct(* Warning 37 tells us [Unchecked_some] is never used as a constructor. This is
intentional, so we disable the warning. *)type'at=Unchecked_someof'a[@@ocaml.boxed][@@ocaml.warning"-37"]endletunsafe_value(typea)(t:at):a=let(Unchecked_somevalue)=(Obj.magict:aUnchecked_some.t)invalue;;endend