{"id":1611,"date":"2018-07-04T14:51:33","date_gmt":"2018-07-04T14:51:33","guid":{"rendered":"http:\/\/dlang.org\/blog\/?p=1611"},"modified":"2021-10-08T11:01:45","modified_gmt":"2021-10-08T11:01:45","slug":"dmd-2-081-0-released","status":"publish","type":"post","link":"https:\/\/dlang.org\/blog\/2018\/07\/04\/dmd-2-081-0-released\/","title":{"rendered":"DMD 2.081.0 Released"},"content":{"rendered":"<p><a href=\"https:\/\/dlang.org\/download.html\">DMD 2.081.0 is now ready for download<\/a>. Things that stand out in this release are a few deprecations, the implementation of a recently approved DIP (D Improvement Proposal), and quite a bit of work on C++ compatibility. Be sure to <a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html\">check the changelog for details<\/a>.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-180\" src=\"http:\/\/dlang.org\/blog\/wp-content\/uploads\/2016\/08\/d3.png\" alt=\"\" width=\"160\" height=\"301\" srcset=\"https:\/\/dlang.org\/blog\/wp-content\/uploads\/2016\/08\/d3.png 160w, https:\/\/dlang.org\/blog\/wp-content\/uploads\/2016\/08\/d3-159x300.png 159w\" sizes=\"(max-width: 160px) 100vw, 160px\" \/><\/p>\n<h2 id=\"improvingcinteroperability\">Improving C++ interoperability<\/h2>\n<p>D has had binary compatibility with C from the beginning not only because it made sense, but also because it was relatively easy to implement. C++ is a different beast. Its larger-than-C feature set and the differences between it and D introduce complexities that make implementing binary compatibility across all supported platforms a challenge to get right. Because of this, D\u2019s <code>extern(C++)<\/code> feature has been considered a work in progress since its initial inception.<\/p>\n<p><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#extern_cpp_overhaul\">DMD 2.081.0 brings several improvements<\/a> to the D &lt;-&gt; C++ story, mostly in the form of name mangling bug fixes and improvements. The mangling of constructors and destructors in <code>extern(C++)<\/code> now properly match the C++ side, as does that of most of D\u2019s operator overloads (where they are semantically equivalent to C++).<\/p>\n<p>Proper mangling of <code>nullptr_t<\/code> is implemented now as well. On the D side, use <code>typeof(null)<\/code>:<\/p>\n<pre class=\"prettyprint lang-d\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">alias nullptr_t = typeof(null);\r\nextern(C++) void fun(nullptr_t);<\/pre>\n<p>The alias in the example is not required, but may help with usability and readability when interfacing with C++. As typing <code>null<\/code> everywhere is likely reflexive for most D programmers, <code>nullptr_t<\/code> may be easier to keep in mind than <code>typeof(null)<\/code> for those special C++ cases.<\/p>\n<p>Most of the D operator overloads in an <code>extern(C++)<\/code> class will now correctly mangle. This means it\u2019s now possible to bind to operator overloads in C++ classes using the standard D <code>opBinary<\/code>, <code>opUnary<\/code>, etc. The exceptions are <code>opCmp<\/code>, which has no compatible C++ implementation, and the C++ <code>operator!<\/code>, which has no compatible D implementation.<\/p>\n<p>In addition to name mangling improvements, a nasty bug where <code>extern(C++)<\/code> destructors were being placed incorrectly in the C++ virtual table has been fixed, and <code>extern(C++)<\/code> constructors and destructors now semantically match C++. This means mixed-language class hierarchies are now possible and you can now pass <code>extern(C++)<\/code> classes to <code>object.destroy<\/code> when you\u2019re done with them.<\/p>\n<p>Indirectly related,\u00a0\u00a0<code>__traits(getLinkage, ...)<\/code>\u00a0has been updated to now tell you the ABI with which a <code>struct<\/code>, <code>class<\/code>, or <code>interface<\/code> has been declared, so you can now filter out your <code>extern(C++)<\/code> aggregates from those which are\u00a0<code>extern(D)<\/code> and <code>extern(Objective-C)<\/code>.<\/p>\n<p>The following shows some of the new features in action. First, the C++ class:<\/p>\n<pre class=\"prettyprint lang-c_cpp\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">#include &lt;iostream&gt;\r\nclass CClass {\r\nprivate:\r\n    int _val;\r\npublic:\r\n    CClass(int v) : _val(v) {}\r\n    virtual ~CClass() { std::cout &lt;&lt; \"Goodbye #\" &lt;&lt; _val &lt;&lt; std::endl; }\r\n    virtual int getVal() { return _val; }\r\n    CClass* operator+(CClass const * const rhs);\r\n};\r\n\r\nCClass* CClass::operator+(CClass const * const rhs) {\r\n    return new CClass(_val + rhs-&gt;_val);\r\n}<\/pre>\n<p>And now the D side:<\/p>\n<pre class=\"prettyprint lang-d\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">extern(C++) class CClass\r\n{\r\n    private int _val;\r\n    this(int);\r\n    ~this();\r\n    int getVal();\r\n    CClass opBinary(string op : \"+\")(const CClass foo);\r\n}\r\n\r\nclass DClass : CClass\r\n{\r\n    this(int v)\r\n    {\r\n        super(v);\r\n    }\r\n    ~this() {}\r\n    override extern(C++) int getVal() { return super.getVal() + 10; }\r\n}\r\n\r\nvoid main()\r\n{\r\n    import std.stdio : writeln;\r\n\r\n    writeln(\"CClass linkage: \", __traits(getLinkage, CClass));\r\n    writeln(\"DClass linkage: \", __traits(getLinkage, DClass));\r\n\r\n    DClass clazz1 = new DClass(5);\r\n    scope(exit) destroy(clazz1);\r\n    writeln(\"clazz1._val: \", clazz1.getVal());\r\n\r\n    DClass clazz2 = new DClass(6);\r\n    scope(exit) destroy(clazz2);\r\n    writeln(\"clazz2._val: \", clazz2.getVal());\r\n\r\n    CClass clazz3 = clazz1 + clazz2;\r\n    scope(exit) destroy(clazz3);\r\n    writeln(\"clazz3._val: \", clazz3.getVal);\r\n}<\/pre>\n<p>Compile the C++ class to an object file with your C++ compiler, then pass the object file to DMD on the command line with the D source module and Bob\u2019s your uncle (just make sure on Windows to pass <code>-m64<\/code> or <code>-m32mscoff<\/code> to <code>dmd<\/code> if you compile the C++ file with the 64-bit or 32-bit Microsoft Build Tools, respectively).<\/p>\n<p>This is still a work in progress and users diving into the deep end with C++ and D are bound to hit shallow spots more frequently than they would like, but this release marks a major leap forward in C++ interoperability.<\/p>\n<h2 id=\"dip1009\">DIP 1009<\/h2>\n<p>Given <a href=\"https:\/\/github.com\/dlang\/DIPs\/blob\/master\/DIPs\/accepted\/DIP1009.md\">the amount of time DIP 1009 spent<\/a> crawling through the DIP review process, it was a big relief for all involved when it was finally approved. The DIP proposed a new syntax for contracts in D. For the uninitiated, the old syntax looked like this:<\/p>\n<pre class=\"prettyprint lang-d\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">int fun(ref int a, int b)\r\nin\r\n{\r\n    \/\/ Preconditions\r\n    assert(a &gt; 0);\r\n    assert(b &gt;= 0, \"b cannot be negative\");\r\n}\r\nout(result) \/\/ (result) is optional if you don't need to test it\r\n{\r\n    \/\/ Postconditions\r\n    assert(result &gt; 0, \"returned result must be positive\");\r\n    assert(a != 0);\r\n}\r\ndo\r\n{\r\n \t\/\/ The function body\r\n    a += b;\r\n    return b * 100;\r\n}<\/pre>\n<p>Thanks to DIP 1009, starting in DMD 2.081.0 you can do all of that more concisely with <a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#expression-based_contract_syntax\">the new expression-based contract syntax<\/a>:<\/p>\n<pre class=\"prettyprint lang-javascript\" data-start-line=\"1\" data-visibility=\"visible\" data-highlight=\"\" data-caption=\"\">int fun(ref int a, int b)\r\n    in(a &gt; 0)\r\n    in(b &gt;= 0, \"b cannot be negative\")\r\n    out(result; result &gt; 0, \"returned result must be positive\")\r\n    out(; a != 0)\r\n{\r\n    a += b;\r\n    return b * 100;\r\n}<\/pre>\n<p>Note that <code>result<\/code> is optional in both the old and new <code>out<\/code> contract syntaxes and can be given any name. Also note that the old syntax will continue to work.<\/p>\n<h2 id=\"deprecations\">Deprecations<\/h2>\n<p>There\u2019s not much information to add here beyond <a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html\">what\u2019s already in the changelog<\/a>, but these are cases that users should be aware of:<\/p>\n<ul>\n<li>the deprecation period for the <code>std.c<\/code> package in Phobos, which was long ago superseded by the <code>core.stdc<\/code> package in DRuntime, has ended and the package has finally been removed.<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#deprecate_ctor_in_static_block\">declaring constructors in a static block<\/a> is deprecated<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#deprecate_this_super_as_types\">usage of <code>this<\/code> and <code>super<\/code> as types<\/a> is deprecated \u2013 use <code>typeof(super)<\/code> and <code>typeof(this)<\/code> instead.<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#implicit_catch_error\">implicit catch statements<\/a> will now result in a compiler error.<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#implicit_enum_comparison_error\">implicit comparison of different enums<\/a> will now result in a compiler error.<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#implicit_string_concatenation_error\">implicit string concatenation<\/a> will now result in a compiler error.<\/li>\n<li><a href=\"https:\/\/dlang.org\/changelog\/2.081.0.html#redundant_stc_error\">specifying redundant storage classes<\/a> will now result in a compiler error.<\/li>\n<\/ul>\n<h2 id=\"thedbugfixcampaign\">The #dbugfix campaign<\/h2>\n<p><a href=\"https:\/\/dlang.org\/blog\/2018\/05\/14\/the-dbugfix-campaign-round-1-report\/\">The inaugural #dbugfix round<\/a> prior to the release of DMD 2.080 was a success, but Round 2 has been much, much quieter (few nominations, very little discussion, and no votes).<\/p>\n<p>One of the two nominated bugs selected from Round 1 was <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=18068\">issue #18068<\/a>. It was fixed and merged into the new 2.081.0 release. The second bug selected was <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=15984\">issue #15984<\/a>, which has not yet been fixed.<\/p>\n<p>In Round 2, the following bugs were nominated with one vote each:<\/p>\n<ul>\n<li>17712 &#8211; <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=17712\">Undefined reference to std.conv.toChars!(10, char, 1, uint).toChars(uint)<\/a><\/li>\n<li>18026 &#8211; <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=18026\">Stack overflow in ddmd\/dtemplate.d:6241, TemplateInstance::needsCodegen()<\/a><\/li>\n<li>18906 &#8211; <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=18906\">Template specialisations should not be stripped if they\u2019re not called<\/a><\/li>\n<li>18954 &#8211; <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=18954\">extern(C++) ICE when <code>cppmangle<\/code> is used<\/a><\/li>\n<li>18955 &#8211; <a href=\"https:\/\/issues.dlang.org\/show_bug.cgi?id=18955\">extern(C++) default struct mangling is overridden when interacting with a <code>cppmangle = class<\/code> template<\/a><\/li>\n<\/ul>\n<p>I\u2019ll hand this list off to our team of bug fixing volunteers and hope there\u2019s something here they can tackle.<\/p>\n<p>Round 3 of the #dbugfix campaign is on now. Please nominate the bugs you want to see fixed! <a href=\"https:\/\/forum.dlang.org\/group\/general\">Create a thread in the General Forum<\/a> with #dbugfix and the issue number in the title, or send out a tweet containing #dbugfix and the issue number. I\u2019ll tally them up at the end of the cycle (September 28).<\/p>\n<p>And please, if you do use the #dbugfix in a tweet, remember that it\u2019s intended for nominating bugs you want fixed and not for bringing attention to your pull requests!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DMD 2.081.0 is now ready for download. Things that stand out in this release are a few deprecations, the implementation of a recently approved DIP (D Improvement Proposal), and quite a bit of work on C++ compatibility. Read all about it!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[35,26,12,6,27],"tags":[],"_links":{"self":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1611"}],"collection":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":16,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"predecessor-version":[{"id":1675,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/posts\/1611\/revisions\/1675"}],"wp:attachment":[{"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dlang.org\/blog\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}