StabbyCutyou / generics

Package generics is the true way to program generically in golang
Apache License 2.0
193 stars 9 forks source link

G or interface{} is good? #24

Closed jmptrader closed 6 years ago

jmptrader commented 6 years ago

Dear, Good catch. I think that G is interface{} and looks like object (Java, c#) or variant of vb.

Dynamic o manually type checking is inevitable for now, i.e.

Not work was spected...

func Add(a G, b G) G { return a + b } .... var ab := Add( 1, "test" ) ... Compiling ok, run fail without type check...

maybe you need to have a generic {} similar to interface {} but with type checking at compile time.

StabbyCutyou commented 6 years ago

This is the beauty of golang and package generics, and the marriage of the two.

What you're running into is not an issue with G, but the safety constraints the language is placing upon you.

Once inside of your Add method, you will need to use a type assertion to determine which types each value is, and then choose the method of adding them together that golang itself provides.

In this way, your method signatures can be fluid, dynamic, expressive... generic.

But your method implementations must adhere to the strict rules of the wonderful golang type system. I admit, it would be nice if I could make this easier for users, but at the end of the day, golang is never wrong in what it allows, or doesn't allow you to do. It's on us, the lowly mortals, to work within those divine rules.

Thanks for using package generics!