On Wednesday, June 15, 2016 at 3:04:05 AM UTC+2, xingtao zhao wrote: > > Here is my proposal for generic in go: > https://docs.google.com/document/d/1nO7D15c2B3eq2kF62C0yUs_UgpkyPL2zHhMAmlq1l98/edit?usp=sharing > > Many parts has not been finished, and just initial thoughts. In the > proposal, I want to keep back compatibility. And I try to add the > orthogonal feature only and keep the language still simple enough. > > Please add your comments on it. Hope it is useful and could inspire some > new features in go 2.0 > > the parametrized struct got me interested, how to best handle the embedding the arbitrary type.
Especially passing said struct to a generic function. type S<T> struct { I1 int C1 char *T1 T* I2 int *Ts1 [2]T* Is1 [10]int Ts2 [53]T C2 char T2 T Ts3 [2]T Tp1 *T I3 int } Now imagine that T=int8 For the reason, the following concrete struct satisfies the template: type ActualS struct { I1 int C1 char *T1 i*nt8 I2 int *Ts1 [2]**i*nt8 Is1 [10]int Ts2 [53]*i*nt8 C2 char T2 *i*nt8 Ts3 [2]*i*nt8 Tp1 *T I3 int } Now, the concrete struct is passed to the algorithm that accepts S<T>. func IllegalAlgo<T>(x S<T>) { x.T1 = x.T2 } I think this should not be allowed, because variable x could have different sizes based on sizeof(T). But passing the arbitrary large struct S via pointer could work: func OKAlgo<T>(x *S<T>) { x.T1 = x.T2 } Now. Obvious mode of operation would be that the procedure or algorithm would perform the multiplications by sizeof(T) to access fields. In my opinion, would be best to move the generic-sized objects to the end (or require them to be placed at the end). This way said algorithm could operate on almost the whole object without need for arithmetic. type S<T> struct { I1 int C1 char I2 int Is1 [10]int C2 char Tp1 *T I3 int ////////// *T1 T* *Ts1 [2]T* Ts2 [53]T T2 T Ts3 [2]T } > Thanks! > -- 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. For more options, visit https://groups.google.com/d/optout.