I had some previous discussions on another site :) where I suggested that when passing a slice to a method, and the method expects an interface, that the actual struct parameter is marked as “this is concrete type T but treat as interface”, etc. It really only works when the slice is “read only”, but there have been similar issues when using generic arrays in Java - you need to know that the underlying array MAY be of a different concrete type, so you can’t store arbitrary structs in it (if it is read-only you don’t need to worry about this).
It would be check - although each access would have another layer of indirection - but no different than accessing a slice that contains interfaces to begin with. I think it gets handled - mostly - if generics are added - but I think a simple solution now would be good IMO... > On Oct 30, 2018, at 7:35 PM, Justin Israel <justinisr...@gmail.com> wrote: > > > > On Wed, Oct 31, 2018 at 1:32 PM robert engels <reng...@ix.netcom.com > <mailto:reng...@ix.netcom.com>> wrote: > I have argued for a runtime/built-in to do this - it is so common…. (if doing > “kind of OO” in Go) > > I would love to have the ability to do it with built-in support, but I feel > like it would go against the goals of not wanting to hide complexity. It > wouldn't be "free" to do it (as far as I know) and I doubt the Go maintainers > want it to hide the copy into the new slice. My 2 cents. > > > >> On Oct 30, 2018, at 7:30 PM, Justin Israel <justinisr...@gmail.com >> <mailto:justinisr...@gmail.com>> wrote: >> >> >> >> On Wed, Oct 31, 2018 at 11:21 AM <zloikom...@gmail.com >> <mailto:zloikom...@gmail.com>> wrote: >> Hello, everyone. >> Consider following code: >> >> package main >> >> import "fmt" >> >> type implementation struct { >> d []int >> } >> >> func (impl *implementation) getData() interface{} { >> return impl.d >> } >> >> type phase struct{} >> >> type data interface { >> getData() interface{} >> } >> >> func MakeIntDataPhase() *phase { >> return &phase{} >> } >> >> func (p *phase) run(population []data) []data { >> return nil >> } >> >> func main() { >> var population []implementation >> MyPhase := MakeIntDataPhase() >> fmt.Println(MyPhase.run(population)) >> >> } >> >> When running following code in playground I got following error: >> prog.go:30:25: cannot use population (type []implementation) as type []data >> in argument to MyPhase.run >> If I understand correctly it is because slice of interface type cannot be >> converted by the compiler to concrete type. >> >> What is correct way in golang to implement functionality that is presented >> in the example? >> When method argument defined using a slice of some interface, how I can pass >> it a slice of a concrete type that implements the interface? >> >> You would end up needing to just do >> >> func (p *phase) run(population {}interface) []data >> >> and then type assert the {}interface into []implementation >> There isn't a way to directly pass a slice of concrete type to a function >> that accepts a slice of interface, unless you first do this: >> >> iface := make([]data, len(population)) >> for i, p := range population { >> iface[i] = p >> } >> MyPhase.run(iface) >> >> Justin >> >> -- >> 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 golang-nuts+unsubscr...@googlegroups.com >> <mailto:golang-nuts+unsubscr...@googlegroups.com>. >> For more options, visit https://groups.google.com/d/optout >> <https://groups.google.com/d/optout>. >> >> -- >> 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 golang-nuts+unsubscr...@googlegroups.com >> <mailto:golang-nuts+unsubscr...@googlegroups.com>. >> For more options, visit https://groups.google.com/d/optout >> <https://groups.google.com/d/optout>. > > > -- > 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 golang-nuts+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- 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 golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.