During a refactoring, I’ve managed to extract an interface that applies on 20 concrete types:
type AbstractItem interface {
GetId() string
SetId(newId string)
GetKind() string
GetName() *string
SetName(newName string)
}
However, my concrete types have also some methods that reference an instance of themselves.
// Reset some attributes
func (car *Car) Reset() *Car {
otherCar := &Car{}
otherCar.odometer = 0
return car
}
func (lamp *Lamp) Reset() *Lamp {
otherLamp := &Lamp{}
otherLamp.lampOn = false
return otherLamp
}
So I’ve attempted to add to my AbstractItem
interface:
Reset() AbstractItem
But then, it doesn’t override anything anymore.
Is this something that can be solved by the use of generics?
I haven’t tried them yet, are they made for that use: replacing a type, even inside an interface?