Re:Maybe adding generics to Go is about syntax after all
by djur on Tuesday September 04, 2018 @11:18AM
(#17909611)
Using macros to implement generics has the same problem that C++ templates have, which is described in the Go 2 generics proposal: it's difficult to provide clear error messages for developers in many cases. Functions parameterized on a single type are the easy problem to solve. Where things get thorny is when you parameterize on multiple types with various constraints and/or have some kind of interaction in the function body. The goal of the Go 2 generics contracts (and C++ concepts, Haskell typeclasses, Rust traits, etc.) is to make those expectations explicit.
Re:Maybe adding generics to Go is about syntax after all
by slavapestov on Tuesday September 04, 2018 @06:24PM
(#17913384)
Macros and C++ templates are also incompatible with separate compilation which leads to increased compile times and binary bloat.
And so they are in fact very related.
For instance with a macro you could do (made up syntax)
SortIntSlice = mymacros.MakeSorter!(int)
...then at compile time have code for sorting a slice of ints generated.
This is again similar to C++ templates (main difference is C++ will implicitly instantiate instead of explicitly).
And C++ doesn't need generocs because people use templates instead.
They really are in the same problem domain.