Any reason why this type conversion is not allowed?

105 views
Skip to first unread message

Glen Huang

unread,
Apr 13, 2020, 9:55:49 PM4/13/20
to golang-nuts
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?

Marcin Romaszewicz

unread,
Apr 13, 2020, 10:33:43 PM4/13/20
to Glen Huang, golang-nuts
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.


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/26c4bcfc-f67b-4638-a812-dce9c8c9b275%40googlegroups.com.

Glen Huang

unread,
Apr 13, 2020, 10:46:21 PM4/13/20
to golang-nuts
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 <hey...@gmail.com> 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 golan...@googlegroups.com.

Personal

unread,
Apr 13, 2020, 11:56:51 PM4/13/20
to Glen Huang, golang-nuts

> On 14 Apr 2020, at 09.46, Glen Huang <hey...@gmail.com> wrote:
>
> 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

I think the Go specification can help to provide explanation:

https://golang.org/ref/spec#Properties_of_types_and_values

Ian Lance Taylor

unread,
Apr 14, 2020, 1:07:36 AM4/14/20
to Glen Huang, golang-nuts
On Mon, Apr 13, 2020 at 6:56 PM Glen Huang <hey...@gmail.com> wrote:
>
https://golang.org/doc/faq#convert_slice_with_same_underlying_type

Ian

Glen Huang

unread,
Apr 14, 2020, 2:37:21 AM4/14/20
to golang-nuts
@lan @Shulhan

Thanks. Glad to know the design goal much better.
Reply all
Reply to author
Forward
0 new messages