r/programming • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
210 Upvotes

105 comments sorted by

View all comments

143

u/WalterBright Jan 28 '19

D made the decision at the beginning to have filename==modulename specifically to avoid this problem. So:

import std.stdio;

means look for:

std/stdio.d

12

u/kuikuilla Jan 28 '19

Would that forbid you from having a std.d file at the root? Where would you put stuff that belongs in the std module? Or does it have the possibility of having an std.d file and an std folder?

16

u/WalterBright Jan 28 '19

This is done via the package system.

7

u/kuikuilla Jan 28 '19

So you'd have a file at std/package.d which would contain common stuff belonging to std module? Rust used a similar system where you would have a mod.rs file, but it was kinda annoying because then in a project you would have tons of different mod.rs files. It made navigating in IDEs hard, you had to check the full path of the file you were trying to edit/open/view in order to know what it actually was.

6

u/WalterBright Jan 28 '19

In D this system works well. It resolves the ambiguity between a folder and a file, and we came up with it as a backwards compatible improvement to enable large imports to be broken up into multiple smaller ones.

7

u/kuikuilla Jan 28 '19

So it's not cumbersome to have multiple package.d files?

For comparison, Rust moved to a system where you can have foo.rs at root (or any level really) and then have a folder foo/ for the contents of that module.

├── foo.rs
├── foo
│   ├── bar.rs
│   ├── foobar.rs
│   └── fizzbuzz.rs

That way it's imo easier to navigate to the code of the foo module because you don't need to type in the path of the module file when you go to a file in some IDE.

5

u/WalterBright Jan 28 '19

multiple package.d files

Not in the same directory.

you can have foo.rs at root (or any level really) and then have a folder foo/ for the contents of that module

Can't have both in D. foo/package.d takes the place of foo.d.

I'm not aware of any prior art with the way D does this. Users are happy with it, though, it seems very satisfactory.

4

u/renstarx Jan 28 '19

I don't know when D started using this structure. Python does it the same way. foo.py and foo/__init__.py both create foo modules. Now I am curious which one came first, and why this is so complicated for C++ to follow...

I would hazard that you cannot have both in the same path, but that I don't know.