Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
D (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Interaction with other systems=== [[C (programming language)|C]]'s [[application binary interface|application binary interface (ABI)]] is supported, as well as all of C's fundamental and derived types, enabling direct access to existing C code and libraries. D [[Language binding|bindings]] are available for many popular C libraries. Additionally, C's standard [[library (computer science)|library]] is part of standard D. On Microsoft Windows, D can access [[Component Object Model]] (COM) code. As long as memory management is properly taken care of, many other languages can be mixed with D in a single binary. For example, the GDC compiler allows to link and intermix C, C++, and other supported language codes such as Objective-C. D code (functions) can also be marked as using C, C++, Pascal ABIs, and thus be passed to the libraries written in these languages as [[Callback (computer programming)|callbacks]]. Similarly data can be interchanged between the codes written in these languages in both ways. This usually restricts use to primitive types, pointers, some forms of arrays, [[union type|unions]], structs, and only some types of function pointers. Because many other programming languages often provide the C API for writing extensions or running the interpreter of the languages, D can interface directly with these languages as well, using standard C bindings (with a thin D interface file). For example, there are bi-directional bindings for languages like [[Python (programming language)|Python]],<ref>{{cite web |title=PyD |website=[[GitHub]] |url=https://github.com/ariovistus/pyd |access-date=2020-05-07 |date=7 May 2020}}</ref> [[Lua (programming language)|Lua]]<ref>{{cite web |last1=Parker |first1=Mike |title=Package derelict-lua on DUB |url=https://code.dlang.org/packages/derelict-lua |website=DUB Package Registry |access-date=2020-05-07}}</ref><ref>{{cite web |last1=Parker |first1=Mike |title=Package bindbc-lua on DUB |url=https://code.dlang.org/packages/bindbc-lua |website=DUB Package Registry |access-date=2020-05-07}}</ref> and other languages, often using compile-time code generation and compile-time type reflection methods. ====Interaction with C++ code==== For D code marked as <code>extern(C++)</code>, the following features are specified: * The name mangling conventions shall match those of C++ on the target. * For function calls, the ABI shall be equivalent. * The vtable shall be matched up to single inheritance (the only level supported by the D language specification). C++ namespaces are used via the syntax <code>extern(C++, namespace)</code> where ''namespace'' is the name of the C++ namespace. =====An example of C++ interoperation===== '''The C++ side''' <syntaxhighlight lang="C++"> import std; class Base { public: virtual void print3i(int a, int b, int c) = 0; }; class Derived : public Base { public: int field; Derived(int field): field(field) {} void print3i(int a, int b, int c) { std::println("a = {}", a); std::println("b = {}", b); std::println("c = {}", c); } int mul(int factor); }; int Derived::mul(int factor) { return field * factor; } Derived* createInstance(int i) { return new Derived(i); } void deleteInstance(Derived*& d) { delete d; d = 0; } </syntaxhighlight> '''The D side''' <syntaxhighlight lang="D"> extern(C++) { abstract class Base { void print3i(int a, int b, int c); } class Derived : Base { int field; @disable this(); override void print3i(int a, int b, int c); final int mul(int factor); } Derived createInstance(int i); void deleteInstance(ref Derived d); } void main() { import std.stdio; auto d1 = createInstance(5); writeln(d1.field); writeln(d1.mul(4)); Base b1 = d1; b1.print3i(1, 2, 3); deleteInstance(d1); assert(d1 is null); auto d2 = createInstance(42); writeln(d2.field); deleteInstance(d2); assert(d2 is null); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)