diff --git a/examples/generics/generics.go b/examples/generics/generics.go index c6fa91afd..6cec12672 100644 --- a/examples/generics/generics.go +++ b/examples/generics/generics.go @@ -5,6 +5,36 @@ package main import "fmt" +type MyInt16 int16 +type MyInt int + +func (m MyInt16) String() string { + return fmt.Sprintf("{MyInt16: %d}", m) +} + +func (m MyInt) String() string { + return fmt.Sprintf("{MyInt: %d}", m) +} + +// `~` the underlying type of T must be itself, and T cannot be an interface. +type Number interface { + int | ~int16 +} + +// An interface representing all types with underlying type int that implement the String method. +type IntString interface { + ~int16 + String() string +} + +func SumNumber[T Number](a, b T) T { + return a + b +} + +func SumIntString[T IntString](a, b T) (T, string) { + return a + b, a.String() + ", " + b.String() +} + // As an example of a generic function, `MapKeys` takes // a map of any type and returns a slice of its keys. // This function has two type parameters - `K` and `V`; @@ -71,4 +101,13 @@ func main() { lst.Push(13) lst.Push(23) fmt.Println("list:", lst.GetAll()) + + // We can use `MyInt16` as the parameter of the function `SumNumber`, because its underlying type is int16. + fmt.Println(SumNumber(MyInt16(1), MyInt16(2))) + fmt.Println(SumNumber(1, 2)) + + // We can't use `MyInt` as the parameter of the function `SumInString`, because underlying type of `MyInt` is int not int16. + // also int16 does not implement `IntString` (missing method String). + result, str := SumIntString(MyInt16(1), MyInt16(2)) + fmt.Printf("result: %d, output: %s\n", result, str) } diff --git a/examples/generics/generics.hash b/examples/generics/generics.hash index 2b23af56f..1ee314d59 100644 --- a/examples/generics/generics.hash +++ b/examples/generics/generics.hash @@ -1,2 +1,2 @@ -91465956a90881ec8b4cca3968b9aa1f6d9f1447 -uXlb-AyeYmQ +58327cbd52a273bbf23034c16cc679e7dbffdd77 +hw05lSyEz41 diff --git a/examples/generics/generics.sh b/examples/generics/generics.sh index 4aa2d0729..7982c43d8 100644 --- a/examples/generics/generics.sh +++ b/examples/generics/generics.sh @@ -1,3 +1,6 @@ $ go run generics.go keys: [4 1 2] list: [10 13 23] +{MyInt16: 3} +3 +result: 3, output: {MyInt16: 1}, {MyInt16: 2} diff --git a/public/generics b/public/generics index adee89770..dd83f086e 100644 --- a/public/generics +++ b/public/generics @@ -42,7 +42,7 @@ - +
package main
@@ -57,6 +57,92 @@ + + + + + + +
type MyInt16 int16
+type MyInt int
+ + + + + + + + + +
func (m MyInt16) String() string {
+    return fmt.Sprintf("{MyInt16: %d}", m)
+}
+ + + + + + + + + +
func (m MyInt) String() string {
+    return fmt.Sprintf("{MyInt: %d}", m)
+}
+ + + + + +

~ the underlying type of T must be itself, and T cannot be an interface.

+ + + + +
type Number interface {
+    int | ~int16
+}
+ + + + + +

An interface representing all types with underlying type int that implement the String method.

+ + + + +
type IntString interface {
+    ~int16
+    String() string
+}
+ + + + + + + + + +
func SumNumber[T Number](a, b T) T {
+    return a + b
+}
+ + + + + + + + + +
func SumIntString[T IntString](a, b T) (T, string) {
+    return a + b, a.String() + ", " + b.String()
+}
+ + +

As an example of a generic function, MapKeys takes @@ -186,13 +272,38 @@ automatically.

- +
    lst := List[int]{}
     lst.Push(10)
     lst.Push(13)
     lst.Push(23)
-    fmt.Println("list:", lst.GetAll())
+    fmt.Println("list:", lst.GetAll())
+ + + + + +

We can use MyInt16 as the parameter of the function SumNumber, because its underlying type is int16.

+ + + + +
    fmt.Println(SumNumber(MyInt16(1), MyInt16(2)))
+    fmt.Println(SumNumber(1, 2))
+ + + + + +

We can’t use MyInt as the parameter of the function SumInString, because underlying type of MyInt is int not int16. +also int16 does not implement IntString (missing method String).

+ + + + +
    result, str := SumIntString(MyInt16(1), MyInt16(2))
+    fmt.Printf("result: %d, output: %s\n", result, str)
 }
@@ -209,7 +320,10 @@ automatically.

$ go run generics.go
 keys: [4 1 2]
-list: [10 13 23]
+list: [10 13 23] +{MyInt16: 3} +3 +result: 3, output: {MyInt16: 1}, {MyInt16: 2} @@ -228,7 +342,7 @@ automatically.