@@ -13,10 +13,11 @@ which has been one of the most wanted features.
13
13
14
14
15
15
# Guide-level explanation
16
- There are two parts of the proposal: First, we address typing declarations of
16
+ There are three parts of the proposal: First, we address typing declarations of
17
17
generic interfaces. Then, we show how the type checker checks structs' method
18
- definitions.
18
+ definitions. Lastly, we present how type checking method application works.
19
19
20
+ ## Declaration of typed generic inferfaces.
20
21
To annotate a generic interface, we introduce a typed counterpart for
21
22
` define/generics ` .
22
23
@@ -71,6 +72,7 @@ cannot tell which argument would act as the specializer based on the type of
71
72
method-id-as-symbol ...)`, and values are simply booleans.
72
73
- #: derive-property , TODO
73
74
75
+ ## Typed method specialization
74
76
For method implementation in a struct's definition, the typechecking process is
75
77
also straightforward
76
78
@@ -86,10 +88,37 @@ also straightforward
86
88
)
87
89
```
88
90
89
- First, the typechecker ensures every ` method-id ` in a ` #:methods ` specification
90
- is well scoped. Then it checks if the specializer argument's and return type
91
- are covariant and if the rest are contraviant. ` define/generic ` makes the local
92
- id ` super-show ` have the same type of ` gen-show ` , namely ` (-> showable String) ` .
91
+ For any structure that implemented generice interfaces, first the typechecker
92
+ ensures every ` interface-id ` in a ` #:methods ` specification is well scoped. It
93
+ then checks if every method of ` interface-id ` is implemented. There are 3
94
+ possibilities of a method to be considered implemented:
95
+
96
+ 1 . The method is defined in the ` #:methods ` specification. Then it checks if the
97
+ specializer and return type are covariant and if the rest are contraviant.
98
+ ` define/generic ` makes the local id ` super-show ` have the same type of
99
+ ` gen-show ` , namely ` (-> showable String) ` .
100
+ 2 . The interface includes a well-typed fallback implementation for the method.
101
+ 3 . In either ` #:defaults ` or ` #:fast-defaults ` , there is a type predicate for
102
+ ` T ` such that T is a super type of the current structure type.
103
+
104
+ Note that under the proposed rules, all methods of a generic interface must be
105
+ implemented, which is different from that in Racket. Consider the following code:
106
+
107
+ ```
108
+ (struct fruit (name)
109
+ #:methods gen:showable
110
+ [(define (gen-show me)
111
+ (format "~a" (fruit-name me)))])
112
+
113
+ (let ([a (fruit "apple")])
114
+ (when (showable? a)
115
+ (gen-show2 'whatever a)))
116
+ ```
117
+
118
+ Racket recognizes a ` fruit ` instance showable, despite that ` fruit ` lacks
119
+ implementation of ` gen-show2 ` . Then a subsequent call ` gen-show2 ` on that
120
+ instance raises a run-time error. However, TR should reject the program above.
121
+
93
122
94
123
Though Racket doesn't support subclass or inheritance between generic
95
124
interfaces, we can still express constraints using types in ` define/generics ` .
@@ -145,6 +174,8 @@ because an `Dummy` instance breaks the contract of `gen-/=`.
145
174
However, the typechecker simply rejects the code. Since ` Dummy ` does not
146
175
implement ` gen:eq-able ` , it is not of a subtype of ` (Intersection eq-able ord-able) `
147
176
177
+ ## Typechecking Method Application
178
+
148
179
# Reference-level explanation
149
180
Add a new prim for ` define-generics ` that supports the features mentioned in the
150
181
Guide-level explanation.
0 commit comments