8 Star 183 Fork 8

Lucifer / Programming-in-D-in-Chinese

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
invariant.html 68.44 KB
一键复制 编辑 原始数据 按行查看 历史
Lucifer 提交于 2014-02-06 17:05 . Programming in D in English
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Contract Programming for Structs</title>
</head>
<body>
<h4 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.5em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Contract Programming for Structs and Classes</h4>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Contract programming is very effective in reducing coding errors. We have seen
two of the contract programming features earlier in<span class="Apple-converted-space">&nbsp;</span><a href="http://ddili.org/ders/d.en/contracts.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Contract Programming chapter</a>: The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
ensure input and output contracts of functions.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Note:</b><span class="Apple-converted-space">&nbsp;</span>It is very important that
you consider the guidelines under the &quot;<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>blocks
versus<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">enforce</code><span class="Apple-converted-space">&nbsp;</span>checks&quot;
section of that chapter. The examples in this chapter are based on the
assumption that problems with object and parameter consistencies are due to
programmer errors. Otherwise, you should use<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">enforce</code><span class="Apple-converted-space">&nbsp;</span>checks
inside function bodies.</i></p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As a reminder, let&#39;s write a function that calculates the area of a triangle by
Heron&#39;s formula. We will soon move the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
of this function to the constructor of a struct.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Obviously, for this calculation to work correctly, the lengths of all of the
sides of the triangle must be greater than or equal to zero. Additionally, since
it is impossible to have a triangle where one of the sides is greater than the
sum of the other two, that condition must also be checked.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Once these input conditions are satisfied, the area of the triangle would be
calculated as greater than or equal to zero. The following function ensures that
all of these requirements are satisfied:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">private</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.math;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> triangleArea(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> a, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> b, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> c)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// No side can be less than zero
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(a &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(b &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(c &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// No side can be longer than the sum of the other two
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(a &lt;= (b + c));
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(b &lt;= (a + c));
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(c &lt;= (a + b));
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">out</span> (result)
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(result &gt;= 0);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">body</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">immutable</span> halfPerimeter = (a + b + c) / 2;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> sqrt(halfPerimeter
* (halfPerimeter - a)
* (halfPerimeter - b)
* (halfPerimeter - c));
}
</pre>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
of member functions</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
can be used with member functions as well.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Let&#39;s convert the function above to a member function of a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Triangle</code>struct:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.stdio;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">import</span> std.math;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">struct</span> Triangle
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">private</span>:
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> a_;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> b_;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> c_;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">public</span>:
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> area() <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">const</span> @property
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">out</span> (result)</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(result &gt;= 0);
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">body</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">immutable</span> halfPerimeter = (a_ + b_ + c_) / 2;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">return</span> sqrt(halfPerimeter
* (halfPerimeter - a_)
* (halfPerimeter - b_)
* (halfPerimeter - c_));
}
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">void</span> main()
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> threeFourFive = Triangle(3, 4, 5);
writeln(threeFourFive.area);
}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As the sides of the triangle are now member variables, the function does not
take parameters anymore. That is why this function does not have an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>block.
Instead, it assumes that the members already have consistent values.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The consistency of objects can be ensured by the following features.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
for object consistency</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The member function above is written under the assumption that the members of
the object already have consistent values. One way of ensuring that assumption
is to define an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>block
for the constructor so that the objects are guaranteed to start their lives in
consistent states:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> Triangle
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>(<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> a, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> b, <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">double</span> c)
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">in</span></span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// No side can be less than zero
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(a &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(b &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(c &gt;= 0);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// No side can be longer than the sum of the other two
</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(a &lt;= (b + c));
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(b &lt;= (a + c));
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(c &lt;= (a + b));
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">body</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>.a = a;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>.b = b;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">this</span>.c = c;
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
This prevents creating invalid<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">Triangle</code><span class="Apple-converted-space">&nbsp;</span>objects
at run time:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> negativeSide = Triangle(-1, 1, 1);
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">auto</span> sideTooLong = Triangle(1, 1, 10);
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>block
of the constructor would prevent such invalid objects:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">core.exception.AssertError@deneme.d: Assertion failure
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Although an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>block
has not been defined for the constructor above, it is possible to define one to
ensure the consistency of members right after construction.</p>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks for object
consistency</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
of constructors guarantee that the objects start their lives in consistent
states and the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
of member functions guarantee that those functions themselves work correctly.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
However, these checks are not suitable for guaranteeing that the objects are
always in consistent states. Repeating the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
for every member function would be excessive and error-prone.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The conditions that define the consistency and validity of an object are called
the<span class="Apple-converted-space">&nbsp;</span><i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">invariants</i><span class="Apple-converted-space">&nbsp;</span>of
that object. For example, if there is a one-to-one correspondence between the
orders and the invoices of a customer class, then an invariant of that class
would be that the lengths of the order and invoice arrays would be equal. When
that condition is not satisfied for any object, then the object would be in an
inconsistent state.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As an example of an invariant, let&#39;s consider the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">School</code><span class="Apple-converted-space">&nbsp;</span>class
from<a href="http://ddili.org/ders/d.en/encapsulation.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">the
Encapsulation and Protection Attributes chapter</a>:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> School
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">private</span>:
Student[] students;
size_t femaleCount;
size_t maleCount;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The objects of that class are consistent only if an invariant that involves its
three members are satisfied. The length of the student array must be equal to
the sum of the female and male students:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(students.length == (femaleCount + maleCount));
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
If that condition is ever false, then there must be a bug in the implementation
of this class.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks are for
guaranteeing the invariants of user-defined types.<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks
are defined inside the body of a<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">struct</code><span class="Apple-converted-space">&nbsp;</span>or
a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">class</code>.
They contain<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">assert</code><span class="Apple-converted-space">&nbsp;</span>checks
similar to<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code>blocks:</p>
<pre class="d_code" style="margin: 0.5em 1em 0.75em 16px; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 1em; vertical-align: baseline; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">class</span> School
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">private</span>:
Student[] students;
size_t femaleCount;
size_t maleCount;
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(255, 255, 136); background-position: initial initial; background-repeat: initial initial;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">invariant</span>()</span>
{
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: blue; background-position: initial initial; background-repeat: initial initial;">assert</span>(students.length == (femaleCount + maleCount));
}
<span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; background-color: transparent; color: green; background-position: initial initial; background-repeat: initial initial;">// ...
</span>}
</pre>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
There can be only one<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">invariant()</code><span class="Apple-converted-space">&nbsp;</span>block
in a user-defined type.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
The<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">invariant()</code><span class="Apple-converted-space">&nbsp;</span>block
is executed automatically at the following times:</p>
<ul style="margin: 0.5em 1em 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
After the execution of the constructor: This guarantees that every object
starts its life in a consistent state.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Before the execution of the destructor: This guarantees that the destructor
will be executed on a consistent object.</li>
<li style="margin: 0.5em 0px 0px 1em; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Before and after the execution of a<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">public</code><span class="Apple-converted-space">&nbsp;</span>member
function: This guarantees that the member functions do not invalidate the
consistency of objects.<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); background-position: initial initial; background-repeat: initial initial;">
<i style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
<b style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">
Note:</b><span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">export</code><span class="Apple-converted-space">&nbsp;</span>functions
are the same as<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">public</code><span class="Apple-converted-space">&nbsp;</span>functions
in this regard. (Very briefly,<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">export</code><span class="Apple-converted-space">&nbsp;</span>functions
are functions that are exported on dynamic library interfaces.)</i></p>
</li>
</ul>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
If the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">assert</code><span class="Apple-converted-space">&nbsp;</span>checks
inside<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks
fail, an<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">AssertError</code>is
thrown. This ensures that the program does not continue running with invalid
objects.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
As with<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks,
the checks inside<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks
can be disabled by the<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">-release</code><span class="Apple-converted-space">&nbsp;</span>command
line option:</p>
<pre class="shell" style="margin: 0.5em 1em 1em; padding: 0.5em; border: 1px dotted rgb(153, 204, 153); outline: 0px; font-size: 13px; vertical-align: baseline; background-color: rgb(224, 224, 224); font-weight: bold; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">dmd deneme.d -w -release
</pre>
<h5 style="margin: 1.25em 0.5em 0.5em 8px; padding: 0px; border: 0px; outline: 0px; font-size: 1.25em; vertical-align: baseline; background-color: transparent; font-family: sans-serif; font-weight: bold; color: rgb(0, 0, 51); font-style: normal; font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
Summary</h5>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
in</code><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">out</code><span class="Apple-converted-space">&nbsp;</span>blocks
are useful in constructors as well. They ensure that objects are constructed in
valid states.</p>
<p style="margin: 0.75em 1em 0.75em 16px; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 19px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
<code class="d_inline" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 1.2em; vertical-align: baseline; background-color: transparent; font-weight: bold; color: rgb(0, 34, 34); background-position: initial initial; background-repeat: initial initial;">
invariant()</code><span class="Apple-converted-space">&nbsp;</span>blocks ensure that
objects remain in valid states throughout their lifetimes.</p>
<div id="ders_nav_son" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 0.9em; vertical-align: baseline; background-color: transparent; float: right; width: 190px; text-align: center; color: rgb(0, 0, 0); font-family: serif; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;">
[<span class="Apple-converted-space">&nbsp;</span><a href="property.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">Prev&nbsp;</a>]
&nbsp; [<span class="Apple-converted-space">&nbsp;</span><a href="templates.html" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; color: purple; text-decoration: none; background-position: initial initial; background-repeat: initial initial;">Next&nbsp;</a>]</div>
</body>
</html>
D
1
https://gitee.com/lucifer2031/Programming-in-D-in-Chinese.git
git@gitee.com:lucifer2031/Programming-in-D-in-Chinese.git
lucifer2031
Programming-in-D-in-Chinese
Programming-in-D-in-Chinese
master

搜索帮助