r/Compilers • u/General_Purple3060 • Jul 13 '26
Delayed Specialization: A Third Way to Implement Generics?
While implementing generics in my GCC-based language (AET), I wasn't satisfied with the two mainstream approaches:
- C++ Templates: Generate a full copy of the code for every concrete type (monomorphization) → code bloat and longer compile times.
- Java Generics: Use type erasure → no code duplication, but lose concrete type information.
So I explored a middle path: Delayed Specialization.
How it works in AET:
During the first compilation:
- Generic parameters (E, T, ...) are treated as void*
- Code that needs the real type is wrapped in a genericblock$
For example:
class$ Abc<E>{
void setData(E value);
};
impl$ Abc{
void setData(E value) {
E a = value;
genericblock$(a) {
E x = a;
E y = 5;
x += y;
}
}
};When the compiler later sees a concrete instantiation like Abc<int>, it performs a second compilation pass only on the Generic Blocks, replacing E with int.
Benefits:
- Avoids C++-style template explosion
- Keeps most generic code shared (like Java)
- Still allows real type-specific operations where needed
I call this Delayed Specialization. It sits between full monomorphization and type erasure.
Has anyone seen a similar approach in other languages or compilers? I'd love to hear about papers or existing implementations using delayed/late specialization.
View all comments
Show parent comment
2
u/dnabre Jul 14 '26
How you structure, order, break down your compilation process into passes or intermediate steps is an implementation detail and doesn't change what methods are being used. Doing a single
XYpass or doing two passes, that ones that doesX, and another pass to doYis the same thing. Separating the pass may or may not be helpful to the implementation, but you are still doingXY. So thinking/explaining in terms of passes may be useful, but it doesn't change what you are doing.What I'm understanding, setting aside your "generic blocks" and whatnot, if you have calls to
setDatawith typeTused asint,float, andfoo, you generate code specific for each type (three blocks of code). That is monomorphization.You are splitting the generic code into two parts: the generic function
setDataand thegenericblock$generic block of code. You then use monomorphism (which is not a bad thing) to implement the genericgenericblock$code.How do you then implement the generic
setData? You have broken off the body of the generic function and implemented that, but how are you implementing:Having a single version of
setDatais just kicking the generic can down the road.At some point you need each of the different typed call sites to get to their specialized implementation. Instead of just rewriting each site to jump to the type-specific version, you are having them all jump to one implementation of
setData. What then?I don't know how you implementing that generic. My first though is dynamic dispatch - switching on the type
T, and jumping to the type-specific version). That would do the job, but you'll need a C++ vtable or the like to do that. Which is adding code, runtime type information (very streamlined amount), and extra branches. Seems like a lot of overhead just to maintain a single copy ofsetData.This maybe where you doing something new.It looks like you are just splitting your generic implementation into two parts, using monomorphism for half, and some other method for the other half. I'm not sure what benefit there is to doing that.