D 语言 / DLang 2.100.0 已正式发布。此版本包含 22 个主要更改和 179 个已修复的 Bugzilla 问题。
DLang 2.100.0 主要变化
改进 C++ header gen
@mustuse
强制返回类型错误检查的新属性支持 contract 不变版本标识符
添加 .tupleof 静态数组的属性
Zlib 更新到 1.2.12
std.functional 引入新函数 bind
引入不可传递的 inout 返回,以及更多改进
使用 ImportC 在 C 源码中导入 D 代码模块
从 D 2.099.0 开始,可通过__import
关键字直接将 D 代码模块导入 C 文件。
// dsayhello.dimport core.stdc.stdio : puts;extern(C void helloImport( {
puts("Hello __import!";
}
// dhelloimport.c__import dsayhello;
__import core.stdc.stdio : puts;int main(int argc, char** argv {helloImport(;
puts("Cool, eh?";
return 0;
}
使用如下代码进行编译:
dmd dhelloimport.c dsayhello.d
此外还可导入已经通过 ImportC 编译过的 C 代码模块:
// csayhello.c__import core.stdc.stdio : puts;void helloImport( {
puts("Hello _import!";
}
// chelloimport.c__import csayhello;
__import core.stdc.stdio : puts;int main(int argc, char** argv {helloImport(;
puts("Cool, eh?";
return 0;
}
使用如下代码进行编译:
dmd chelloimport.c csayhello.c
引入抛出表达式 (throw expression
在 D 语言的生命周期中,throw
属于声明 (statement ,不能在表达式中使用,因为表达式必须有一个类型,而由于 throw
不返回值,所以没有合适的类型,这导致它不能使用以下语法。
(string err => throw new Exception(err;
只能使用如下的方案:
(string err { throw new Exception(err; }
不过从 D 2.099.0 开始,以下代码片段可通过编译:
void foo(int function( f {}void main( {
foo(( => throw new Exception(;
}