DELAPARRA
Index
Color
NL
Category: Programming paradigms

Monomorphization

Explained by Updated

What is Monomorphization?

Monomorphization: In other languages (Rust, C++ templates), the compiler generates a separate concrete version of the generic code per type used — faster, but bigger binaries. Monomorphization is how languages like Rust and C++ compile generic code. For every concrete type you use a generic function with, the compiler creates a separate, specialised copy of that function.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Monomorphization

How is Monomorphization used in practice?

A generic max<T> used with integers and with floats becomes two functions in the final program, each optimised for its type. That makes generic code as fast as hand-written specific code.

What should you keep in mind with Monomorphization?

The trade-off is larger programs and longer compile times, because the same logic is compiled many times. Other languages make different choices. Java erases generic types at runtime and uses one shared version, and PHP has no generics at all, only checks by static analysis tools. You rarely need to think about this unless binary size or build speed becomes a problem.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with M

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.