FAQ
I've noticed some unexpected behavior with reflection when applied to
slices and was wondering whether this is expected or a bug:

type A interface{}
type B struct{}

data := []A{B{}}
sliceval := reflect.ValueOf(data)
elemval := sliceval.Index(0)
altval := reflect.ValueOf(data[0])
fmt.Println(elemval.Type()) // prints "main.A"
fmt.Println(altval.Type()) // prints "main.B"

Runnable: http://play.golang.org/

I was expecting both lines to print main.B, since in both cases the value
refers to the same element of the same slice, and in both cases the runtime
type of that object is main.B. Is this a bug or a feature?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Oct 5, 2015 at 10:36 pm

    On Mon, Oct 5, 2015 at 11:16 AM, Alex Flint wrote:
    I've noticed some unexpected behavior with reflection when applied to slices
    and was wondering whether this is expected or a bug:

    type A interface{}
    type B struct{}

    data := []A{B{}}
    sliceval := reflect.ValueOf(data)
    elemval := sliceval.Index(0)
    altval := reflect.ValueOf(data[0])
    fmt.Println(elemval.Type()) // prints "main.A"
    fmt.Println(altval.Type()) // prints "main.B"

    Runnable: http://play.golang.org/

    I was expecting both lines to print main.B, since in both cases the value
    refers to the same element of the same slice, and in both cases the runtime
    type of that object is main.B. Is this a bug or a feature?
    It's a feature. It's because you can use Index(0).Set(someValue).
    That means that Index(0) has to be the slice element type, even if
    that element type is an interface type. You would get a similar
    result by doing ValueOf(&data[0]).Elem().

    Ian

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Alex Flint at Oct 5, 2015 at 11:04 pm
    Ah, thanks!
    On Mon, Oct 5, 2015 at 3:36 PM Ian Lance Taylor wrote:
    On Mon, Oct 5, 2015 at 11:16 AM, Alex Flint wrote:
    I've noticed some unexpected behavior with reflection when applied to slices
    and was wondering whether this is expected or a bug:

    type A interface{}
    type B struct{}

    data := []A{B{}}
    sliceval := reflect.ValueOf(data)
    elemval := sliceval.Index(0)
    altval := reflect.ValueOf(data[0])
    fmt.Println(elemval.Type()) // prints "main.A"
    fmt.Println(altval.Type()) // prints "main.B"

    Runnable: http://play.golang.org/

    I was expecting both lines to print main.B, since in both cases the value
    refers to the same element of the same slice, and in both cases the runtime
    type of that object is main.B. Is this a bug or a feature?
    It's a feature. It's because you can use Index(0).Set(someValue).
    That means that Index(0) has to be the slice element type, even if
    that element type is an interface type. You would get a similar
    result by doing ValueOf(&data[0]).Elem().

    Ian
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 5, '15 at 6:17p
activeOct 5, '15 at 11:04p
posts3
users2
websitegolang.org

2 users in discussion

Alex Flint: 2 posts Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2021 Grokbase