On Fri, Jul 1, 2016 at 12:40 PM, <charras...@gmail.com> wrote:

> 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.
>

How does the compiler know sizeof(T)? If it's passed as an argument or
saved together with the struct or something in that regard, you end up with
the "java approach", if you have the compiler specializes the function per
size, you end up with the "C++ approach" (also, in either case, there is no
need for the "only pointers are allowed" kludge), from
http://research.swtch.com/generic
The most basic rule, for everyone arguing in favor of generics, is, that
they need to decide which tradeoff they make and how they justify it. That
blog post should be step 1 for everyone wanting to propose generics.

-- 
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.

Reply via email to