On Wed, Nov 8, 2017 at 1:39 PM, Haiyu Zhen <zhenhaiyuce...@gmail.com> wrote:
>
> As someone who is new to Golang world, I am confused with the following
> code.
>
> As sample code shown, type S's "underlying" type is slice (in C++ jargon S
> is the derived class of slice), and if I pass a slice as S in function
> parameter, it works; but if I pass slice as S in function receiver, Go
> doesn't "downcast" it to S.
> I do know that S and []int are treated as different types in Go, but as the
> append function shows, Go will "upcast" S to slice to use slice.append().
>
> So when should I expect the type casting to work? Or or specific to the
> scenario, why it fails if I use slice as the function receiver for S?
>
>>
>> package main
>> import (
>> "fmt"
>> )
>> type S []int
>>
>> func (s S) testUnderlyingTypeAsReciever() {
>> fmt.Println("Hello, playground")
>> }
>> func testUnderlyingTypeAsParam(s S) {
>> fmt.Println("Hello, playground")
>> }
>> func main() {
>> d := []int{2}
>> s := S{}
>> s = append(s, 1) // use append function for slice
>> testUnderlyingTypeAsParam(d) // pass
>> d.testUnderlyingTypeAsReciever() // fail
>> }

When thinking about Go it's best to avoid concepts that do not apply,
like derived class, upcast, and downcast.  You can't write
d.testUnderlyingTypeAsReceiver() because the method is only defined on
the type S, and d is type []int, not type S.  Passing d to
testUnderlyingTypeAsParam works because a value of an unnamed type is
assignable to a value of a named type when the underlying type of the
named type is the same as the unnamed type.  There is no upcasting or
downcasting involved.  There is just simple assignment.

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

Reply via email to