Skip to content

Commit f246cc7

Browse files
authored
Merge pull request #888 from devlights/add-go124-generic-type-alias
2 parents 437cd39 + b637edf commit f246cc7

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/devlights/try-golang/examples/basic/floatop"
2727
"github.com/devlights/try-golang/examples/basic/formatting"
2828
"github.com/devlights/try-golang/examples/basic/functions"
29+
"github.com/devlights/try-golang/examples/basic/go124"
2930
"github.com/devlights/try-golang/examples/basic/goroutines"
3031
"github.com/devlights/try-golang/examples/basic/helloworld"
3132
"github.com/devlights/try-golang/examples/basic/hexop"
@@ -116,6 +117,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
116117
floatop.NewRegister().Regist(m)
117118
formatting.NewRegister().Regist(m)
118119
functions.NewRegister().Regist(m)
120+
go124.NewRegister().Regist(m)
119121
goroutines.NewRegister().Regist(m)
120122
helloworld.NewRegister().Regist(m)
121123
hexop.NewRegister().Regist(m)

examples/basic/go124/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
| file | example name | note |
6+
| --------------------- | ------------------------ | ------------------------------------------------------- |
7+
| generic_type_alias.go | go124_generic_type_alias | Go 1.24 で追加された Generic Type Alias のサンプルです. |

examples/basic/go124/examples.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package go124
2+
3+
import (
4+
"github.com/devlights/try-golang/mapping"
5+
)
6+
7+
type (
8+
register struct{}
9+
)
10+
11+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
12+
func NewRegister() mapping.Register {
13+
return new(register)
14+
}
15+
16+
// Regist -- 登録します.
17+
func (r *register) Regist(m mapping.ExampleMapping) {
18+
m["go124_generic_type_alias"] = GenericTypeAlias
19+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package go124
2+
3+
import "fmt"
4+
5+
type (
6+
Item[T any] struct {
7+
Value T
8+
}
9+
10+
// Generic Type Definition (完全に新しい型を作成) -- これは Go 1.24 以前から出来ていた
11+
TypeDefinition[T any] Item[T]
12+
// Generic Type Alias (既存の型に別名を付与) -- これが Go 1.24 から出来るようになった
13+
TypeAlias[T any] = Item[T]
14+
)
15+
16+
func newTypeDefinition[T any](v T) *TypeDefinition[T] {
17+
return &TypeDefinition[T]{Value: v}
18+
}
19+
20+
func newTypeAlias[T any](v T) *TypeAlias[T] {
21+
return &TypeAlias[T]{Value: v}
22+
}
23+
24+
// Type Definition は、完全に新しい型を作成するので、独自メソッドを追加可能
25+
func (me *TypeDefinition[T]) String() string {
26+
return fmt.Sprintf("(%v:%p)", me.Value, &me.Value)
27+
}
28+
29+
// Type Alias は、元の型の別名なので、独自メソッドの追加不可能
30+
// (cannot define new methods on generic alias type TypeAlias[T any]compiler (InvalidRecv))
31+
//
32+
// func (me *TypeAlias[T]) String() string {
33+
// return fmt.Sprintf("(%v:%p)", me.Value, &me)
34+
// }
35+
36+
// GenericTypeAlias は、Go 1.24 で追加された Generic Type Alias のサンプルです.
37+
//
38+
// GenericなType Definitionは、Go 1.24以前でも可能でしたが
39+
// Go 1.24にて、GenericなType Aliasも可能となりました。([Go 1.24 Release note])
40+
//
41+
// - Type Definition は、完全に新たな型を作成する機能です。元の型との互換性はありません。
42+
// - Type Alias は、元の型の別名を作成する機能です。元の型と互換性があります。
43+
//
44+
// コードでは、以下のようになります。
45+
//
46+
// type ID int // Type Definition
47+
// type ID = int // Type Alias
48+
//
49+
// [Go 1.24 Release note]: https://tip.golang.org/doc/go1.24
50+
func GenericTypeAlias() error {
51+
var (
52+
td = newTypeDefinition(100)
53+
ta = newTypeAlias(200)
54+
55+
item *Item[int]
56+
)
57+
58+
// Type Definitionは元の型との互換性が無いためコンパイルエラーとなる
59+
// (cannot use td (variable of type *TypeDefinition[int]) as *Item[int] value in assignment compiler (IncompatibleAssign))
60+
//
61+
//item = td
62+
63+
// Type Aliasは、元の型の別名であるため互換性がある
64+
item = ta
65+
66+
fmt.Printf("%s\t(%v:%p)\t(%v:%p)\n", td, ta.Value, &ta.Value, item.Value, &item.Value)
67+
68+
return nil
69+
70+
/*
71+
$ task
72+
task: [build] go build -o "/workspace/try-golang/try-golang" .
73+
task: [run] ./try-golang -onetime
74+
75+
ENTER EXAMPLE NAME: go124_generic_type_alias
76+
77+
[Name] "go124_generic_type_alias"
78+
(100:0xc000012a70) (200:0xc000012a78) (200:0xc000012a78)
79+
80+
[Elapsed] 19.6µs
81+
*/
82+
}

0 commit comments

Comments
 (0)