Go version: 1.18
Here's a silly example that is not particularly useful. I am using this as an exercise to learn generics.
I have a Pokemon interface
type Pokemon interface {
ReceiveDamage(float64)
InflictDamage(Pokemon)
}
and Charmander with type parameter that implements the Pokemon interface.
type Float interface {
float32 | float64
}
type Charmander[F Float] struct {
Health F
AttackPower F
}
I want to use Charmander's attack power to inflict damage.
func (c *Charmander[float64]) ReceiveDamage(damage float64) {
c.Health -= damage
}
func (c *Charmander[float64]) InflictDamage(other Pokemon) {
other.ReceiveDamage(c.AttackPower)
}
My compiler gives error
cannot use c.AttackPower (variable of type float64 constrained by Float) as float64 value in argument to other.ReceiveDamage compiler(IncompatibleAssign)
I already instantiated the struct generic as *Charmander[float64]. I'd expect that the compiler knows AttackPower is a float64.
When I pass a float64 into a function that expects float64, why should it complain? On the other hand, ReceiveDamage does not complain. I am subtracting a float64 from Health which is a constrained type.