272 points · by pplonski86 · 7y ago · 193 comments
import std.stdio;
means look for: std/stdio.d2. Because Windows has case-insensitive filenames, and Linux, etc., have case sensitive filenames, we recommend that path/filenames be in lower case for portability. This annoys some people.
3. There are command line switches to map from module names to filenames for special purposes. They're very rarely needed, but invaluable when they are.
Overall, it's been very successful.
That's also a problem in most other languages; just a couple of days ago, someone else's C++ code didn't compile on my machine because they had accidentally included <something/whatever.h> when the file was actually named <something/Whatever.h>, because macOS is case insensitive. I had the same experience with JavaScript some months ago, that time because they were running Windows.
On the "filenames must be valid identifiers" thing; I really wish more languages would start allowing kebab-case in identifiers. That's also absolutely not a D thing, more of a common complaint about most languages.
this could be solved by allowing strings in qualified imports
import "illegal identifier"."some more weird unicode" as someLib;I'd say "valid X language identifier characters" should always be ASCII.
I never understood the BS fad for unicode identifiers.
Wanna allow some math symbols? Maybe. The full unicode gamut, so that you can have a variable named shit emoji? Yeah, no.
import A -- compiler reads A.hs
import A.B -- compiler reads A/B.hs
Thus, two semantically related modules are now in different directories.Python, IMO, handles this correctly by having __init__.py support inside directories. It's theoretically less elegant because of the special name, but in practice leads to better file organization.
Same for Rust, but even better, because one can define nested modules in the same file. So you can either define a new module in the same file, put it in a different file named by the module, or put it in the file `mod.rs` inside the directory named by the module.
> import A -- compiler reads A.hs
> import A.B -- compiler reads A/B.hs
That has to happen at some level, assuming subdirectories; otherwise, what would "import A.B.C" refer to?
require( "engine.shared.entities" )
or require "engine.shared.entities"
Which means look for: engine/shared/entities.lua
Additionally, with the `package` module, `require` can also be modified to look for: engine/shared/entities/init.lua`
which is a common Lua practice.Not that my memories of C++ are bad or that I'd avoid using it again! It's just that it would be like trying to reconnect with someone I haven't seen since college. I'm curious, but I don't know if it would be worth the awkwardness.
It's the best time to be a C++ programmer, because if there's something that annoyed you about C++03, there's probably a better way to do it in C++11/14/17.
In a word, Rust is becoming "C++ for 80% cases", but the remaining 20% is inherently difficult (much harder than most people's wildest imaginations - just try to implement a file system library, make it work on last two versions of Windows, Mac and major Linux distributions and you'll understand what it means).
- Go doesn't place requirements to the name of files defining a package [1]. However, it has a preprocessor neither, so the problems described in this article (specifying the module name within an #ifdef) are impossible.
- FreePascal has a preprocessor [2], but it defines a deterministic algorithm [3] to find the files containing a unit (the FPC equivalent of a module). Moreover, the compiler creates two files for every unit: a .o object file, and a "unit description file" [4], much like the C++ proposal.
It seems that FPC's case is the most similar. I think the author is right; the C++ committee should adopt a deterministic way to find the name of the files defining a module.
[1] https://golang.org/ref/spec#Packages
[2] https://www.freepascal.org/docs-html/current/prog/progse4.ht...
[3] https://www.freepascal.org/docs-html-3.0.0/user/usersu7.html
[4] https://www.freepascal.org/docs-html/current/prog/progse13.h...
In Go, the import path and package name are two distinct things: the import path locates and identifies the package, while the package name acts as the default name for scoping qualified name exported from that package.
Furthermore in Go the file names are not part of the import path: the name of the directory containing the files that together define a single package is part of the import path.
An imperfect analogy with C++ would be:
- Go import paths <-> path to the included file (header)
- Go package names <-> a namespace inside that included file.
- Go package filenames <-> sections within the included file
I'm generally disliking the need to maintain separate header and implementation files. Maintaining both is time consuming and putting everything in headers is no panacea, either. Now modules seem to add another type of interface definition to the mix that would need to be maintained after a project adopts modules.
It’s archaic and low level, but it’s also powerful and expressive. Replacing the CPP would probably just require a new language.
That being said, if constexpr is great; iterating over either sparse or dense arrays with in Blaze without wrapping in two functions was mind-blowing when I discovered I could so easily.
One of the design goals of the module proposals is that there is a migration path from pure include to pure modules. The transition must not require a flag day.
In particular a program must be able to handle a mix of modularized libraries and old school libraries for the rest of the eternity (it is not likely that C is going to move to modules anytime soon).
If you have the other module as a build step it will compile. If not, you have a linker error.
I don't see the issue here?
Especially if you force module imports to be at the top of the file, you could only scan the start of the file to get an idea on whether you can continue and pause until it's possible or all modules are finished and you didn't get your BMI.
When a new import statement is encountered, Python will first find a source file corresponding to that module, and then look for a pre-compiled version in a deterministic fashion. If the pre-compiled version already exists and is up-to-date, it will be used. If no pre-compiled version exists, the source file will be compiled and the resulting bytecode will be written to disk. The bytecode is then loaded.
The article suggests using this idea in C++ and the parent comment objects, but then it sounds like you're saying it wouldn't be needed anyway (so you're disagreeing with the article too?).
Maybe I'm missing something?
It's not like C++ modules were designed by random nobodies, though; this has been worked on by build infra engineers at major companies with enormous C++ codebases like Facebook, and compiler maintainers e.g. the Clang maintainers. It's possible they completely forgot to think about parallel builds, but that seems at least a little unlikely.
The clang modules proposal had the concept of mapping files, mapping module names to file names.
Companies like Facebook will presumably use proper build systems that already encode the dependency information in the build files rather than try to autodetect it. In that kind of an environment this proposal probably isn't particularly painful.
That seems to leave us with just one conclusion: the article is right, and most of the ecosystem will never migrate to modules, leaving us with the worst of both worlds.
What??? How would that happen? Are modules always compiled with zero flags because in non-module c++ how the dependent module gets compiled is defined in the build system so in order for the compiler to build a missing .bmi it would have to ask the build system how to build it .
That seems to answer the question. What happens if foo.bmi does not exist? Anwser: you get a compilation error (Missing foo.bmi or foo.bmi out of date). You then need to go fix the dependencies in your build system to make sure foo.cpp gets compiled before bar.cpp.
Right?
I get that might suck but it's not unprecedented. lots of builds have dependent steps. Maybe in order to implement C++ modules build systems will need an eaiser way to declare lots of dependencies where as now dependencies are an exception?
The build system just figures out how to invoke the compiler. The compiler does the actual building. When the compiler runs, it has all the flags.
Remember, headers in C / C++ are basically a file level construct. They happen before you even split the file into tokens. #include just means "do the equivalent of opening that file in a text editor and copy and paste it in place of this #include line."
The compiler is already compiling header files, as part of compiling cpp files.
modules work with the import statement (new) not the #include statement. They are not the same as include at all.
In fact this is spelled out in the article in the first goal
> The “importer” of a module cannot affect the content of the module being imported. The state of the compiler (preprocessor) in the importing source has no bearing on the processing of the imported code.
In other words, the flags passed in when compiling bar.cpp have no effect on foo.bmi. foo.bmi is the result of the flags passed in when foo.cpp was compiled and those flags can only be gotten from the build system if foo.bmi does not exist.
Sometimes C++ can use its age as an excuse to be super complicated but here, the modules implementation of C++ is younger than Rust's.
If I'm understanding the post correctly, the entire problem they are facing is that you have to scan all source files to build this module->filename mapping.
None of the "essential goals" listed at the top of the blog post requires that modules be imported by namespace instead of filename, as far as I can see. So why was this design chosen when it causes these problems?
Since modules operate at the language level, they need to operate on this notion, which precludes importing by file.
OCaml or Haskell do just fine with proper modules and parallel builds. I presume the same holds for Go, rust, et. al.
Clang’s module maps solve a lot of these problems. There is a known location to find the module map and all headers from the module must be reachable from the map.
Even at Apple, which originally designed them, they just focused on making it good enough for C and Objective-C system headers.
https://www.youtube.com/watch?v=K_fTl_hIEGY&feature=youtu.be...
I agree, but that's because C++ TUs are too small. Rust works like this "dead-on-arrival" way, but survives because TUs are larger.
For example,according to the clang documentation for modules [1], they are meant to obviate the necessity of #including headers of libraries you are linking to.
When you link with libraries, you expect them to be compiled already. If the library is a dependency of your current project (in Visual Studio solution parlance), then it will be recompiled if needed before your project is compiled, if your build system is set up correctly. You will also take care to point your build system to the correct version of the library you want to link to, including versions that change depending on CPP flags etc.
I don't see how modules are any different.
Please enlighten me if I'm not fully grasping the point of the article.
Header, source like before. Allow putting all implementations in the source (including templates). Allow private member functions to be defined in the source file even though they were not declared in the class. Every function that is not declared in the header is no-external-linkage (including these adhoc private members). Defines don't leak into header file from including files unless explicitly passed into it (using some new syntax). Defines can leak out of header files. If the define already exists it's an error (the point of this is to make sure that order doesn't matter), unless the origin of the define is the same (so if x includes y and z, and y includes z, then a define in z would go into x directly and also through y which would not be an error).
This seems like it should be a strict improvement.
Do you realize how hard this is to implement? This actually was part of the original C++ spec (export keyword), but no compiler successfully implemented it in a way that was compatible with other compilers, and it was deprecated.
This (and C++ modules) requires a standard intermediate representation of the language that all compilers share, otherwise compiler A can't use a module generated by compiler B.
This is why I've never really expected C++ modules to ever exist, or if they do, it will be in a form that is much more limited than most people want or expect. Either they'll only allow a subset of the language to exist in a module, or the feature won't be much different than the "pre-compiled header" feature offered by most C++ compilers), or modules won't be portable across compilers (and maybe even versions of the same compiler).
Currently every compiler except MSVC plans to make module files version locked. Different versions of the same compiler will use different module files.
Personally I find little value in portable module files, as you need to be able to rebuild modules anyway to handle pretty much any change to compile flags.
But I have to wonder, is adding one more big feature wise? Will modules be the feature where it becomes impossible to create a compiler that's both useful amd conforming?
It seems to me that the module-interface unit (MIU) would be pretty much the equivalent of present-day header files. So for two modules foo and bar, there is no dependency on the order foo.cpp and bar.cpp are compiled, because only the MIU needs to be compiled for a given module and the design ensures two MIU are isolated. If they are mutually dependent it's a bad design, but teh solution is the same as in Java: you need to build the MIU in the same compiler invocation. (In fact, that would probably happen automatically, using the equivalent of today's -I include directory directive to find MIUs.)
Yes, that means you need to split your module into a clean MIU andthe actual implementation file, just like now you split them in a header file and a cpp file.
Yes, you need MIU to be "available" to be translated to BMI gobally, just like you need header files to be available globally when compiling.
My immediate though was create a SQLLite database per some collection of modules (a crate of modules?).
Each row in that table will have unique 'business id' and unique row_id. business Id is a composite key on
module_name+exported_function_names_with_attribute_and_result signature
every BusinesID as defined above, can point to source location, compiled object location, last compilation time, compilation state.
Moving crates to other locations will not change the BusinessID. Moving compiled object location will not change the BusinessID.
The sqllite database can have a network interface for multi-machine compilation. can be backed up/restored into a new environment and be unchanged as long as compilation flags for all the modules remained the same, and compiler versions are same.
C++ does not. The build system will also take care of this problem, just as we currently e.g. define libraries in cmake.
I didn't get why - one could imagine parallelizing BMI generation, then parallelizing "normal" compilation.
The only issue I see is that you wouldn't know which BMI you'd need, so you would need to generate all of them (or regenerate those who are out of date), or specifically list those that needs to be generated in a build tool. Given how the rest of the compilation pipeline works, is that undesirable?
I'll keep my irrelevant eyes to myself then, thanks.
It is a binary module interface not binary module implementation.
Well before reaching 300 I'd have to ask WTF are you doing? I mean really, seriously, if your dependency chain is 1/10 that deep I'd look for something wrong.
I've often thought that including headers from inside headers in C or C++ is a mistake. And that thinking is probably wrong. It makes sense when using a library that may itself have a lot of components - I just include the top-level header for the library. But even that is different from having a really deep dependency chain.
Maybe - just maybe - people have shifted the spaghetti out of their code and into the file structure.
class Foo {
};
Then if I must write bar.hpp as: #include "foo.hpp"
class Bar {
Foo f;
}
I cannot forward declare Foo because in order to size Bar, I must know the size of Foo. I must therefore include `foo.hpp` in `bar.hpp`, thus I must include headers in headers, unless my headers are not allowed to contain class definitions.This is the type of complexity that a good software "architect" should be trying to reduce rather than manage.
e.g. a blanket rule that a header file isn't allowed to include another header file is trivial to enforce, one which says it can't be more than n deep, is subject to boundary pushing.
Are there other languages _like Rust_? Have you heard of large, production code bases maintained at large successful companies that are written in Rust?
Google, Amazon, Facebook, Microsoft, Dropbox...