On Fri, Feb 4, 2022 at 5:01 PM Kamil Ziemian <kziemian...@gmail.com> wrote: > > > I title say, I download Go 1.18 beta (at this moment "go version go1.18beta2 > linux/amd64") and I try to work through "Type Parameters Proposal" with it > (https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md). > This thread is about thing that I can't code properly into Go 1.18 beta. > > I hope that you forgive me if I ask some questions before reading full > proposal for the second time, previously I read in the September of the last > year, and trying every solution obvious to good gopher. I have a little time > and energy recently and I still want to try my hand with generics ASAP. > > I read and try every example from "Tutorial: Getting started with generics" > (https://go.dev/doc/tutorial/generics), but it didn't help me with my > problems. Maybe I just not advanced enough gopher (I'm at most medicore > gopher) or I'm to tired recently to think creatively about Go. > > My first stumbling block is from section "Generic types" > (https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#generic-types). > At the end of it we have code. > > type StringableVector[T Stringer] []T > > > > func (s StringableVector[T]) String() string { > > var sb strings.Builder > > for i, v := range s { > > if i > 0 { > > sb.WriteString(", ") > > } > > // It's OK to call v.String here because v is of type T > > // and T's constraint is Stringer. > > sb.WriteString(v.String()) > > } > > return sb.String() > > } > > So, I try to code it. > > > package main > > > > import ( > > "fmt" > > "strings" > > ) > > > > type Stringer interface { > > String() string > > } > > > > type StringableVector[T fmt.Stringer] []T > > > > type stupidFloat float64 > > > > func (sF stupidFloat) String() string { > > return fmt.Sprintf("Stupid float %v", float64(sF)) > > } > > > > func main() { > > var varStupidFloat stupidFloat = -1.0 > > > > sliceStupidFloat := make([]stupidFloat, 3) > > > > for i := 0; i < 3; i++ { > > sliceStupidFloat[i] = stupidFloat(float64(i)) > > } > > > > fmt.Println("stupidFloat.String(): ", varStupidFloat.String()) > > > > fmt.Println("sliceStupidFloat:", sliceStupidFloat) > > > > fmt.Println("sliceStupidFloat:", sliceStupidFloat) > // fmt.Println("sliceStupidFloat.String():", > sliceStupidFloat.String()) > > } > > > > func (s StringableVector[T]) String() string { > > var sb strings.Builder > > > > for i, v := range s { > > if i > 0 { > > sb.WriteString(", ") > > } > > sb.WriteString(v.String()) > > } > > > > return sb.String() > > } > > It works fine and produce result. > > stupidFloat.String(): Stupid float -1 > > sliceStupidFloat: [Stupid float 0 Stupid float 1 Stupid float 2] > > sliceStupidFloat: [Stupid float 0 Stupid float 1 Stupid float 2] > > But, when I uncommon last line in function main I get > > ./Go-s11-08.go:46:61: sliceStupidFloat.String undefined > > (type []stupidFloat has no field or method String) > > I try to change my code in several ways, but result is always the same. How > correct code should look like?
sliceStupidFloat is a variable of type []stupidFloat. As such, it does not have a String method. That doesn't have anything to do with generics, it's just how the Go language works. Perhaps the line that you are looking for, rather than the commented out line, is fmt.Println("sliceStupidFloat.String():", StringableVector[stupidFloat](sliceStupidFloat).String()) 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/CAOyqgcW%3D5oEOV9UNQwOd5G4Ps_K2PscKYtrqYOqziHhRtoM21g%40mail.gmail.com.