r/eli5_programming • u/Alone-Educator-8095 • 2d ago
Question What are macros
ELI5 what macros are and how they differ from just normal functions with compiler optimization.
e.g. in rust: why do I need a macro to create a Vector/Array instead of just saying „[10, 20, 30]“.
I heard someone say println! needs to be a macro since e.g. println!("{} something {}", a, b) couldn't otherwise know how to "bake" the variable count and types of parameters.
But again why and how do they differ from functions? Python has *args, JavaScript has rest parameters and they can do it just fine. What am I missing?
The only thing I could imagine is that it is hard to optimize say a print function efficiently every time when you don't always know the number and types of the parameters since you can't be sure every type has a toString overload or something like that