Skip to content
← Back

src/semantic/mut_local_join.ghul

1
namespace Semantic is
2
use Types.Type
3
4
// The type a `mut` local with an initializer settles on once the values
5
// assigned to it are known: the join of the initializer and those values,
6
// where that join is a type written for them to share. A join that exists
7
// only because every type has one - object, System.ValueType, or a trait
8
// neither the initializer nor any assigned value is typed as - and any
9
// join involving a value type, which would be a conversion rather than a
10
// common supertype, leaves the local at its initializer's type, so an
11
// incompatible assignment is still reported where it is written.
12
class MUT_LOCAL_JOIN is
13
init() is
14
super.init()
15
si
16
17
type_for(initializer: Type, joined: Type?, contributors: Collections.Iterable[Type]) -> Type is
18
if !joined? \/ !joined.is_settled \/ joined.is_equivalent_to(initializer) then
19
return initializer
20
fi
21
22
if joined.is_object \/ joined.is_root_value_type \/ joined.is_value_type then
23
return initializer
24
fi
25
26
for contributor in contributors do
27
if contributor.is_value_type then
28
return initializer
29
fi
30
od
31
32
if joined.is_trait then
33
for contributor in contributors do
34
if contributor.is_equivalent_to(joined) then
35
return joined
36
fi
37
od
38
39
return initializer
40
fi
41
42
return joined
43
si
44
si
45
si