
For some reason, these aren't lowered when a generic function is instantiated by the SSA package. I've left unsafe.Offsetof to be implemented later, it's a bit difficult to do correctly the way the code is currently structured.
30 строки
389 Б
Go
30 строки
389 Б
Go
package main
|
|
|
|
import "unsafe"
|
|
|
|
type Coord interface {
|
|
int | float32
|
|
}
|
|
|
|
type Point[T Coord] struct {
|
|
X, Y T
|
|
}
|
|
|
|
func Add[T Coord](a, b Point[T]) Point[T] {
|
|
checkSize(unsafe.Alignof(a))
|
|
checkSize(unsafe.Sizeof(a))
|
|
return Point[T]{
|
|
X: a.X + b.X,
|
|
Y: a.Y + b.Y,
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var af, bf Point[float32]
|
|
Add(af, bf)
|
|
|
|
var ai, bi Point[int]
|
|
Add(ai, bi)
|
|
}
|
|
|
|
func checkSize(uintptr)
|