-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtypeComparison.grace
48 lines (47 loc) · 1.42 KB
/
typeComparison.grace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma noTypeChecks
dialect "standard"
import "mirror" as mirror
method methodsIn(DesiredType) missingFrom (value) -> String {
def vMethods = mirror.reflect(value).methodNames >> set
def tMethods = DesiredType.methodNames >> set
def missing = list.withAll(tMethods -- vMethods).sort
def n = missing.size
if (n == 0) then {
ProgrammingError.raise "{value.asDebugString} seems to have all the methods of {DesiredType}"
}
var s := ""
missing.keysAndValuesDo { ix, each ->
s := s ++ each
if (ix == (n - 1)) then {
if (n > 2) then {
s := s ++ ", and " // Oxford comma
} else {
s := s ++ " and "
}
} elseif { ix < n } then {
s := s ++ ", "
}
}
return s
}
method protocolOf(value) notCoveredBy (Q:Type) -> String {
def vMethods = mirror.reflect(value).methodNames >> set
def qMethods = Q.methodNames >> set
def missing = list.withAll(vMethods -- qMethods).sort
def n = missing.size
if (n == 0) then { return "" }
var s := ""
missing.keysAndValuesDo { ix, each ->
s := s ++ each
if (ix == (n - 1)) then {
if (n > 2) then {
s := s ++ ", and " // Oxford comma
} else {
s := s ++ " and "
}
} elseif { ix < n } then {
s := s ++ ", "
}
}
return s
}