此代码与合同:ldc和gdc是否支持D语言合约?
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脏),但不LDC(0.17.1)或GDC(5.4.0 20160609)中运行。
LDC说:
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
}
编辑:用 “身体” 编译,而不是 “做”,每答案与最不发达国家成功。 GDC得到一个新的编译错误:
/usr/include/d/core/stdc/stdarg.d:48:5: error: undefined identifier __va_list_tag
alias __va_list = __va_list_tag;
注意,在目前的时间为contract programming的dlang.org文件没有提到身体,而可能过时,仍然有效,而且是必要的DMD的版本早于[未知版本]的编译器以及在版本[未知版本]之前使用dmd前端的任何版本的gdc或ldc。
ldc(0.17.5)为我工作,使用'do'而不是'body'。并且dmd(2.077.0)也可以工作,这并不奇怪。 – Eljay