Skip to content
← Back

src/semantic/non_generic_preference.ghul

1
namespace Semantic is
2
use Ghul.Pipes
3
4
// When a non-generic overload may win a call it ties on with a
5
// specialized generic one. It may only when it is at least as
6
// specific: each of its formals converts to the generic's formal in
7
// the same position. A legacy overload taking a catch-all type
8
// (`object`, `List[object]`) is less specific than the generic, so it
9
// doesn't capture a call the generic serves more precisely.
10
class NON_GENERIC_PREFERENCE is
11
// Whether some specialized generic among `candidates` takes a
12
// formal that `candidate`'s formal in the same position doesn't
13
// convert to.
14
is_less_specific_than_a_generic(
15
candidate: Symbols.Function,
16
candidates: Collections.List[Symbols.Function]
17
) -> bool static =>
18
candidates |> any(g => g.specialized_from? /\ !converts_to(candidate, g))
19
20
// Whether each of `from`'s formals converts to `to`'s formal in
21
// the same position.
22
converts_to(from: Symbols.Function, to: Symbols.Function) -> bool static is
23
if from.arguments.count != to.arguments.count then
24
return false
25
fi
26
27
for i in 0..from.arguments.count do
28
let match = to.arguments[i].compare(from.arguments[i])
29
30
if match != Types.MATCH.SAME /\ match != Types.MATCH.ASSIGNABLE then
31
return false
32
fi
33
od
34
35
return true
36
si
37
si
38
si