5 ms·
you don't want preprocessors. Of all the metaprogramming choices, the preprocessor (C or erlang) is the worst, since it operates on lexical forms, and is sort
by dnautics 6y ago
you don't want preprocessors. Of all the metaprogramming choices, the preprocessor (C or erlang) is the worst, since it operates on lexical forms, and is sort of its own DSL. Not only do you have to learn two languages for the benefit of one, but you're gonna have bugs and it's much harder to automated tools to do code analysis.
You could have lispish ast transforms (rust, julia, elixir)
You could have comptime (zig, elixir)
both of these are far nicer than preprocessor.
- jcelerier 6y agoif you don't give a preprocessor to people they will either hack it (see cpp being used for parsing .Xresources and affiliated) or write their custom preprocessor in an unholy mess of bash / python / ruby / cmake anyways.
- andrepd 6y agoWhy? Anything you want to do on a textual preprocessor you can also do on an AST rewriter (like ocaml ppx or rust macros).
- a1369209993 6y ago> Anything you want to do on a textual preprocessor you can also do on an AST rewriter That is blatantly and obviously false: #define BEGIN { #define END } int main(void) BEGIN int x = 0; return x; END
- sebbertsen 6y agoI don't want to do that
- nemetroid 6y agoThey said "want", not "can".
- jcelerier 6y agobut can you do it in a couple lines of ugly hacks or grep/awk/sed scripts at 02:35 AM when shit hits the fan ?
- dnautics 6y agoYeah when the shit hits the fan, you do this deploy, and at 5:00 am you realize that you forgot that your prepro macro was supposed to be a pseudo function and you shouldn't have put a semicolon in there what do you do?
- rixed 6y agoLet's never assume we can know what anyone will ever legitimately want to do. One of the use cases of preprocessors is to accommodate several versions of the language. Obviously, if the AST changes between versions, then no AST rewriter can help. We have had to bake our own ad-hoc simple preprocessor for OCaml to accommodate various versions of its stdlib API, for instance.
- lmm 6y agoNot if you're a decent language that can fulfil their use cases without needing a preprocessor.
- throwaway_pdp09 6y agoYou may be right but prepros are what I'm familiar with and AST-mungers I'm not. The best solution is better language features. I get the PP = DSL (not a problem) that makes the link between original DSL code and the produced code very obscure (big problem) but they are needed at times. Thanks for pointing me at AST transformers.
- dnautics 6y agoYou should also check out zig's comptime, it's fantastic.
- Doxin 6y agoD has a nice middle-road between a preprocessor and an AST-munger: mixins. The mixin function is essentially compile-time eval: whichever string you pass it gets compiled into the program at that point. Combined with solid compile-time function evaluation and doing wacky things like brainfuck-as-a-DSL becomes fairly trivial.