diff --git a/README.md b/README.md index c27ff04..8502bae 100644 --- a/README.md +++ b/README.md @@ -53,3 +53,57 @@ $ make compile-noinline- $ make decomp- $ make decomp-noinline- ``` + +--- + +# Extra: Per chi non sa cosa sono le interfacce leggere prima questo + +```go +type Circle struct { + Radius float64 +} +type Rectangle struct { + Width, Height float64 +} + +type Shape interface { + Area() float64 + Perimeter() float64 +} + +func (c Circle) Area() float64 { + return c.Radius * c.Radius * math.Pi +} +func (c Circle) Perimeter() float64 { + return 2 * c.Radius * math.Pi +} +func (c Circle) Curvature() float64 { + return 1 / c.Radius +} + +func (r Rectangle) Area() float64 { + return r.Width * r.Height +} +func (r Rectangle) Perimeter() float64 { + return 2 * r.Radius * math.Pi +} +func (r Rectangle) CornerCount() int { + if (r.Width == 0 && r.Height == 0) { return 0 } + if (r.Width == 0 || r.Height == 0) { return 2 } + return 4 +} + +func AreaOverPerimeter(s Shape) float64 { + return s.Area() / s.Perimeter() +} + +// c1 := Circle{ Radius: 5.0 } +c1 := Circle{ 5.0 } + +// r1 := Rectangle{ Width: 2.0, Height: 3.0 } +r1 := Rectangle{ 2.0, 3.0 } + +AreaOverPerimeter(c1) +AreaOverPerimeter(r1) +``` + diff --git a/slides.md b/slides.md index bc23b60..02d4a83 100644 --- a/slides.md +++ b/slides.md @@ -136,7 +136,7 @@ return y --- -#### Type Parameters & Type Sets +#### Type Parameters ```go import "golang.org/x/exp/constraints" @@ -191,16 +191,18 @@ func Min[T constraints.Ordered](x, y T) T { --- +#### Type Sets + ```go -type Liter float64 +type Liter32 float32 -type Meter float64 +type Meter64 float64 -type Kilogram float64 +type Kilogram64 float64 ``` ```go -func Min[T float64](x, y T) T { +func Min[T float64|float32](x, y T) T { if x < y { return x } @@ -215,30 +217,47 @@ Min(a, b) // Errore --- +#### Type Sets + ```go type Liter float64 -type Meter float64 +type Meter64 float64 -type Kilogram float64 +type Kilogram64 float64 ``` ```go -func Min[T ~float64](x, y T) T { - if x < y { - return x - } +func Min[T ~float64|~float32](x, y T) T { + if x < y { return x } return y } ``` ```go -var a, b Liter = 1, 2 +var a, b float32 = 1.0, 2.0 +Min(a, b) // Ok +var a, b float64 = 1.0, 2.0 +Min(a, b) // Ok + +var a, b Liter = 1.0, 2.0 Min(a, b) // Ok ``` --- +#### Type Sets + +```go +type Float interface { + ~float32 | ~float64 +} +``` + +--- + +#### Type Sets + ```go package constraints