Code generation is one possible implementation of generics. But generics isn't just about the code you run but also about making sure the source code is sound at compile-time, i.e. they also have implications for the compiler's type checks.
Surely if you use code generation to create an "IntFancyDataStructure" then, once generated, type safety is ensured? There's no type erasure, no interface{} etc., in fact from compiler's point of view it's as simple as it gets. Or am I missing something?
> How would you define a function that accepts/returns FancyDataStructure of any type or only of a type that has X embedded?
I'm not sure what you mean by this. If you mean that it accepts any type known at compile time, then you use the code generator (we're talking about a function template, after all). If you want this to work with any type known at runtime, then use Go's interface since you need the vtable.
> BTW type erasure isn't necessarily a bad thing when the compiler can prove the code is correct at compile time.
Sure, though I'm not sure what the performance implications -- and while I can think of some counter-examples were this wouldn't be the case, in most cases the impact would probably negative.