diff --git a/assets/method-sets.png b/assets/method-sets.png new file mode 100644 index 0000000..e45a370 Binary files /dev/null and b/assets/method-sets.png differ diff --git a/assets/type-sets-2.png b/assets/type-sets-2.png new file mode 100644 index 0000000..35e2c0b Binary files /dev/null and b/assets/type-sets-2.png differ diff --git a/assets/type-sets.png b/assets/type-sets.png new file mode 100644 index 0000000..c264a24 Binary files /dev/null and b/assets/type-sets.png differ diff --git a/examples/min/main.go b/examples/min/main.go index fd0d394..dddb20a 100644 --- a/examples/min/main.go +++ b/examples/min/main.go @@ -58,6 +58,13 @@ func MinFloat64(x, y float64) float64 { // with go generics +func MinFloat[T float64 | float32](x, y T) T { + if x < y { + return x + } + return y +} + func Min[T constraints.Ordered](x, y T) T { if x < y { return x diff --git a/slides.md b/slides.md index f8c0710..4560d5c 100644 --- a/slides.md +++ b/slides.md @@ -132,10 +132,18 @@ return y --- -## La Soluzione +## Soluzioni Pre-Generics + +- Fare una funzione che prende `any` e mettere tanti switch + +- Utilizzare `go generate` [...] + +- Copia incollare tante volte la funzione per ogni tipo --- +## Soluzione Post-Generics + #### Type Parameters ```go @@ -152,9 +160,7 @@ func Min[T constraints.Ordered](x, y T) T { ```go var a, b int = 0, 1 Min[int](a, b) -``` - -```go +... var a, b float32 = 3.14, 2.71 Min[float32](a, b) ``` @@ -166,9 +172,7 @@ Min[float32](a, b) ```go var a, b int = 0, 1 Min(a, b) -``` - -```go +... var a, b float32 = 3.14, 2.71 Min(a, b) ``` @@ -179,40 +183,49 @@ Min(a, b) code { font-size: 150% } -```go -func Min[T constraints.Ordered](x, y T) T { - if x < y { - return x - } - - return y -} +``` +[T Vincolo1, R interface{ Method(), ... }, ...] ``` --- -#### Type Sets + -```go -type Liter32 float32 +## Type Sets -type Meter64 float64 + -type Kilogram64 float64 -``` +  -```go -func Min[T float64|float32](x, y T) T { - if x < y { - return x - } - return y -} -``` +--- + + + +## Type Sets + + + +  + + +--- + + + +## Type Sets + + + +  + +--- + +#### Type Sets (Sintassi) ```go -var a, b Liter32 = 1, 2 -Min(a, b) // Errore +[T interface{}] ~> [T any] + +[T interface{ int | float32 }] ~> [T int | float32] ``` --- @@ -220,30 +233,48 @@ Min(a, b) // Errore #### Type Sets ```go -type Liter32 float32 +func SumTwoIntegers[T int](x, y int) T { + if x < y { return x } + return y +} +``` + +```go +type Liter int +``` -type Meter64 float64 +```go +var a, b int = 1, 2 +SumTwoIntegers(a, b) // Ok -type Kilogram64 float64 +var a, b Liter = 1, 2 +SumTwoIntegers(a, b) // Errore ``` +--- + +#### Type Sets + ```go -func Min[T ~float64|~float32](x, y T) T { +func SumTwoIntegers[T ~int](x, y int) T { if x < y { return x } return y } ``` ```go -var a, b float32 = 1.0, 2.0 -Min(a, b) // Ok -var a, b float64 = 1.0, 2.0 -Min(a, b) // Ok +type Liter int +``` + +```go +var a, b int = 1, 2 +SumTwoIntegers(a, b) // Ok -var a, b Liter32 = 1.0, 2.0 -Min(a, b) // Ok +var a, b Liter = 1, 2 +SumTwoIntegers(a, b) // Ok ``` + --- #### Type Sets @@ -292,36 +323,22 @@ type Unsigned interface { ... ``` - - --- - - -## Tipi Generici - -```go -type Stack[T interface{}] []T -``` + - +# Tipi Generici --- -## Tipi Generici - ```go type Stack[T any] []T ``` ---- - ```go func (s *Stack[T]) Push(value T) { *s = append(*s, value) @@ -373,12 +390,11 @@ func Zero[T any]() T { -# Pattern (1) -Quando usare le generics? +# Pattern: Tipi Contenitore --- -### Tipi "Contenitore" +### Tipi generici nativi - `[n]T` @@ -398,12 +414,6 @@ Quando usare le generics? --- -In Go sono sempre esistite queste strutture dati "generiche" - -Solo che prima delle generics non era possibile definire algoritmi generali per questi tipi di container, ora invece possiamo ed infatti alcune di questi sono "già in prova" - ---- -