Hacker News new | ask | show | jobs
by Kalanos 1608 days ago
never understood why this was better/ how it was different from regular functions. just seems like bugs/vulns waiting to happen
4 comments

Being able to operate on expressions before they reach the compiler is very handy.

Consider how ergonomic testing is thanks to macros: https://docs.julialang.org/en/v1/stdlib/Test/

Here's an example of passing quasi-json to a plotting function: https://www.queryverse.org/VegaLite.jl/stable/userguide/vlpl... . This lets you essentially transliterate a VegaLite spec into Julia without needing to translate it into Julia.

Finally, macros that operator on dataframes let you write code that looks kind of like SQL, and is much more pleasant than working with functions: https://dataframes.juliadata.org/stable/man/querying_framewo...

It is not necessarily better in all cases and should not be overused: https://youtu.be/mSgXWpvQEHE?t=579

However, it is useful to provide a nicer syntax and DSLs.

Some examples: https://stackoverflow.com/questions/58137512/why-use-macros-... https://www.juliafordatascience.com/animations-with-plots-jl... https://gist.github.com/MikeInnes/8299575

Some macro systems can create variables in a loop for you. You could make this macro,

     (define-all i 5 0)
     ;; creates i1 i2 i3 i4 i5 initialized to 0
That's somewhat impossible with functions. The closest you get is either an array/dict with only runtime error checking, or an external codegen program.

I wrote a post [0] about how to do this in Racket. The macro generates ORM code based given a SQLite DB. Aka the compiler queries SQLite and generates table-column functions automatically.

More potential benefits are: Better static error messages (can implement a type system using macros, example here[1]), and controlling execution order (can add lazy computation semantics).

[0]: http://tech.perpetua.io/2022/01/generating-sqlite-bindings-w...

[1]: https://gist.github.com/srcreigh/f341b2adaa0fe37c241fdf15f37...

no one is proposing macros as a paradigm of programming. they simply give programmers expressive powers not afforded by use of regular functions. in a sense you can compare macros to c++ templating. know what you are doing and use sparingly