此代码与合同:
import std.stdio;
int TestContract(int a)
in
{
assert( a > 0);
}
do
{
return a + 1;
}
int main(string[] args)
{
auto a = 2;
try
{
writeln(a," + 1 is ",TestContract(a));
a = -2;
writeln(a," + 1 is ",TestContract(a));
}
catch (Exception e)
{
writeln(e);
}
return 0;
}
使用 dmd (v2.076.0-dirty) 编译和运行,但不是 ldc (0.17.1) 或 gdc (5.4.0 20160609)。
最不发达国家 说:
contracts.d(12): Error: declaration expected, not 'do'
contracts.d(15): Error: unrecognized declaration
和 gdc 说:
contracts.d:12:1: error: declaration expected, not 'do'
do
^
contracts.d:15:1: error: unrecognized declaration
}
编辑:使用“body”而不是“do”进行编译,根据 ldc 的答案成功。gdc 得到一个新的编译错误:
/usr/include/d/core/stdc/stdarg.d:48:5: error: undefined identifier __va_list_tag
alias __va_list = __va_list_tag;
请注意,目前合同编程的 dlang.org 文档没有提到body,虽然可能已弃用,但仍然有效,并且对于早于 [未知版本] 的 dmd 编译器版本和任何 gdc 或 ldc 版本是必需的在版本 [未知版本] 之前使用 dmd 前端。