News for hackers. Stuff that matters.
Re:D Language accepted for inclusion in GCC by iamNumber4 on Wednesday June 21, 2017 @10:46AM (#14604212)
One of D's goals is to be ABI compatible with C and C++, ABI compatibility with C is 100% as I understand it. C++ is a work in progress.

What is ABI compatiblity? in a nutshell it let's you use libraries from another programming language. In D, all you need is a D file that describes the C/C++ library as a wrapper so the D compiler knows what you are using.

This means D will build upon everything that already exists for C/C++. You can intermix C/C++ and D in the same project if you so choose to. Which is really a big deal because you don't necessarily have to stop and rewrite years of code you might already have to start using D.

Syntax wise D is a kin to C#/Java, so if you know C# or Java, jumping over to using D is just a matter of learning the language specific Gotchas and core libraries. So the learning curve is quite low if you know C/C++,C#, or Java.

to see what D libraries are out there I suggest looking at http://code.dlang.org/

Re:D Language accepted for inclusion in GCC by copx on Wednesday June 21, 2017 @11:00AM (#14604348)
How would that even work? Modern C++ libraries are largely templates, templates which must be instantiated at compile-time i.e. you need a full C++ compiler to use C++ libraries directly.

Said compiler must somehow work together with the D compiler here, which should lead to god-awful memory use, compilation time, and error messages.

Re:D Language accepted for inclusion in GCC by WalterBright on Wednesday June 21, 2017 @05:57PM (#14608311)
Yes, it is amazing that D can work with C++ templates! I gave a talk on how it is done here:

https://www.youtube.com/watch?v=IkwaV6k6BmM

Re:D Language accepted for inclusion in GCC by _yosefk on Wednesday June 21, 2017 @11:17AM (#14604517)
Most useful C++ code is either not templates or it's templates which can be instantiated a finite number of times and then used like non-templated code. Most "header-only"/header-heavy C++ libraries which present the problem you mention solve problems like C++'s smart for loop working like "for x in xs" does in Python but not working like "for x,y in zip(xs,ys)" does or like "for i,x for enumerate(xs)" does etc.; you don't really need these outside C++ (whether you need them inside C++ is another question but it's irrelevant here.)
Re:D Language accepted for inclusion in GCC by CJefferson on Wednesday June 21, 2017 @11:27AM (#14604599)
That sounds ..wrong. what about the C++ standard library? Neither vector or sort sounds like that?
Re:D Language accepted for inclusion in GCC by yorwba on Wednesday June 21, 2017 @11:37AM (#14604700)
Those > can be instantiated a finite number of times and then used like non-templated code.

So you have a C++ file that uses vector<int>, vector<struct c_struct>, vector<CppClass>, vector<DClass>, etc. once and then you can link to them from D code.

The only case where that doesn't work is when you have lots of one-off instantiations, where requiring an instantiation in C++ code for each use in D would get annoying quickly.

Re:D Language accepted for inclusion in GCC by amelius on Wednesday June 21, 2017 @12:13PM (#14605054)
Does the D compiler know how to mangle/demangle C++ names?

Do you have to write some kind of D header file for foreign C++ functions? Otherwise, how would D even know about the existence of e.g. vector<int>::push?

Re:D Language accepted for inclusion in GCC by biotronic on Wednesday June 21, 2017 @12:24PM (#14605186)
https://dlang.org/spec/cpp_interface.html#cpp-templates

You will need to recreate the C++ function definitions in D and mark them extern(C++), but that's about it.

Re:D Language accepted for inclusion in GCC by pzone on Wednesday June 21, 2017 @01:20PM (#14605858)
Those parts of the C++ standard library are header-only and will be instantiated with concrete types at compile-time.
Re:D Language accepted for inclusion in GCC by iamNumber4 on Wednesday June 21, 2017 @12:16PM (#14605099)
You should probably read up on ABI Compatibility, and, Compiling vs linking as well.

Let's be clear a compiler builds a object files that have to be linked to create a executable binary. IE compiler makes .o files. Linker takes output object files from the compiler an puts them (.o,.so/.dll,.a) together, to make .exe files.

This all works because you are linking to the C/C++ library binaries (the .o, .so, .a, .dll) files generated by a C/C++ compiling/linking. In most cases this is shared object library/dll provided by the operating system or pre-built library you install on the system. There is no need to compile it because it is pre-built. if you do compile the library yourself, it is a one time thing.

In D you create a .d file that describes what is in a C++ library .o/.so/.a/.dll file. So that upon linking, you take your D code's .o files + the C/C++ .o files to put together a full executable binary.

Re:D Language accepted for inclusion in GCC by enzo1982 on Wednesday June 21, 2017 @12:37PM (#14605329)
That's not how C++ templates work. You cannot deliver them as binaries.

Templates are delivered as C++ include files (i.e. source code). When you use them, the compiler fills in the types you want and compiles the result.

For example, there's the std::vector<> template. You can fill in any type that meets the specific requirements of the template and use it like std::vector<int> or std::vector<YourC++ClassType>.

However, you probably won't be able to do std::vector<YourDClassType>. ABI compatibility is not enough to do this as you would be effectively mixing C++ and D source code here.

Re:D Language accepted for inclusion in GCC by jpf91 on Wednesday June 21, 2017 @12:56PM (#14605581)
> However, you probably won't be able to do std::vector<YourDClassType>. ABI compatibility is not enough to do this as you would be effectively mixing C++ and D source code here.

I think this should be working. You don't have to 'mix source code', you can implement YourDClassType in D and use it in C++. You simply have to write a C++ header describing the class. Then write a D file describing the external C++ sdt::vector<YourDClassType> file and link everything together.

Not sure if anybody actively tested this though ;-)

Re:D Language accepted for inclusion in GCC by iamNumber4 on Wednesday June 21, 2017 @02:06PM (#14606359)
As mentioned before, C++ compatibility is not 100% yet. My comments are purely what works now; which by describing to D what is in a c/c++ .o file so you can link it in. In your .d file that describes the C/C++ you want to use, sometimes you will have to describe the template to D by creating D template of the C++ template.

Also if your "library" is all templates, it's not a library, it's a framework that uses other libraries. There still has to be at some point tangible code that is compiled to an object file to be linked. You will have to find all of the dependancies and make sure that they are also described in a .d file.

This might mean you have a bunch of work that you need to do in order use the c++ code you want to bring in into D. But then again, if it's a public/open source library, and you go down that rabbit hole and make it work, you just made D even better.

please see https://dlang.org/spec/cpp_interface.html#cpp-templates for more details.

Re:D Language accepted for inclusion in GCC by biotronic on Thursday June 22, 2017 @02:21AM (#14610429)
> if your "library" is all templates, it's not a library

TIL STL is not a library.

Re:D Language accepted for inclusion in GCC by danieldk on Wednesday June 21, 2017 @12:34PM (#14605301)
I think you misunderstood your parent. A lot of modern C++ 'libraries' are header-only, because they heavily use templates. There is no object code or library to link against until you instantiate templates.
Re:D Language accepted for inclusion in GCC by johncolanduoni on Wednesday June 21, 2017 @12:34PM (#14605297)
That's not really relevant to their question. Many templates are implemented via inlining/instantiation in client code by default, and as such they won't be present in the output of the C++ compiler.
Re:D Language accepted for inclusion in GCC by jjnoakes on Wednesday June 21, 2017 @11:09AM (#14604438)
Re:D Language accepted for inclusion in GCC by seren on Wednesday June 21, 2017 @12:11PM (#14605033)
> D understands how C++ function names are "mangled" and the correct C++ function call/return sequence.

How does it work in practice ? C++ name mangling is not standardized and so every compiler can implement its own scheme. So does it mean there is a list of D compatible c++ compiler somewhere ? I have the feeling that if you are using some exotic proprietary c++ compiler it won't work well.

Re:D Language accepted for inclusion in GCC by kbenson on Wednesday June 21, 2017 @12:21PM (#14605156)
There was some interesting discussion regarding this here recently in the comments regarding a Rust demangling library[1].

1: https://news.ycombinator.com/item?id=13706799

Re:D Language accepted for inclusion in GCC by kryptiskt on Wednesday June 21, 2017 @12:17PM (#14605109)
In practice there are two dominating standards nowadays, the Itanium ABI and Microsoft's, so it's quite tractable to handle the common cases.
Re:D Language accepted for inclusion in GCC by seren on Wednesday June 21, 2017 @12:31PM (#14605266)
Thanks, I might be biased because I have a legacy platform still using gcc 2.7, which is older than the adoption of the Itanium ABI.
Re:D Language accepted for inclusion in GCC by WalterBright on Wednesday June 21, 2017 @06:00PM (#14608327)
> C++ name mangling is not standardized and so every compiler can implement its own scheme.

That's right, and the D compiler deals with that by customizing the C++ mangling for every platform.

Re:D Language accepted for inclusion in GCC by seren on Thursday June 22, 2017 @03:49AM (#14610756)
I am just curious what the actual list is, it is surely not possible to support every version of every compiler on every platform.
Re:D Language accepted for inclusion in GCC by WalterBright on Thursday June 22, 2017 @04:16AM (#14610838)
We don't. We pick one.
Re:D Language accepted for inclusion in GCC by penpapersw on Wednesday June 21, 2017 @11:05AM (#14604389)
I assume the idea is to be able to reuse libraries like Box2d etc where it's mostly concrete code, and that other libraries where it's mostly templates kind of just have to be rewritten.
Re:D Language accepted for inclusion in GCC by qznc on Wednesday June 21, 2017 @12:32PM (#14605285)
You can port some of the templates to D, since it also has templates. Although I'm not sure if they work the same in all pathological cases.
Re:D Language accepted for inclusion in GCC by Zardoz84 on Wednesday June 21, 2017 @01:52PM (#14606216)
any thing that you can do with C++ templates, could be done with D templates + CTFE plus would be far more easy to read and understand that C++ templates.
Re:D Language accepted for inclusion in GCC by mhh__ on Wednesday June 21, 2017 @01:45PM (#14606139)
No it does not. C++ templates mean matching the name mangling. That already works. Catching C++ exceptions is being worked on now.