Skip to content

Commit d874765

Browse files
committed
all: use latest type set grammar and 1.18
1 parent 75b5322 commit d874765

File tree

16 files changed

+529
-430
lines changed

16 files changed

+529
-430
lines changed

README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go2generics [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/go2generics)](https://pkg.go.dev/golang.design/x/go2generics) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/go2generics)
22

3-
A chunk of demos and research regarding Go 2 generics (type parameters).
3+
A chunk of demos and research regarding Go 2 generics (type parameters and type sets).
44

55
## Getting started
66

@@ -16,10 +16,55 @@ You can use the latest Go tip version to build all code examples in this reposit
1616
```sh
1717
$ git clone https://github.com/golang/go && cd src && ./all.bash
1818

19-
$ ../bin/go test -gcflags=all=-G=3 -v ./...
19+
$ ../bin/go test -v ./...
2020
```
2121

22-
The `-gcflags=all=-G=3` is the key compiler flag to run Go code with type parameters.
22+
## Working Examples
23+
24+
The current compiler implementation is still under development.
25+
These examples can be run without an error:
26+
27+
```
28+
gotip run demo/ex1-sort.go
29+
gotip run demo/ex2-mapreduce.go
30+
gotip run demo/ex3-stack.go
31+
gotip run demo/ex4-map.go
32+
cd errors && gotip test
33+
cd fmt && gotip test
34+
cd future && gotip test
35+
cd linalg && gotip test
36+
cd list && gotip test
37+
cd math && gotip test
38+
cd metrics&& gotip test
39+
cd ring && gotip test
40+
cd stack && gotip test
41+
cd strings&& gotip test
42+
cd sync/atomic && gotip test
43+
cd tree && gotip test
44+
```
45+
46+
## Upcoming Standard Package
47+
48+
See [changkun/generics](https://github.com/changkun/generics).
49+
50+
## Known Issues
51+
52+
The know issues of the current implementation:
53+
54+
- Type parameter for slices is not supported yet.
55+
- Package import is still buggy
56+
57+
These written packages are not runnable yet (will trigger some internal compiler bug):
58+
59+
```
60+
chans
61+
demo/ex5-loadbalance.go
62+
graph
63+
maps
64+
sched
65+
slices
66+
sync
67+
```
2368

2469
## Licnese
2570

chans/lb_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,5 @@ func TestLB(t *testing.T) {
4646
for i := 0; i < 10; i++ {
4747
outs[i] = make(chan int, 10)
4848
}
49-
LB(func(m int) int {
50-
return rand.Intn(m)
51-
}, ins, outs)
49+
LB(func(m int) int { return rand.Intn(m)}, ins, outs)
5250
}

constraints/string.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ type PrintableStringer[T Stringer] interface {
1515
}
1616

1717
type Sequence interface {
18-
type string, []byte
18+
~string | ~[]byte
1919
}

constraints/types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ package constraints
99
// In practice this type constraint would likely be defined in
1010
// a standard library package.
1111
type Ordered interface {
12-
type int, int8, int16, int32, int64,
13-
uint, uint8, uint16, uint32, uint64, uintptr,
14-
float32, float64,
15-
string
12+
~int | ~int8 | ~int16 | ~int32 | ~int64 |
13+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
14+
~float32 | ~float64 |
15+
~string
1616
}
1717

1818
type Integer interface {
19-
type int8, int16, int32, int64, int,
20-
uint8, uint16, uint32, uint64, uint
19+
~int8 | ~int16 | ~int32 | ~int64 | ~int |
20+
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint
2121
}
2222

2323
type Signed interface {
24-
type int, int8, int16, int32, int64
24+
~int | ~int8 | ~int16 | ~int32 | ~int64
2525
}
2626

2727
type Unsigned interface {
28-
type uint, uint8, uint16, uint32, uint64, uintptr
28+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
2929
}
3030

3131
type Adder[T any] interface {
3232
Add(T) T
3333
}
3434

3535
type FloatComplex interface {
36-
type float32, float64, complex64, complex128
36+
~float32 | ~float64 | ~complex64 | ~complex128
3737
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module golang.design/x/go2generics
22

3-
go 1.16
3+
go 1.18

history/202108.go2.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2021 Changkun Ou. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package history
6+
7+
// constrained type set
8+
9+
type Ordered interface {
10+
~int | ~int8 | ~int16 | ~int32 | ~int64 |
11+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
12+
~float32 | ~float64 | ~string
13+
}
14+
15+
// Max returns the maximum among all parameters
16+
func Max[T Ordered](v0 T, vn ...T) T {
17+
switch l := len(vn); {
18+
case l == 0:
19+
return v0
20+
case l == 1:
21+
if v0 > vn[0] { return v0 }
22+
return vn[0]
23+
default:
24+
vv := Max(vn[0], vn[1:]...)
25+
if v0 > vv { return v0 }
26+
return vv
27+
}
28+
}
29+
30+
// a more complex example:
31+
32+
type I1 [P1 any] interface {
33+
m1(x P1)
34+
}
35+
type I2 [P1, P2 any] interface {
36+
m2(x P1) P2
37+
type int, float64
38+
}
39+
40+
func F[P1 I1[P1], P2 I2[P1, P2]] (x P1, y P2) {}

linalg/linalg.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import "math"
99
// Numeric is a contract that matches any numeric type.
1010
// It would likely be in a contracts package in the standard library.
1111
type Numeric interface {
12-
type int, int8, int16, int32, int64, uint, uint8,
13-
uint16, uint32, uint64, uintptr, float32,
14-
float64, complex64, complex128
12+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 |
13+
~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 |
14+
~float64 | ~complex64 | ~complex128
1515
}
1616

1717
// NumericAbs matches numeric types with an Abs method.
@@ -22,13 +22,13 @@ type NumericAbs[T any] interface {
2222

2323
// OrderedNumeric matches numeric types that support the < operator.
2424
type OrderedNumeric interface {
25-
type int, int8, int16, int32, int64, uint, uint8,
26-
uint16, uint32, uint64, uintptr, float32, float64
25+
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 |
26+
~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
2727
}
2828

2929
// Complex matches the two complex types, which do not have a < operator.
3030
type Complex interface {
31-
type complex64, complex128
31+
~complex64 | ~complex128
3232
}
3333

3434
func DotProduct[T Numeric](s1, s2 []T) T {
@@ -44,7 +44,7 @@ func DotProduct[T Numeric](s1, s2 []T) T {
4444

4545
// AbsDifference computes the absolute value of the difference of
4646
// a and b, where the absolute value is determined by the Abs method.
47-
func AbsDifference[T NumericAbs](a, b T) T {
47+
func AbsDifference[T NumericAbs[T]](a, b T) T {
4848
d := a - b
4949
return d.Abs()
5050
}

linalg/linalg_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 Changkun Ou. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package linalg
6+
7+
import "testing"
8+
9+
func TestDotProduct(t *testing.T) {
10+
a := []int{1, 2, 3}
11+
b := []int{2, 3, 4}
12+
13+
val := DotProduct(a, b)
14+
if val != 20 {
15+
t.Fatalf("unexpected DotProduct result, got %v want %v", val, 20)
16+
}
17+
}

list/list_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2021 Changkun Ou. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package list
6+
7+
import "testing"
8+
9+
func TestList(t *testing.T) {
10+
l := List[int]{}
11+
12+
l.Push(1)
13+
l.Push(2)
14+
l.Push(3)
15+
}

maps/orderedmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type keyValue[K, V any] struct {
7070

7171
// InOrder returns an iterator that does an in-order traversal of the map.
7272
func (m *OrderedMap[K, V]) InOrder() *Iterator[K, V] {
73-
sender, receiver := chans.Ranger(keyValue[K, V])()
73+
sender, receiver := chans.Ranger[keyValue[K, V]]()
7474
var f func(*node[K, V]) bool
7575
f = func(n *node[K, V]) bool {
7676
if n == nil {

0 commit comments

Comments
 (0)