IT资讯 D 语言/DLang 2.099.0 发布

conrad · 2022-04-09 13:00:06 · 热度: 8

D 语言/DLang 2.099.0 已于上月发布。

公告显示,这是一个重大版本,更新亮点包括:

  • D 代码模块可通过 ImportC 被导入 C 代码中
  • 引入抛出表达式 (throw expression)
  • PE/COFF 输出现在是 DMD 在 Windows 上的默认输出
  • ……

详情查看 Changelog

使用 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());
}

详情查看 Changelog


P.S. 发稿前看到最新版本为 2.099.1。更新内容主要是修复错误,以及两项关于编译器和库的重要变更。

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册