Skip to content
← Back

src/semantic/type_variable_optional.ghul

1
namespace Semantic is
2
use Types.Type
3
4
use Symbols.TypeParameterConstraintKind
5
6
// The `T?` form of a type parameter, which lowers by the parameter's
7
// declared kind: a `struct` parameter to NULLABLE[T], a `class` one
8
// to a flagged reference, an `?` one is already optional, and an
9
// unconstrained one to `Ghul.MAYBE[T]`, the runtime carrier that
10
// picks neither lowering at declaration site and defers the
11
// absent/present distinction to a runtime flag. Null when the
12
// parameter is unconstrained and MAYBE is unavailable, which is the
13
// case while compiling ghul-runtime itself.
14
class TYPE_VARIABLE_OPTIONAL is
15
of(type_variable: Type, lookup: Lookups.InnateSymbolLookup) -> Type? static is
16
let kind = type_variable.symbol.constraint_kind
17
18
if kind == TypeParameterConstraintKind.VALUE then
19
return lookup.get_optional_type(type_variable)
20
elif kind == TypeParameterConstraintKind.REFERENCE then
21
return type_variable.as_optional()
22
elif kind == TypeParameterConstraintKind.OPTIONAL then
23
return type_variable
24
fi
25
26
return lookup.get_maybe_type(type_variable)
27
si
28
si
29
si