Convert struct with anonymous field to that type

5,941 views
Skip to first unread message

steveh

unread,
Apr 11, 2012, 4:59:44 AM4/11/12
to golan...@googlegroups.com
I have a struct type which contains an initial anonymous field and then some additional fields but it seems to be impossible to convert from the wrapper type to that of the anonymous field which seems strange did I miss something?

Example:-
type InnerStructType struct {
    str1 string
    int1 int
}

type Wrapper struct {
     InnerStructType
     flag1 bool
     flag2 bool
}

func test() {
    var convertTest InnerStructType
    wrapper := Wrapper{}
    convertTest = InnerStructType(wrapper)
}

cannot convert wrapper (type Wrapper) to type InnerStructType

Rob 'Commander' Pike

unread,
Apr 11, 2012, 5:22:54 AM4/11/12
to steveh, golan...@googlegroups.com
You can't convert it - a quart doesn't fit in a pint pot. You want to
access it, just like a regular field. Its name is that of the type:

convertTest = wrapper.InnerStructType

-rob

chris dollin

unread,
Apr 11, 2012, 5:30:33 AM4/11/12
to steveh, golan...@googlegroups.com
On 11 April 2012 09:59, steveh <steven....@multiplay.co.uk> wrote:
> I have a struct type which contains an initial anonymous field and then some
> additional fields but it seems to be impossible to convert from the wrapper
> type to that of the anonymous field which seems strange did I miss
> something?

You can't convert, but you can get at the embedded field by its name
(which, despite being anonynous, it does have):

http://golang.org/ref/spec#Struct_types

"An embedded type must be specified as a type name T or as a pointer
to a non-interface type name *T, and T itself may not be a pointer type.
The unqualified type name acts as the field name."

Chris

--
Chris "allusive" Dollin

steveh

unread,
Apr 11, 2012, 12:47:09 PM4/11/12
to golan...@googlegroups.com
Thanks guys confusion comes from the fact that Wrapper in java and other languages would be a super class of InnerStructType and hence it would be convertible. Using the direct accessor is an acceptable work around but does require quite a bit more work when for example you have an array of Wrapper's and you want to return an array of InnerStructType's a full range scan is needed to build a new array instead of just a cast. I don't suppose there's any avoiding that in go is there?

Matt Kane's Brain

unread,
Apr 11, 2012, 12:52:44 PM4/11/12
to steveh, golan...@googlegroups.com
On Wed, Apr 11, 2012 at 12:47, steveh <steven....@multiplay.co.uk> wrote:
> I don't suppose there's any avoiding that in go is there?

[]*InnerStructType

--
matt kane's brain
http://hydrogenproject.com

Kyle Lemons

unread,
Apr 11, 2012, 1:04:12 PM4/11/12
to steveh, golan...@googlegroups.com
On Wed, Apr 11, 2012 at 9:47 AM, steveh <steven....@multiplay.co.uk> wrote:
Thanks guys confusion comes from the fact that Wrapper in java and other languages would be a super class of InnerStructType and hence it would be convertible. Using the direct accessor is an acceptable work around but does require quite a bit more work when for example you have an array of Wrapper's and you want to return an array of InnerStructType's a full range scan is needed to build a new array instead of just a cast. I don't suppose there's any avoiding that in go is there?

Thinking about Go in terms of inheritance or any sort of hierarchy will only get you into design trouble.  Think about actors, behaviors, interfaces, and composing them.

steveh

unread,
Apr 11, 2012, 1:07:17 PM4/11/12
to golan...@googlegroups.com, steveh
That will just fail for the same reason as the original, unless I'm miss-understanding?

To be clear we have something like []*Wrapper and we want to take that and return a []*InnerStructType atm the only way to do that that I've found is:-
inners := make([]*InnerStructType, len(wrappers))
for _, wrapper := range wrappers {
  inners = append(inners, wrapper.InnerStructType)
}

On Wednesday, April 11, 2012 5:52:44 PM UTC+1, mkb wrote:

Steven Blenkinsop

unread,
Apr 11, 2012, 1:07:09 PM4/11/12
to steveh, golan...@googlegroups.com
On Wed, Apr 11, 2012 at 12:47 PM, steveh <steven....@multiplay.co.uk> wrote:
Thanks guys confusion comes from the fact that Wrapper in java and other languages would be a super class of InnerStructType and hence it would be convertible. Using the direct accessor is an acceptable work around but does require quite a bit more work when for example you have an array of Wrapper's and you want to return an array of InnerStructType's a full range scan is needed to build a new array instead of just a cast. I don't suppose there's any avoiding that in go is there?

It won't work and it shouldn't work. For one thing, you'd need a []*Wrapper in order to be analogous to Java. Second, your []*InnerStructType will have to copy the underlying array, because otherwise, you could put *InnerStructType elements in the original []*Wrapper, which would undeniably be broken. The thing is, most[1] slice conversions don't actually copy the underlying array, so allowing such a conversion would create inconsistent semantics and hidden overhead.

[1] The only ones that do are the string related conversions, but those are a very distinct special case, since string is a bulit-in type.
Reply all
Reply to author
Forward
0 new messages