3 ms·
...except that both can be applied to solve the same problems. And so they are in fact very related. For instance with a macro you could do (made up syntax)
by dagss 8y ago
...except that both can be applied to solve the same problems.
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.
- djur 8y agoUsing 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.
- slavapestov 8y agoMacros and C++ templates are also incompatible with separate compilation which leads to increased compile times and binary bloat.