Hey all,

Congrats to the Go Team for shipping generics!

I was experimenting with a design for generics for a long time. I thought 
I'd share it anyway for fun. I think it has some interesting (IMHO) aspects 
and trade-offs compared to the design that shipped:

   - Type variable declarations are implicit
   - Type arguments for functions are implicit
   - Type variable constraints can be omitted for the empty interface
   - Methods can be generic
   - Operations are represented with generics as normal methods
   - It can simplify the Go language specification
   
Here's a quick sample:

type Number interface {
    $N Add($N) $N
}

type MyInt struct {
    my int
}

func (x MyInt) Add(y MyInt) MyInt {
    return MyInt{my: x + y}
}

type MyFloat struct {
    my float64
}

func (x MyFloat) Add(y MyFloat) MyFloat {
    return MyFloat{my: x + y}
}

func Add[$N Number](a, b $N) $N {
    return a.Add(b)
}

var _ MyInt = Add(MyInt{1}, MyInt{2})
var _ MyFloat = Add(MyFloat{1}, MyFloat{2})

There are also a few other feature ideas at the end, for those interested 
(sum types, tuple types, unifying signatures and structures).

Here it is:

https://gist.github.com/willfaught/70f178ff52296d1865ae1f5ea0e574c4

-- 
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/efa7c07e-4a92-48b3-80e0-2820d1831ecan%40googlegroups.com.

Reply via email to