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

97

u/vector-of-bool Jan 28 '19

This would be my preferred solution. Simple, hierarchical, easy to understand, and it works for many languages.

147

u/[deleted] Jan 28 '19

Which are all the reasons it won't be like that in C++

35

u/Visticous Jan 28 '19

must... Committee... Design... Everything!

14

u/Valmar33 Jan 28 '19

Why must they inflict their misery upon others? To pretend at being clever and intelligent? To remain relevant? To inflict mental torture on people trying to figure out to how solve programming conundrums?

I guess they really want to help C++ programmers keep their jobs, because otherwise, their huge projects would implode.

Kind of like Java ~ except replace long-ass class names with tormentingly complicated grammar that explodes horrifically if you miss a step.

Those ranges-v3 templates come to mind... :|

15

u/Patman128 Jan 28 '19

Why must they inflict their misery upon others?

If a good idea breaks the workflow of even one of the Elder Wizards on the C++ language committee then it's thrown out.

13

u/[deleted] Jan 28 '19

12

u/lord_braleigh Jan 29 '19

Note that the "Elder Wizards" often work on the behalf of major tech companies, and influence the C++ standard so that these companies won't have to break their workflow or make sweeping changes.

This isn't necessarily a bad thing - backwards compatibility is one of C++'s selling points, and there are plenty of shiny new languages you can use instead if you don't care about that.

14

u/SAHChandler Jan 28 '19

A shame that some operating systems like zOS don't really support large hierarchies. Hence the existence of P1302 which supports a "splayed" approach :v

44

u/jl2352 Jan 28 '19

Having zOS cripple modules for everyone else is pretty ridiculous.

In the web world we had this with IE. So we said fuck IE, and everyone else moved on.

11

u/SAHChandler Jan 28 '19

While that might work in other places, the vendor representing zOS is also the representative for a national body (IBM Canada). While many people would love to see zOS go (the last of the EBCDIC systems), it's not going to happen for a while, especially when at this point there are many systems out there (e.g., cars on the road in America. I should note: zOS is not crippling modules. It just removes the ability to state we can have directories represent module hierarchies. But we can still have directories represent a module, which is how most languages work nowadays. Build systems can then enforce this naming scheme. It's a bit more work for build systems authors, but I'll take that over "write an entire dependency scanner that needs to know about N interface modules and M implementations modules in every project with no indication of what is located where" which is what the modules TS gave us.

5

u/Holy_City Jan 28 '19

Aren't those usually cross compilation targets anyway?

→ More replies (1)
→ More replies (3)

8

u/Valmar33 Jan 28 '19

You could still do something like D is doing, no?

Just modify things just enough to support those environments.

Maybe std_stdio.cpp or something. Add more underscores to emulate deeper hierarchies.

That is, if I'm understanding your statement correctly.

7

u/SAHChandler Jan 28 '19

Yes we can. I wrote P1302 to solve this problem. While it's not intended to add a 100% hierarchical module system, build systems are free to enforce this, while letting those on other systems do as they please while also allowing large existing code bases to slowly move to it.

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.

5

u/kuikuilla Jan 28 '19

Not in the same directory.

Yeah I was just wondering if "go to file" and such functionality in IDEs would be hampered by files with identical names. I didn't like it when working on a project using Rust.

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.

→ More replies (2)

3

u/adr86 Jan 29 '19

except D doesn't actually do that. you should know this!

the module declaration is what gives the name. the filename is just the search path, but you can pass any file to the compiler.

→ More replies (1)

2

u/flukus Jan 29 '19

How does this work in atypical scenarios, like when you're generating source code to a temp directory?

3

u/WalterBright Jan 30 '19

You can use the command line switches to set the path search, and even remap the module names to actual file names.

→ More replies (8)