[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{returnx}
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{returnx}
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 {
...
```
<!-- Le interfacce possono introdurre questo type-set per limitare i tipi a cui vengono applicate, l'unico tipo che non possiamo utilizzare nei type-sets sono le interfacce con metodi. -->
---
<stylescoped>
code { font-size: 150% }
</style>
## Tipi Generici
```go
type Stack[T interface{}] []T
```
<!-- _class: chapter -->
<!-- In realtà non serve usare "interface{}" tutte le volte -->
# Tipi Generici
---
<stylescoped>
code { font-size: 150% }
code { font-size: 120% }
</style>
## 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 {
<!-- _class: chapter -->
# 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"
---
<stylescoped>
section {
font-size: 140%;
@ -458,7 +468,7 @@ Esempio notevole: <https://github.com/zyedidia/generic> (1K:star: su GitHub)
<!-- _class: chapter -->
# Anti-Pattern (1)
# Anti-Pattern 1
Utility HTTP
---
@ -549,7 +559,7 @@ Quindi nella maggior parte dei casi se ci ritroviamo a scrivere una funzione gen
<!-- _class: chapter -->
# Anti-Pattern (2)
# Anti-Pattern 2
Generics vs Interfacce
---
@ -639,60 +649,6 @@ d := &bytes.Buffer{} /* (*bytes.Buffer) */