Thank you so much, about explanation. This is the first time that I hear 
about named type and unnamed type in Go.

Thank you even more for giving my another reason to learn Go Spec. I try it 
before, but then I was lost in section about runes, so I go to learn UTF-8. 
But soon enough I understand that to learn UTF-8 I need to go learn about 
Unicode and I try to find time for it.

Best regards,
Kamil
niedziela, 6 lutego 2022 o 03:47:36 UTC+1 Ian Lance Taylor napisaƂ(a):

> On Sat, Feb 5, 2022 at 4:27 PM Kamil Ziemian <kziem...@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/cd1e67f7-0b56-4223-92ae-04663625f70cn%40googlegroups.com.

Reply via email to