If by "generics doc" you mean this one
<https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md>,
then note:
*"Generic types can have methods. The receiver type of a method must
declare the same number of type parameters as are declared in the receiver
type's definition. They are declared without any constraint."*
That is, you can't define a method on plain Table, but you can define a
method on Table[T1, T2, T3]
Without actually looking further into what your code is doing, that implies
the following change:
func (t *Table*[T1, T2, T3]*) Add (a, b, c interface{}){
row := Row {colA: a, colB: b, colC: c}
append(t.rows, row)
}
However that's also not right, because you're ignoring the return value
from append (for more info read the blog posting on slices
<https://blog.golang.org/slices>). So:
func (t *Table[T1, T2, T3]) Add (a, b, c interface{}){
row := Row {colA: a, colB: b, colC: c}
t.rows := append(t.rows, row)
}
The next problem is here:
func (t *Table[T1, T2, T3]) Print (){
fmt.Println("table format is: %v, %v, %v", reflect.TypeOf(t.colATemplate),
reflect.TypeOf(t.colBTemplate), reflect.TypeOf(t.colCTemplate))
for row := range t.rows {
fmt.Println("%v, %v, %v", row.colA, row.colB, row.colC)
}
}
*prog.go2:72:33: row.colA undefined (type int has no field or method colA)*
*prog.go2:72:43: row.colB undefined (type int has no field or method colB)*
*prog.go2:72:53: row.colC undefined (type int has no field or method colC)*
Iterating over a slice with just one receiver variable gives you only the
index. This needs to be:
func (t *Table[T1, T2, T3]) Print (){
fmt.Println("table format is: %v, %v, %v", reflect.TypeOf(t.colATemplate),
reflect.TypeOf(t.colBTemplate), reflect.TypeOf(t.colCTemplate))
for _, row := range t.rows {
fmt.Println("%v, %v, %v", row.colA, row.colB, row.colC)
}
}
This then breaks because t.rows is a slice of interface{}, not a slice of
Row. So I changed it to []Row. Then fixing your Println's to Printf's,
this gives https://go2goplay.golang.org/p/dAnPU_r0DpM and now it runs.
However, I think this still needs work. There should be no need for
interface{} in Row; you should use generics for this too, and then your
Table just needs to be a slice of Row[T1, T2, T3]
On Friday, 1 January 2021 at 15:06:15 UTC [email protected] wrote:
> I thought I read the generics doc well but.. :-) Help is appreciated:
>
> I instantiate a generic table example here in line 41:
> https://go2goplay.golang.org/p/SadxA0khqx7
>
> Then I use it in lines 42 and 43.
>
> The errors I get are below:
> prog.go2:67:10: cannot use generic type Table[colA, colB, colC
> fmt.Stringer] without instantiation
> prog.go2:72:10: cannot use generic type Table[colA, colB, colC
> fmt.Stringer] without instantiation
>
> I am using the same table. The method belongs to the struct so I would
> think should be considered instantiated and that I wouldn't have to repeat
> in lines 42 and 43 the types.
>
> Is this a bug and it should infer since created in line 41 or what did I
> misunderstand in the doc?
>
> Thanks in advance for the help!
> David
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/9e491c38-2d27-4ec7-bbe1-4a3ae36359fbn%40googlegroups.com.