programmeur aihser
D_DOC
最終更新:
匿名ユーザー
-
view
契約プログラミング Design of Contract
#! こめんと?!
import std.stdio;
class A
{
public int month;
// コンストラクタ
public this(int m)
{
month = m;
}
// クラス不変条件
invariant
{
assert(1 <= month && month <= 12);
}
};
class B : A
{
public this()
{
super(3);
}
public void f()
{
writefln("B::f() called");
}
}
int f(int a)
// 事前条件
in
{
assert(a >= 0);
}
// 事後条件
out (result)
{
assert(a < 0);
}
// コード
body
{
writefln(a);
a = -a; // コメントアウトすると、事後条件にひっかかる
return a;
}
void main(char[][] args)
{
writefln("Hello World, Reloaded");
f(3);
B b;
//b.f();
//assert(b);
//b.month = 0; // クラス不変条件にひっかかる。
// 正常にスコープを抜けるとき
scope(success)
{
writefln("Scope Success");
}
// 異常にスコープを抜けるとき
scope(failure)
{
writefln("Scope Failure");
}
// スコープを抜けるとき(異常でも正常でも関係なし)
scope(exit)
{
writefln("Scope Exit");
}
}