It is highly likely that when go2 comes out, with generics, it will be 
trivial to write a ToInterfaceSlice() function that takes a slice of any 
type T and returns a []interface{}. 

On Tuesday, October 30, 2018 at 8:35:39 PM UTC-4, Justin Israel wrote:
>
>
>
> On Wed, Oct 31, 2018 at 1:32 PM robert engels <ren...@ix.netcom.com 
> <javascript:>> 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 <justin...@gmail.com 
>> <javascript:>> wrote:
>>
>>
>>
>> On Wed, Oct 31, 2018 at 11:21 AM <zloik...@gmail.com <javascript:>> 
>> 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...@googlegroups.com <javascript:>.
>>> For more options, visit 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...@googlegroups.com <javascript:>.
>> For more options, visit 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.

Reply via email to