I'm trying to represent a human wearing glasses near a window when a nearby explosion occurs. main
is a sketch of what should be done during the explosion. Something should gather up a list of objects in proximity of the explosion and do specific things (such as shattering or melting) for each of them. The glass and window shatter as expected, but for some reason the human also shatters. Why?
package main
import "fmt"
type Human struct { Glasses }
type Glasses struct {}
type Shatterable interface { shatter() }
func (g Glasses) shatter() {}
type Window struct {}
func (w Window) shatter() {}
func main() {
h := Human{Glasses{}}
objectsInProximity := []interface{}{h,h.Glasses,Window{}}
for _,o := range objectsInProximity {
shatter(o)
}
}
func shatter(i interface{}) {
s, ok := i.(Shatterable)
if ok {
fmt.Printf("shattering a %T
", s)
s.shatter()
}
}
$ go run a.go
shattering a main.Human
shattering a main.Glasses
shattering a main.Window
As mentioned in this thread:
We're talking about anonymous fields of a struct (http://golang.org/ref/spec#Struct_types).
(modified version, as the term superset is confusing)
A struct with an anonymous field satisfies every interface with all the interface methods declared by the anonymous field or the struct itself.
type Human struct { Glasses }
Since Glasses
can be shattered, Human
also satisfy the same interface.
Note: embedding anonymous field in a struct is the closest to "inheritance", even though the answer to "Golang: what's the point of interfaces when you have multiple inheritance" reminds us that:
Go doesn't have inheritance.
IfMan
'extended'Human
(by having it as an anonymous field), any method that usedHuman
as an argument, would not be able to takeMan
as an argument.
(That means here Human
extends... Glasses
?! That might show some kind of design imperfection)
I explained before in "If struct A
is embedded in B
, can methods on A
access method and fields of B
?" that this isn't true sub-typing.
Interfaces enable functions to have a 'placeholder' parameter which can take different structs as an argument.
Here, if Human
isn't supposed to be shattered, it shouldn't include the anonymous field Glasses
.