I am trying to use an embedded type that implements an interface, in order
to provide some functional options to a group of related types.
However, I am receiving an error that I cannot use the embedded type to
call the option function.
Here is a contrived example. A type "Waiter" implements the "TableServer"
interface using the type "tableserver".
The error is "cannot use SetTable(9) (type func(*tableServer)) as type
func(*Waiter) in argument to NewWaiter"
package main
import "fmt"
type tableServer struct {
table int
}
type TableServer interface {
SetTable(table int)
GetTable() int
}
func NewTableServer() TableServer {
return &tableServer{}
}
func (i *tableServer) SetTable(table int) {
i.table = table
}
func (i *tableServer) GetTable() int {
return i.table
}
func SetTable(table int) func(*tableServer) {
return func(i *tableServer) {
i.SetTable(table)
}
}
type Waiter struct {
Name string
TableServer
}
func NewWaiter(name string, options ...func(*Waiter)) *Waiter {
w := &Waiter{
Name: name,
TableServer: NewTableServer(),
}
for _, option := range options {
option(w)
}
return w
}
func main() {
waiter := NewWaiter("joe")
fmt.Println(waiter.Name, "is working table", waiter.GetTable())
// the following line cannot compile, with error:
// ./opts.go:56: cannot use SetTable(9) (type func(*tableServer)) as
type func(*Waiter) in argument to NewWaiter
waiter = NewWaiter("bob", SetTable(9))
fmt.Println(waiter.Name, "is working table", waiter.GetTable())
}
Any suggestions? Thanks in advance!
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.