6 ms·
Translation Units Considered Harmful?
- chromatin 6y ago> In practice, we use the preprocessor - a tech implemented by a drunk bell labs intern on LSD sometime in the late 60s, early 70s - to stitch together a collection of files that we are never quite sure where in the system they come from. We call them headers and source files, but really, you can include a .cpp file in a .h or elect to use the .js extension for headers, .rs for sources files and your tools would not care. You can, of course, create circular header dependencies. The whole section is gold
- throwaway_pdp09 6y agoUmm. 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!
- still_grokking 6y ago>But, you are a very great engineer, so you wouldn’t let a source file grow above a few hundred lines of code, would you? I bet your code is beautiful, like this small self-contained super useful program #include <iostream> int main () { std::cout << "Hello world\n"; } >Which on my system expands to about 33000 lines of code. Really great rant! Much underrated.
- esc_colon_q 6y ago> But as the C++ community dreams of better tools, and dependency managers, maybe it would help to define the fundamentals more accurately: Our programs are a set of definitions, some of which are provided and maintained out-of-tree by other people. I think the more closely our tools adhere to that model, the better off we will fare in the long run. > So maybe we need to ask fundamental questions about the compilation model and examine some beliefs we hold (For example “Compilers and build system need to be kept separated”. Do they? To what extent?). I think you just put your finger on why Google is a monorepo - Blaze/Bazel kinda suck horribly (they feel super dated and could have been made much friendlier if they'd been designed from the ground up knowing what the pain points are, and esp if they weren't basically an ad-hoc set of new languages/syntaxes), but they're also awesome. The huge benefit is that you can make tools that work across everything and track monstrous dependency graphs.
- deleted 6y ago[deleted]
- vlovich123 6y agoI'm curious about this. I haven't felt that way about Blaze. What parts could be made friendlier?
- toolslive 6y agoregarding noexcept. Honestly, `noexcept` is more documentation than anything else. Its specification merely guarantees that if the function does throw, you get a call to `std::terminate` (which aborts). It's just another thing you cannot really really trust.
- senkora 6y agonoexcept is also important for optimization. If a template function is guaranteed to be exception-safe, and it’s called with a type whose move constructor isn’t noexcept, then the function needs to fallback to the copy constructor to maintain exception-safety. This happens a lot with the standard library.
- nicoburns 6y agoIt would be better if it could actually statically guarantee that the function doesn't throw though!
- choeger 6y agoI don't agree fully with the idea of getting rid of compilation units, object files and libraries. I think their existence is necessary for fundamental technical reasons. I also consider the "link everything statically" approach failed, because in the end you want at least some run-time linking (vdso for instance and actually also the fundamental system libraries). But also languages like rust and swift and before them OCaml and Haskell show that the current prevalent C/C++ ABI is not powerful enough. And in principle, there is not much of a difference between the reason for header-only libraries in C++ and rust not having a composable ABI. It is always about the lack of information one can pass through the ABI. Interestingly, one does not even need to exchange the definition of a rust function or that of a C++ template for monomorphisation. One could easily define an extensible library that will create the new specialisation for any given input. One would only need to "freeze" the compiler that was used to create the library and ship it with that library. Ok, easier said than done, of course.
- fouric 6y agoWould you describe these "fundamental technical reasons"?
- choeger 6y agoWell, I think that we need an executable format that is universal, transparent, and standardized. With ELF I can take a debugger and attach it to a rust binary as well as a C++ or go binary. Also, we want to be able to interface different languages in an efficient manner. Now with a universal executable file format that becomes possible, but requires us to have a way to combine different object files. From here on, I think it becomes obvious that we should have compilation units. A language that does not allow the definition of such a standardized object is a bad citizen, IMO. Rust, for instance does allow it, but is severely hampered by the limitations of the C-ABI.
- fouric 6y agoI still don't understand what the "fundamental technical limitation is". It seems more like you're making the argument that "an executable format that is universal, transparent, and standardized" is a predicate for getting rid of translation units - which means that it's not a fundamental problem. Additionally, I don't see how removing translation units precludes multi-language integration.
- deleted 6y ago[deleted]
- IshKebab 6y agoAnother lazy title. Article writers, please stop!