8 ms·
Umm. I'll read the article but as far as preprocessors go, I'm doing some C# and I'd really, really like one so as not to have to reproduce loadsa boilerplate.
by throwaway_pdp09 6y ago
Umm. I'll read the article but as far as preprocessors go, I'm doing some C# and I'd really, really like one so as not to have to reproduce loadsa boilerplate.
The removal of such things seems to be driven by dogma as much as good reasons, of which there definitely are.
- Jtsummers 6y agoI'd rather have boilerplate than C's preprocessor system. Perhaps a different macro system would be nice for C#, but not C's.
- MereInterest 6y agoI'd rather have language features to cover those use cases than a preprocessor. As an example of what I mean, in Java and C++, class properties are typically accessed by getters and setters, since you might want to change the internal structure of a class without changing the interface. In Python and C#, you can get the same benefit by changing a member variable to a property. As a result, the boilerplate of getters/setters isn't needed.
- throwaway_pdp09 6y agoErm, I get this, but I want a PP because the language doesn't do what I want.
- MereInterest 6y agoIn that case, why not fix the language, rather than adding an entirely separate subsystem to the language?
- throwaway_pdp09 6y agoBecause I'm not in a position to. However, that's pretty well what I said elsewhere, so we're in agreement.
- MereInterest 6y agoMakes sense. For me, I see the preprocessor as a fundamental part of the language, since that is how it is implemented in C and C++. I was figuring that it would take just as much effort to implement/change a preprocessor as it would to change the core language.
- marcosdumay 6y agoSome people look at a problem and say "I know how to solve that! I'll write a code generator!". They then have a problem generator, that will output fresh interesting problems every time it runs. It is the only advanced tool available in C and C++, where people hate it. But for some reason Java developers are great fans (even though reflection is available), and their generators will always accept XML. It's still the first time I see a C# developer asking for it.
- qsort 6y agoThe problem isn't even code generation. It's the fact that the C preprocessor adopts the most retarded model one can possibly think of, text replacement. Read the paragraph following point 3. in the following link, and laugh or cry (your choice): https://man7.org/linux/man-pages/man3/pthread_cleanup_push.3.html https://man7.org/linux/man-pages/man3/pthread_cleanup_push.3... Thinking in terms of strings of text instead of the abstract syntax tree is literally the roman numerals of computer science.
- throwaway_pdp09 6y agoTo be fair you're rolling in the luxury of hindsight. It wasn't that log ago that a supercomputers had only a few MB of memory. Price US$7.9 million in 1977 (equivalent to $33.3 million in 2019) Weight 5.5 tons (Cray-1A) Memory 8.39 Megabytes https://en.wikipedia.org/wiki/Cray-1#Cray-1M https://en.wikipedia.org/wiki/Cray-1#Cray-1M in the 1970s. Dig up the spec of the PDP and be, well, something'd. Not sure what. Glad you were born now, I guess.
- qsort 6y agoNot sure how this is relevant to the topic at hand. Sure, there are many features of C that are easy to laugh at but in context make a lot of sense, but the macro system is not one of them. Hart/Levin LISP had hygienic macros, just saying... And you don't even need the full generality of a compiler to have limited but still useful AST-rewriting macros, there are a ton of ways this could be implemented very cheaply in terms of both memory and cpu, none of which are rocket science. Seriously, the fact that textual macros are terrible isn't even a controversial point. Maybe I'm too young to be a Real Programmer(tm), but this 'everything is text' idea never made sense to me.
- dnautics 6y agoyou 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 ?
- emmanueloga_ 6y agoCan partial classes help? I always thought it was a great feature to have, and as far as I know only C# implements something like it. https://stackoverflow.com/a/3601918/855105 https://stackoverflow.com/a/3601918/855105
- throwaway_pdp09 6y agoPerhaps. They don't seem to add all that much, and what really pissed me off is having to write extension classes to enums so they would produce a string representations of themselves. Again and again. Not sure that would help here but if this carries on I will just stick these in python, treating it as a preprocessor. There are other things too. But thanks for the link, will read up.
- vvanders 6y agoTo this day I still write #define WIN32_LEAN_AND_MEAN reflexively ahead of #include <windows.h>, it's a muscle memory I can't unlearn. Be careful what you wish for.
- c17r 6y ago> I'm doing some C# and I'd really, really like one so as not to have to reproduce loadsa boilerplate. If it's repetitive boilerplate, you could probably use T4 to generate it for you.
- throwaway_pdp09 6y agoT4 - microsoft's M4 - but worse. But I didn't think of that. Don't want to, but thanks for the reminder! (will probably use python if I have to do similar).
- koyote 6y agoI have never wanted a preprocessor and I write both C# and C++. Are you able to share your use-case for one in C#?
- throwaway_pdp09 6y agoA few things, but see other of my comments re. extension methods for enums -> strings.
- koyote 6y agoYou can ToString() an enum and Enum.Parse a string to an enum. Or are you talking about pretty printing? You can also create reasonably 'generic' extension methods that will work on 'any' Enum. It's a bit more fiddly but you'd only have to write it once. That being said, Enums in C# are crap compared to Java.
- throwaway_pdp09 6y agoI'm pretty printing. > You can also create reasonably 'generic' extension methods that will work on 'any' Enum Can you post a link or something to this, I didn't know it was possible and it will prove useful (even if only as an exercise). > Enums in C# are crap compared to Java. yessss, interesting isn't it. Some features of C# seem to be held back so as to make the next release look more attractive, do you think?
- koyote 6y agoSomething like this? https://dotnetfiddle.net/xwYCrt https://dotnetfiddle.net/xwYCrt You can also use the Enum static functions in order to get metadata on any enum. If you use attributes such as [Display("Pretty Name")] you can use that to retrieve the value from an enum.
- throwaway_pdp09 6y agoThat's an extension method, I was doing that. What's new is you can pass in any enumeration as you've used Enum as the base class. OK, I should have thought of that. Not sure it would have helped but... Now the [Display("Pretty Name")] is what I want! It apparently uses reflection, but for my app that's a very acceptable cost. I can see a few hundred LOC disappearing - thanks so much!