On Sat, Feb 5, 2022 at 4:27 PM Kamil Ziemian <kziemian...@gmail.com> wrote:
>
> On the other hand this code compiled
> > package main
> >
> > import "fmt"
> >
> > type Stringer interface {
> >         String() string
> > }
> >
> > type StringableVector[T Stringer] []T
> >
> > type someFloat float64
> >
> > func (sF someFloat) String() string {
> >         return fmt.Sprintf("someFloat: %v", float64(sF))
> > }
> >
> > func main() {
> >         var varStringer Stringer = someFloat(7)
> >         sliceSomeFloat := make([]someFloat, 3)
> >
> >         var varStringableVector StringableVector[someFloat] = sliceSomeFloat
> >
> >         fmt.Printf("varStringer type: %T\n", varStringer)
> >         fmt.Printf("sliceSomeFloat type: %T\n", sliceSomeFloat)
> >         fmt.Printf("varStringableVector type: %T\n", varStringableVector)
> > }
>
> and produced result
>
> > stringerVar type: main.someFloat
> > sliceScomeFloat type: []main.someFloat
> > stringableVectorVar type: main.StringableVector[main.someFloat]
>
> Variable stringableVectorVar is not of interface type, because in such case 
> its type printed by fmt.Printf should be []main.someFloat. So, it looks like 
> to me as []main.someFloat is implicitly conversed to 
> main.StringableVector[main.someFloat].
>
> Answer to my previous questions was that []stupidFloat/[]someFloat is not of 
> type StringableVector[stupidFloat] so it doesn't have method String() string. 
> But in my poor understanding of Go, this code shouldn't compile due to 
> implicit conversion of two types.
>
> Can anyone explain to me, where am I wrong?

You are not permitted to assign directly from one named type to
another named type.  But here you are assigning an unnamed type,
[]someFloat, to a named type, StringableVector[someFloat].  Assigning
an unnamed type to a named type is permitted if the underlying type of
the named is identical to the unnamed type, which in this case it is.
The same is true in non-generic Go.  The exact rules are at
https://go.dev/ref/spec#Assignability.

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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcU1sOL-_h_L1xvtY-79i%3DEef6%3D9%3DafQjEANv4Q4HcriZg%40mail.gmail.com.

Reply via email to