With all due respect, I think assigning to interface{} is orthogonal to the
question, which deals with variance. (I asked about it
here: https://groups.google.com/forum/#!topic/golang-nuts/S4p54OpQHGw)
Here, the type is redefined, so in the context of conversion, it and the
original should be equivalent.
A tangent question, why this also isn't allowed:
type Data []byte
type ContainerRaw struct {
Data []byte
}
type Container struct {
Data Data
}
c := Container{[]byte{1}}
ContainerRaw(c) // not allowed, but if Container. Data is of type []byte,
then it's allowed
On Tuesday, April 14, 2020 at 10:33:43 AM UTC+8, Marcin Romaszewicz wrote:
>
> Go doesn't do any implicit type conversions, and it's quite consistent
> about that. The only things which are type "converted" are untyped
> constants.
>
> I would love for this to work too, by the way, since I often come up with
> something like:
>
> var a []interface{}
> var b []SomethingConcrete
>
> I would love to be able to assign a to b without having to loop, but it
> won't work because their sizes are completely different, so the compiler
> can't emit a single conversion of this type, since one structure isn't
> safely coercible to the other type. This would be along the lines of
> reinterpret_cast<> in c++, which can corrupt memory.
>
>
> On Mon, Apr 13, 2020 at 6:56 PM Glen Huang <[email protected] <javascript:>>
> wrote:
>
>> Given
>>
>> type Data []byte
>> s := [][]byte{{1},{2}}
>>
>> I wonder why this conversion isn't allowed?
>>
>> []Data(s)
>>
>> Seems pretty straightforward. Maybe I miss some edge cases where things
>> won't square?
>>
>> --
>> 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] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/26c4bcfc-f67b-4638-a812-dce9c8c9b275%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/golang-nuts/26c4bcfc-f67b-4638-a812-dce9c8c9b275%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/4f884941-ff5b-489b-a41e-bfeae5d49a73%40googlegroups.com.