Excuse me, but I'm lost again. I think I read it in the "A Tour of Go" and 
"Go FAQ", that "In Go types just are" and "There is no implicit conversion 
of types in Go". Because of that code below doesn't compile.

> package main
>
> import "fmt"
>
> type someFloat float64
>
> func main() {
>         var floatVar float64 = 1.0
>         var someFloatVar someFloat = floatVar
>
>         fmt.Println(someFloatVar)
> }

We need explicit type conversion "someFloat(floatVar)" to make it compile.

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?

Best,
Kamil
sobota, 5 lutego 2022 o 11:44:06 UTC+1 Kamil Ziemian napisał(a):

> Thank you very much for information. I think now I understand my problem. 
> I was thinking about method just as a function ("methods are just a 
> functions" is one line from "A Tour of Go", that I know but don't 
> understand its depth-in-simplicity) and for some reason I assumed 
> StringableVector[T Stringer] is a kind of constraint. A such my thinking 
> was that sliceStupidFloat is of type []stupidFloat, when stupidFloat 
> satisfying constraint Stringer, so function (method) find that such 
> constraint is satisfied.
>
> Good gopher probably from the start would understand that since 
> StringableVector[T Stringer] is NOT an interface type, it is a concrete 
> generic type, not constraint. (Is calling something "concrete generic type" 
> correct statement?) My stupid excuse is that materials on generics in Go 
> focus on using generic functions, not generic types. I don't want to be 
> misunderstood, in materials about Go I find a lot of examples how to define 
> and call generic function. There is also many examples of defining generic 
> types, but very little of creating and using objects (?) of generic types.
>
> For example "Tutorial: Getting started with generics" (
> https://go.dev/doc/tutorial/generics) deals only with generic functions. 
> I guess some additional tutorial about generic types will be good thing for 
> people like me.
>
> Best regards,
> Kamil
> sobota, 5 lutego 2022 o 02:57:23 UTC+1 Ian Lance Taylor napisał(a):
>
>> On Fri, Feb 4, 2022 at 5:01 PM Kamil Ziemian <kziem...@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/e35f275b-1fbb-4a15-b9c9-3a73de5d21fan%40googlegroups.com.

Reply via email to