Paradiesstaub
Paradiesstaub

Reputation: 2660

downcast higher-type to lower

Is there beside copying the inner Box value by hand a language feature to down-cast RatedBox into a Box?

type Box struct {
    Name string
}

type RatedBox struct {
    Box
    Points int
}

func main() {
    rated := RatedBox{Box: Box{Name: "foo"}, Points: 10}

    box := Box(rated) // does not work
}

go-playground

// works, but is quite verbose for structs with more members
box := Box{Name: rated.Name}

Upvotes: 4

Views: 4729

Answers (1)

icza
icza

Reputation: 417642

Embedding a type in a struct adds a field to the struct, and you can use the unqualified type name to refer to it (unqualified means omit the package name and the optional pointer sign).

For example:

box := rated.Box
fmt.Printf("%T %+v", box, box)

Output (try it on the Go Playground):

main.Box {Name:foo}

Note that assignment copies the value, so the box local variable will hold a copy of the value of the RatedBox.Box field. If you want them to be the "same" (to point to the same Box value), use a pointer, e.g.:

box := &rated.Box
fmt.Printf("%T %+v", box, box)

But here of course type of box will be *Box.

Or you may choose to embed the pointer type:

type RatedBox struct {
    *Box
    Points int
}

And then (try it on the Go Playground):

rated := RatedBox{Box: &Box{Name: "foo"}, Points: 10}

box := rated.Box
fmt.Printf("%T %+v", box, box)

Output of the last 2:

*main.Box &{Name:foo}

Upvotes: 9

Related Questions