Skip to content
← Back

src/semantic/types/tuple.ghul

1
namespace Semantic.Types is
2
use Source.LOCATION
3
4
use Logging
5
6
use Ghul.Pipes
7
8
class TUPLE: GENERIC is
9
names: Collections.List[string?]?
10
11
tuple_element_names: Collections.List[string?]? => names
12
13
// A tuple type built from its element types is a value tuple on
14
// every target, whether or not its symbol came from reflecting
15
// .NET's ValueTuple.
16
is_value_tuple: bool => true
17
18
short_description: string => get_short_description(self, names, false)
19
20
// Build an element-name list of exactly `arity` entries from
21
// the leading entries of a flattened TupleElementNamesAttribute
22
// array, padding with null.
23
take_element_names(names: Collections.List[string?]?, arity: int) -> Collections.List[string?]? static is
24
if !names? then
25
return null
26
fi
27
28
let result = Collections.LIST[string?]()
29
30
for i in 0..arity do
31
result.add(if i < names.count then names[i] else null fi)
32
od
33
34
return result
35
si
36
37
38
get_short_description(tuple: Type, names: Collections.List[string?]?) -> string static =>
39
get_short_description(tuple, names, false)
40
41
// Qualified renders each element as it would be written where the
42
// tuple is read, scope-relative; the short form names each bare.
43
get_short_description(tuple: Type, names: Collections.List[string?]?, qualified: bool) -> string static is
44
let args = tuple.arguments
45
46
assert args |> all(a => a?) else "tuple at least one null argument"
47
48
let element = (i: int) -> string => if qualified then "{args[i]}" else args[i].short_description fi
49
50
return "({(0..args.count) |> map(i => "{_get_element_name(names, i)}{element(i)}") |> join()})"
51
si
52
53
_get_element_name(names: Collections.List[string?]?, index: int) -> string static =>
54
if names? /\ index < names.count /\ names[index]? /\ !Symbols.Symbol.is_positional_member_name(names[index]) then
55
if Lexical.TOKENIZER.is_reserved_word(names[index]) then
56
"`{names[index]}: "
57
else
58
"{names[index]}: "
59
fi
60
else
61
""
62
fi
63
64
init(
65
location: LOCATION,
66
symbol: Symbols.Classy,
67
arguments: Collections.List[Type],
68
names: Collections.List[string?]?
69
) is
70
super.init(Symbols.TUPLE(location, symbol, arguments, names))
71
72
self.names = names
73
si
74
75
create(
76
location: LOCATION,
77
symbol: Symbols.Classy,
78
arguments: Collections.List[Type]
79
) -> GENERIC =>
80
TUPLE(location, symbol, arguments, names)
81
82
apply_tuple_element_names(new_names: Collections.List[string?]) -> Type =>
83
if let generic_symbol = cast Symbols.GENERIC?(symbol) then
84
TUPLE(LOCATION.internal, generic_symbol.symbol, arguments, take_element_names(new_names, arguments.count))
85
else
86
self
87
fi
88
89
to_string() -> string => get_short_description(self, names, true)
90
si
91
si