My idea for seamless interop to C++

Markus contact at markus-lanner.com
Thu Nov 19 09:59:03 UTC 2020


Thx for your great reply!

On Wednesday, 18 November 2020 at 23:32:00 UTC, Petar Kirov 
[ZombineDev] wrote:
> That said, I'm sorry to disappoint, but I don't think this 
> would be a a viable approach (translating D source code to C++ 
> (not just headers) while preserving the high-level structure). 
> D is also a complex language (just the complexity is much less 
> and much better managed compared to C++) and doing a 
> semantically correct translation (while even preserving 
> high-level concepts, such as OOP) would be quite hard. D's 
> template system is more powerful (and more sane) than the one 
> of C++, so you can't translate any D code that use templates 
> basically (which is pretty much the whole standard and runtime 
> library).

I completely agree! D-templates are complex, but unlike in C++ 
manageable. However I'd simply not support them :) It shall not 
be possible to instance a D-template in C++. Keep it simple and 
usable! In the end of the day, that's one thing that makes D so 
awesome. D is not C++ compatible. However I'd like D to be 
compatible with a sane simple subversion of C++. Which is the 
exchange of build-in-types.
So the interop shall ONLY support build-in types, combinations of 
them (`struct`/POD) and non-templated classes. functions and 
methods shall only take and return build-in-types/POD.
In my opinion that's all that's needed for D to work cleanly and 
safe with C++. Everything else would require "magic".
A module shall have a simple interface. That should do for 99% of 
all the cases. This way it would be easily possible to access the 
vast ecosystem of C++. The D-UseCase provides an Interface of 
what it wants, and in C++ you implement that interface to a 
library/framework of your choosing.

So in my opinion, with that subversion of D and C++ a seamless 
interop should be possible and quite helpful. Could I change your 
mind?

> I believe there are 5 approaches / areas worth investing to:
> 1. dmd's built-in extern (C/C++) ABI and header generation [1] 
> [6] [7] [8]

I really like the direction this dtoh is going. And thanks for 
the link to the upcoming version, I did not read that one before. 
I tested it, and what I didn't like is that it generates code 
like:

```cpp
virtual void setString(DArray< char > ) = 0;
virtual SomeClass* getSomeClass() = 0;
```

You get raw pointers and custom arrays. I get it why they are 
doing it like that. It's the fastest and most flexible solution.
However, it includes manual memory management, it's unsafe and 
you still have to write an error-prone glue code. In example of 
the string, I'm sure in a real world project you will cast 
`DArray<char>` to `std::string` and vice-versa. Why not do it for 
them, when generating the wrapper. Out of the box awesomness :D 
and more concentration on the problem, not on the language and 
its interfacing.

> 2. dpp [2]

Great tool, until C++ gets involved. Epic for C! no doubt.

> 3. dstep [3] (I really wish Atilla would collaborate with Jacob 
> on merging dpp and dstep)

Great tool too... for C.

> 4. cling [4] - as far as I know Symmetry have build some sort 
> of bridge between D and C++ based on this.
> 5. Calypso - using Qt from D is nothing sort of astonishing!

these are C++ to D tools? not D to C++?
Calypso really looks astonishing. will give it a try!

Have fun
Markus


More information about the Digitalmars-d mailing list