File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1
1
package types
2
2
3
+ import (
4
+ "bytes"
5
+ )
6
+
3
7
// Type is a type of a value
4
8
type Type interface {
5
9
// String returns a string like "<type %s>"
@@ -159,3 +163,43 @@ func (*anyType) String() string {
159
163
func (* anyType ) InstanceOf (_ Type ) bool {
160
164
return true
161
165
}
166
+
167
+ // ===================== Lambda type ===================== //
168
+
169
+ // NewLambdaType creates lambda type instance.
170
+ // Signature must have 1 type at least for a return type.
171
+ func NewLambdaType (t Type , rest ... Type ) Type {
172
+ signature := append ([]Type {t }, rest ... )
173
+ return & lambdaType {signature : signature }
174
+ }
175
+
176
+ type lambdaType struct {
177
+ signature []Type
178
+ }
179
+
180
+ func (t * lambdaType ) String () string {
181
+ var arg bytes.Buffer
182
+ for i := range t .signature {
183
+ if i > 0 {
184
+ arg .WriteString ("," )
185
+ }
186
+ arg .WriteString (t .signature [i ].String ())
187
+ }
188
+ return "Lambda[" + arg .String () + "]"
189
+ }
190
+
191
+ func (t * lambdaType ) InstanceOf (t2 Type ) bool {
192
+ lambda , ok := t2 .(* lambdaType )
193
+ if ! ok {
194
+ return false
195
+ }
196
+ if len (t .signature ) != len (lambda .signature ) {
197
+ return false
198
+ }
199
+ for i := range t .signature {
200
+ if ! t .signature [i ].InstanceOf (lambda .signature [i ]) {
201
+ return false
202
+ }
203
+ }
204
+ return true
205
+ }
You can’t perform that action at this time.
0 commit comments