Scoop API

scoop:scoop.core:0.1.0

src/option.scoop

Selected production source · src/option.scoop

1// The Option type (spec 7.2). `T?` desugars to `Option<T>`, and the
2// variant names `Some` / `None` are resolvable without a prefix
3// (spec 4.2, 5.1).
4/**
5 * A value that is either [Some] or [None]. The spelling `T?` denotes `Option<T>`; absence is
6 * represented explicitly and can be matched exhaustively.
7 */
8public enum Option<T> {
9 /**
10 * Contains the present value.
11 */
12 Some(T),
13 /**
14 * Represents the absence of a value.
15 */
16 None
18 /**
19 * Inspection and serialization operations for this value type.
20 */
21 public companion object {
22 /**
23 * Builds an inspector that delegates a present payload to the supplied element inspector.
24 */
25 public fun inspector(element: ValueInspector<T>): ValueInspector<Option<T>> = OptionInspector<T>(element)
27 /**
28 * Builds an encoder that delegates a present payload to the supplied element encoder.
29 */
30 public fun encoder(element: ValueEncoder<T>): ValueEncoder<Option<T>> = OptionEncoder<T>(element)
32 /**
33 * Builds a decoder that delegates a present payload to the supplied element decoder.
34 */
35 public fun decoder(element: ValueDecoder<T>): ValueDecoder<Option<T>> = OptionDecoder<T>(element)
37 override interface ValueEncoder<Option<T>> where T.Companion : ValueEncoder<T> {
38 /** Encodes None as null and delegates Some to the element companion. */
39 public override fun encode(value: Option<T>, encoder: Encoder): Unit =
40 OptionEncoder<T>(T.Companion).encode(value, encoder)
41 }
43 override interface ValueDecoder<Option<T>> where T.Companion : ValueDecoder<T> {
44 /** Decodes null as None and delegates other values to the element companion. */
45 public override fun decode(decoder: Decoder): Option<T> =
46 OptionDecoder<T>(T.Companion).decode(decoder)
47 }
48 }