Skip to content
← Back

src/semantic/types/maybe.ghul

1
namespace Semantic.Types is
2
3
use Source.LOCATION;
4
5
// `Ghul.MAYBE[T]` — the runtime's unconstrained-T optional
6
// carrier. is_maybe drives the implicit conversion to `T?` at
7
// slot boundaries; surfacing it as a distinct GENERIC subclass
8
// means recognition is a virtual-property check (no per-call
9
// symbol lookup, and source-side MAYBE structs inside
10
// `ghul-runtime`'s own compile stay as plain GENERIC where the
11
// conversion is correctly inactive).
12
class MAYBE: GENERIC is
13
is_maybe: bool => true;
14
15
is_optional: bool => true;
16
17
optional_inner_type: Type? => arguments[0];
18
19
short_description: string => get_short_description(self);
20
21
get_short_description(reference: GENERIC) -> string static => "{reference.arguments[0].short_description}?";
22
23
init(
24
location: LOCATION,
25
symbol: Symbols.Classy,
26
arguments: Collections.List[Type]
27
) is
28
super.init(location, symbol, arguments);
29
si
30
31
create(
32
location: LOCATION,
33
symbol: Symbols.Classy,
34
arguments: Collections.List[Type]
35
) -> GENERIC =>
36
Types.MAYBE(location, symbol, arguments);
37
38
// MAYBE[T] accepts a value of T via the implicit T -> T? widening,
39
// the same rule NULLABLE uses: the boxer emits a MAYBE<T>::.ctor
40
// wrap at the slot boundary. Reported as ASSIGNABLE, not SAME, so
41
// an overload resolver with both f(T) and f(T?) candidates and a T
42
// argument still prefers f(T). Shares NULLABLE's helper — both are
43
// value-type optional carriers with a single-argument present-value
44
// constructor.
45
compare(other: Type) -> Types.MATCH =>
46
Types.NULLABLE.compare_optional(self, other, super.compare(other));
47
48
to_string() -> string => "{arguments[0]}?";
49
si
50
si