r/programming • u/alexeyr • Jan 16 '20
Defunctionalization: Everybody Does It, Nobody Talks About It
https://blog.sigplan.org/2019/12/30/defunctionalization-everybody-does-it-nobody-talks-about-it/
115
Upvotes
r/programming • u/alexeyr • Jan 16 '20
19
u/horsesAreSpheres Jan 16 '20
I originally thought this. Of course this is a useless optimization that makes the code harder to read and change.
But, after reading the article a second time, I realized the author doesn't advocate for defunctionalization in every situation. It's only to support the very specific design requirement of needing to send functions between different systems.
If you had a normal function
\x -> 5 < x and x < 10, you couldn't print this function. However, when at function is stored as a data type such asAnd (GreaterThan 5) (LessThan 10)it can be printed or sent between systems.So, defunctionalization is only used when you have to reason about the code. It's a way to manually create an AST that you can reason about during run time.