D 语言 / DLang 2.100.0 已正式发布。此版本包含 22 个主要更改和 179 个已修复的 Bugzilla 问题。
主要变化
- 改进 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.d
import 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());
}
https://github.com/topics/rust 31,679 public repositories
https://github.com/topics/go 41,908 public repositories
https://github.com/topics/java 165,377 public repositories
https://github.com/topics/python 278,484 public repositories
https://github.com/topics/javascript 310,507 public repositories
https://github.com/search?q=language:D&type=Repositories 8,274 available repository
https://github.com/search?q=language:rust&type=Repositories 54,151 available repository
https://github.com/search?q=language:go&type=Repositories 292,828 available repository
https://github.com/search?q=language:python&type=Repositories 2,126,662 available repository
https://github.com/search?q=language:java&type=Repositories 4,262,095 available repository
https://github.com/search?q=language:javascript&type=Repositories 4,303,023 available repository
生态上差距蛮大的
https://pan.baidu.com/s/1J_QcWH4as8_CuNWVylB7yw?pwd=3rgi