forked from bcmills/go2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigure5_mono.go
44 lines (34 loc) · 1.06 KB
/
figure5_mono.go
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
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// figure5 illustrates figure 5 of Featherweight Go.
//
// Unfortunately, it does not compile because the interface Eq
// constrains its own type parameter.
package main
import "fmt"
// Featherweight Go, Fig. 5
// Eq is the Eq interface from figure 5, but without type parameters.
type Eq interface {
Equal(that Eq) bool
}
type Int int
func (this Int) Equal(that Eq) bool {
return this == that
}
// Pair is the Pair struct from figure 5, but with the type constraints
// strengthened to Eq because the final Go design for type parameters
// does not allow methods that refine the receiver's type constraints.
type Pair[a Eq, b Eq] struct {
left a
right b
}
func (this Pair[a, b]) Equal(that Pair[a, b]) bool {
return this.left.Equal(that.left) && this.right.Equal(that.right)
}
func main() {
var i, j Int = 1, 2
var p Pair[Int, Int] = Pair[Int, Int]{i, j}
fmt.Println(p.Equal(p)) // true
}