Currently interfaces may only contain method signatures. So, if I want
an interface to specify that an object has a method, M() _and_ a field
F, I must do this (untested):
type I interface {
M()
F() int
SetF(int)
}
type T struct {
f int
g string
}
func (t T) M() { fmt.Println(t.f, t.g) }
funt (t T) F() int { return t.f }
funt (t* T) SetF(f int) { t.f = f }
This forces me to create a getter and setter for every single type that
implements I.
But if interfaces could also specify fields then the above would
simplify to:
type I interface {
M()
F int
}
type T struct {
F int
g string
}
func (t T) M() { fmt.Println(t.f, t.g) }
This doesn't require any getters or setters for types that implement I.
I realize that this wouldn't appeal to OO purists, but I think it would
be useful and reduce code size (well, it would for one of my projects).
But maybe this has been thought of before and found wanting?
--
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
C++, Python, Qt, PyQt - training and consultancy
"Programming in Go" - ISBN 0321774639
http://www.qtrac.eu/gobook.html
But maybe this has been thought of before and found wanting?