12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152open!ImportincludeBase.Optiontype'at='aoption[@@derivingbin_io,typerep,stable_witness]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,hash,sexp,sexp_grammar,stable_witness]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