Hi,

I am trying to write a function that initializes a generic collection. For 
adding new items to the collection, the user specifies a constructor which 
takes one argument and returns a pointer to the struct that will be 
inserted in the collection, and also sets fields to initial values. For 
example:

func Constructor(id int) *NewItemX ...
func Constructor(id int) *NewItemY ...
func Constructor(id int) *NewItemZ ...

In the constructor for the collection, I want to accept any function which 
has this general form, although the return type will be different for each 
struct. At first, I thought this might work:

func NewCollection(itemCtr func(int) interface{}) *Collection ...

but of course, trying to pass Constructor fails during compilation since 
the types *NewItemX and interface{} do not match:

.\Collection_test.go:xx: cannot use NewItemX (type func(int) *NewItemX) as 
type func(int) interface {} in argument to NewCollection

I could just do this:

func NewCollection(itemCtr interface{}) *Collection

but then I would have to do some runtime checks using reflect to make sure 
that it is a func etc and I lose compile-time checking of types.

How can I express this best in go?

-- 
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.

Reply via email to