Essential Pascal Chapter 2 and 3
 
Marco Cantù's
Essential Pascal Chapter 2
Coding in Pascal
 
第2章
Pascal を使ったコーディング
 
Before we move on to the subject of writing Pascal language statements, it is important to highlight a couple of elements of Pascal coding style. The question I'm addressing here is this: Besides the syntax rules, how should you write code? There isn't a single answer to this question, since personal taste can dictate different styles. However, there are some principles you need to know regarding comments, uppercase, spaces, and the so-called pretty-printing. In general, the goal of any coding style is clarity. The style and formatting decisions you make are a form of shorthand, indicating the purpose of a given piece of code. An essential tool for clarity is consistency-whatever style you choose, be sure to follow it throughout a project.
 
Pascal 文について述べる前に、Pascal のコーディング・スタイルについて、いくつか指摘しておこう。 それは「文法に注意する以外に、どのようにコードを書けばよいか」、ということだ。 これについては、人により違いがあるので、答えはひとつにならない。 ただ、コメント、大文字、スペース、それにいわゆる Pretty-printing ( 見た目の良さ ) については、原理原則と言うものがある。
 
一般論で言えば、コーディング・スタイルの目標は、「明快さ」だ。 君がプログラミングで取ろうとしているスタイルや書式は、あるコードが実現しようとする目標・目的を、わかりやすい形で表していなければならない。 この明快さを保つには、どのようなスタイルを君が取るにせよ、それをプログラミング全体を通じて、しっかりと維持することが必要だ。
 
Comments
 
コメント
 
In Pascal, comments are enclosed in either braces or parentheses followed by a star. Delphi also accepts the C++ style comments, which can span to the end of the line:
 
Pascal では、コメントは大括弧か、星付きの括弧で囲む。 Delphi では、C++ で使われているコメント形式も使え、この場合は、その行末まで有効だ。
 
{this is a comment}
(* this is another comment *)
// this is a comment up to the end of the line
 
{これはコメント}
(* 別のコメント *)
// この行末まで、コメント
 
The first form is shorter and more commonly used. The second form was often preferred in Europe because many European keyboards lack the brace symbol. The third form of comments has been borrowed from C++ and is available only in the 32-bit versions of Delphi. Comments up to the end of the line are very helpful for short comments and for commenting out a line of code.
 
最初の形式が、一番良く使われているし、一般的だろう。 二番目のものは、ヨーロッパでよく使われる。 というのも、ヨーロッパのキーボードには大括弧の無いものが多いからだ。 三番目のものは、C++ から借りてきたもので、32ビット Delphi でのみ使用できる。 行末までコメントが有効、というのは、コメントが短い場合や、コードの一部をコメントとして除外する ( コメントアウト ) ときに便利だ。
 
In the listings of the book I'll try to mark comments as italic (and keywords in bold), to be consistent with the default Delphi syntax highlighting.
 
Delphi の、デフォルト設定の構文表示と同じ体裁にするため、この本では、コメントはイタリックで、キーワードは太字で表す。
 
Having three different forms of comments can be helpful for making nested comments. If you want to comment out several lines of source code to disable them, and these lines contain some real comments, you cannot use the same comment identifier:
 
コメントにいくつか種類があるのは、コメントがネストしている場合に便利だ。 コードの一部をコメントアウトして除外したい時に、除外するコード部分に本物のコメントが含まれている場合、同じ形式のコメント識別子は使えない。
 
{ ... code
{comment, creating problems}
... code }
 
{ ... コード
{コメント、これは問題を引き起こす}
... コード}
 
With a second comment identifier, you can write the following code, which is correct:
 
別のコメント識別子を使えば、次のような正しいコードを書ける。
 
{ ... code
//this comment is OK
... code }
 
{ ... コード
// このコメントはOK
... コード}
 
Note that if the open brace or parenthesis-star is followed by the dollar sign ($), it becomes a compiler directive, as in {$X+}.
 
注意。 左大括弧や星付き左括弧にドル ($) マークが付いているときは、{$X+} のように、コンパイラ指令になる。
 
Actually, compiler directives are still comments. For example, {$X+ This is a comment} is legal. It's both a valid directive and a comment, although sane programmers will probably tend to separate directives and comments.
 
実のところ、コンパイラ指令も、コメントのひとつだ。 たとえば、{$X+ これはコメント} は、正しい書式だ。 ただ、こう書くのは有効な指令でもあり、コメントでもあるけれど、賢明なプログラマは、指令とコメントを一緒くたに書こうとは思わないだろう。
 
Use of Uppercase
 
大文字の使用
 
The Pascal compiler (unlike those in other languages) ignores the case (capitalization) of characters. Therefore, the identifiers Myname, MyName, myname, myName, and MYNAME are all exactly equivalent. On the whole, this is definitely a positive, since in case-sensitive languages, many syntax errors are caused by incorrect capitalization.
 
Pascal コンパイラは、他の言語とは違って、大文字小文字の区別をしない。 だから、
Myname, MyName, myname, myName, MYNAME は、皆同じ識別子になる。 他の大文字小文字を区別する言語では、大文字小文字の使い方を間違えて、構文エラーを起こすことが多いので、このことは、非常に有効なポイントになる。
 
Note: There is only one exception to the case-insensitive rule of Pascal: the Register procedure of a components' package must start with the uppercase R, because of a C++Builder compatibility issue.
 
注意:この大文字小文字を区別しない、という Pascal で唯一の例外が、コンポーネントのパッケージを登録する手続き、 Register procedure の名称は大文字 R で始める、というものだ。 これは、C++Builder との互換を取るためだ。
 
There are a couple of subtle drawbacks, however. First, you must be aware that these identifiers really are the same, so you must avoid using them as different elements. Second, you should try to be consistent in the use of uppercase letters, to improve the readability of the code.
 
ただ、いくつか、小さな欠点もある。 第一に、大文字でも小文字でも識別子としては同じなので、まったく別の対象に、同じ名前を付けないことだ。 第二には、コードの読みやすさを考えて、大文字をうまく使いこなす必要がある。
 
A consistent use of case isn't enforced by the compiler, but it is a good habit to get into. A common approach is to capitalize only the first letter of each identifier. When an identifier is made up of several consecutive words (you cannot insert a space in an identifier), every first letter of a word should be capitalized:
 
大文字を使わないとコンパイラが文句を言う、ということは無いけれど、大文字を使うことに慣れておく、というのは、良い習慣だ。 一般的には、識別子の最初の文字を大文字にする。 単語をいくつかつないで識別子とする時、識別子にはスペースを挿めないので、それぞれの単語の最初の文字を大文字にしておくといい。 こういう具合だ。
 
MyLongIdentifier
MyVeryLongAndAlmostStupidIdentifier
(単語をつないだ識別子)
 
Other elements completely ignored by the compiler are the spaces, new lines, and tabs you add to the source code. All these elements are collectively known as white space. White space is used only to improve code readability; it does not affect the compilation.
 
コンパイラが無視する要素には、ソースコードに加えられるスペース、改行、タブがある。
これらは、まとめて「ホワイトスペース」と呼ばれている。 ホワイトスペースはコードの可読性を向上するだけで、コンパイルには影響しない。
 
Unlike BASIC, Pascal allows you to write a statement on several lines of code, splitting a long instruction on two or more lines. The drawback (at least for many BASIC programmers) of allowing statements on more than one line is that you have to remember to add a semicolon to indicate the end of a statement, or more precisely, to separate a statement from the next one. Notice that the only restriction in splitting programming statements on different lines is that a string literal may not span several lines.
 
BASIC とは違い、Pascal では、命令文などコード文を複数行に書くことができる。 文を次の文と切り離すために、セミコロンを文の最後に置く、ということに常に神経を使う、というのは、(少なくとも BASIC プログラマには)ひとつの欠点だ。 ただ、プログラミング文を複数行に書いていい、というのにはひとつ例外がある。 それは、String は複数行にすることができない、ということだ。
 
Again, there are no fixed rules on the use of spaces and multiple-line statements, just some rules of thumb:
 
スペースをどう挿み、や複数行をどう書くか、には定まったルールは無いが、いくつかを上げておこう。
 
The Delphi editor has a vertical line you can place after 60 or 70 characters. If you use this line and try to avoid surpassing this limit, your source code will look better when you print it on paper. Otherwise long lines may get broken at any position, even in the middle of a word, when you print them.
 
60 か 70 文字入力すると、Delphi のエディタには縦線が現れる。 この線を越えないようにコードを書けば、印刷した時、ソースコードの見栄えが良くなる。 そうでないと、印刷した時、長い文は、単語の真中でも、折り返されてしまう。
 
When a function or procedure has several parameters, it is common practice to place the parameters on different lines.
 
関数や手続きにパラメータのある時は、パラメータを別の行にする。
 
You can leave a line completely white (blank) before a comment or to divide a long piece of code in smaller portions. Even this simple idea can improve the readability of the code, both on screen and when you print it.
 
コメントの前に、空行を挿んでもいいし、コードを分割してもいい。 アイデアとしては大したものではないが、モニターに表示したり印刷したりした時に、ずっと見やすくなる。
 
Use spaces to separate the parameters of a function call, and maybe even a space before the initial open parenthesis. Also keep operands of an expression separated. I know that some programmers will disagree with these ideas, but I insist: Spaces are free; you don't pay for them. (OK, I know that they use up disk space and modem connection time when you upload or download a file, but this is less and less relevant, nowadays.)
 
関数のパラメータを区切る時や、左括弧の前にもスペースを付けると良い。 オペランドは区別できるように分離させておくことだ。 プログラマの中には、こうしてスペースを挿むことに否定的な人もあるだろうが、はっきり言って、「スペースは無料だ!」。 もちろん、ハードディスクの容量は食うだろうし、ファイルのアップやダウンロードに時間は余分にかかるだろうが、現在ではこうした欠点は、本当に取るに足りないことになりつつある。
 
Pretty-Printing
 
見栄えの良い書式
 
The last suggestion on the use of white spaces relates to the typical Pascal language-formatting style, known as pretty-printing. This rule is simple: Each time you need to write a compound statement, indent it two spaces to the right of the rest of the current statement. A compound statement inside another compound statement is indented four spaces, and so on:
 
最後にお薦めするが、Pascal 言語の書式に則った Pretty-printing 見栄えの良い書式、というものだ。 これは簡単で、たとえば、複文を書くときには2文字程度インデント(字下げ)をする。 その複文に、また複文を含むときは、4文字分インデントする。
 
if ... then
 statement; 文
 
if ... then
begin
 statement1; 文1
 statement2; 文2
end;
 
if ... then
begin
 if ... then
  statement1; 文1
 statement2; 文2
end;
 
The above formatting is based on pretty-printing, but programmers have different interpretations of this general rule. Some programmers indent the begin and end statements to the level of the inner code, some of them indent begin and end and then indent the internal code once more, other programmers put the begin in the line of the if condition. This is mostly a matter of personal taste.
 
上記の書式は、Pretty-printing に則っている。 ただ、この書式には、別のやり方があるだろう。 ある人は、begin と end を、内部のコードのところまでインデントするだろうし、また begin と end をインデントし、その上、内部のコードをインデントする人もいる。 if と同じレベルまで、begin を下げる人もいるだろう。 これは、各個人の好み、と言ってもいい。
 
A similar indented format is often used for lists of variables or data types, and to continue a statement from the previous line:
 
同様のインデントは、変数やデータ型のリスト表示、それに前の行から文を続ける時にも用いられる。
 
type
 Letters = set of Char;
var
 Name: string;
begin
  { long comment and long statement, going on in the  次の行にまたがる長いコメントな
   following line and indented two spaces }   どで、次行は2文字分イン  MessageDlg ('This is a message',            デント
   mtInformation, [mbOk], 0);
 
Of course, any such convention is just a suggestion to make the code more readable to other programmers, and it is completely ignored by the compiler. I've tried to use this rule consistently in all of the samples and code fragments in this book. Delphi source code, manuals, and Help examples use a similar formatting style.
 
もちろん、こうしたやり方は、他のプログラマにコードを読みやすくしよう、というだけのもので、コンパイラは、すべて無視する。 この本では、この書式を見本用のプログラムや、部分的なコードに、完全に適用して行く。 Delphi のソースコード、マニュアル、ヘルプの例でも、同様の書式が使われている。
 
Syntax Highlighting
 
構文ハイライト
 
To make it easier to read and write Pascal code, the Delphi editor has a feature called color syntax highlighting. Depending on the meaning in Pascal of the words you type in the editor, they are displayed using different colors. By default, keywords are in bold, strings and comments are in color (and often in italic), and so on.
 
コードの読み書きをより分かりやすくするため、Delphi のエディタでは、color syntac highlighting (構文色別表示)を採用している。 エディタに打ち込まれた語句の意味に応じ、自動的に色を変えて表示される。 デフォルトでは、キー・ワードは太字で、文字列とコメントは色付き(場合によってはイタリック体)で、などだ。
 
Reserved words, comments, and strings are probably the three elements that benefit most from this feature. You can see at a glance a misspelled keyword, a string not properly terminated, and the length of a multiple-line comment.
 
予約語、コメント、文字列などは、この機能のおかげで見やすくなる。 スペルを間違ったキーワードや、正しく終わっていない文字列や、複数行になっているコメントなどは、簡単に見分けられる。
 
You can easily customize the syntax highlight settings using the Editor Colors page of the Environment Options dialog box (see Figure 2.1). If you work by yourself, choose the colors you like. If you work closely with other programmers, you should all agree on a standard color scheme. I find that working on a computer with a different syntax coloring than the one I am used to is really difficult.
 
環境設定ダイアログボックス(図2.1参照)のエディタ・カラーのページで、デフォルトの設定を変更できる。 自分の好みの色にしたければ、好きな色を選べばよい。 他のプログラマと、チームで働くような場合は、定められた標準カラーに従うべきだ。 まったく別の設定で書かれたコードを読むのは、本当に難しい。
 
FIGURE 2.1: The dialog box used to set the color syntax highlighting.
図2.1:構文色別表示を設定するダイアログボックス
 
Note: In this book I've tried to apply a sort of syntax highlighting to the source code listings. I hope this actually makes them more readable.
 
この本では、私の設定を適用する。 皆さんに、読みやすくなっていればいいんだけれど。
 
Using Code Templates
 
コードテンプレートを使う
 
Delphi 3 introduced a new feature related to source code editing. Because when writing Pascal language statements you often repeat the same sequence of keywords, Borland has provided a new feature called Code Templates. A code template is simply a piece of code related with a shorthand. You type the shorthand, then press Ctrl+J, and the full piece of code appears. For example, if you type arrayd, and then press Ctrl+J, the Delphi editor will expand your text into:
 
Delphi 3 では、ソースコード編集、という機能が追加された。 Pascal プログラムを書いていると、同じキーワード群を書かなければならないことが多い。 簡便化するために、Borland は、コードテンプレート、という機能を提供してくれた。 コードテンプレートというのは、キーワードに結び付けられた、いくつかのコードの集まりだ。 このキーワードをタイプして、Ctrl + J を押すと、全体のコードが現れる。 例えば、arrayd とタイプして、Ctrl + J を押すと、下記のようにコードを補完してくれる。
 
array [0..] of ;
 
Since the predefined code templates usually include several versions of the same construct, the shortcut generally terminates with a letter indicating which of the versions you are interested in. However, you can also type only the initial part of the shortcut. For example, if you type ar and then press Ctrl+J, the editor will display a local menu with a list of the available choices with a short description, as you can see in Figure 2.2.
 
このコード補完は、同じ構造をしたバージョンの違うものを扱うので、ショートカットは必要なバージョンが分かるような文字で終わっている。 ショートカットの最初の部分を入力してもいい。 たとえば、ar と入力し、Ctrl+J とすると、図2.2 にあるように、エディタは短い説明付きでリストを表示する。
 
Figure 2.2: Code Templates selection
図2.2: コードテンプレート選択
 
 
You can fully customize the code templates by modifying the existing ones or adding your own common code pieces. If you do this, keep in mind that the text of a code template generally includes the '|' character to indicate where the cursor should jump to after the operation, that is, where you start typing to complete the template with custom code.
 
このコードテンプレートに、自分のコードを追加したり、既存のものを編集して使うことができる。 その時には、コードテンプレートには、'|' 文字があることに注意することだ。 前のコードが実行された後、カーソルがこの文字位置に移動する。 つまり、この文字のところから、君の独自のコードを書き加えればいい。
 
Language Statements
 
言語文
 
Once you have defined some identifiers, you can use them in statements and in the expressions that are part of some statements. Pascal offers several statements and expressions. Let's look at keywords, expressions, and operators first.
 
一度識別子を定義したら、それを文の中や、文節の中で用いることができる。 Pascal では幾種類かの文や文節がある。 キーワード、文、オペレータについて、まず見てみよう。
 
Keywords
 
キーワード
 
Keywords are all the Object Pascal reserved identifiers, which have a role in the language. Delphi's help distinguishes between reserved words and directives: Reserved words cannot be used as identifiers, while directives should not be used as such, even if the compiler will accept them. In practice, you should not use any keywords as an identifier.
 
キーワードは Object Pascal の予約識別子で、Pascal でそれぞれの役割を担っている。 Delphi のヘルプは、予約語と指令を区別している。 予約語は識別子としては使えず、また指令はコンパイラが見過ごす場合があっても、識別子に使ってはならない。 実際には、キーワードは一切、識別子としては、使わないこと。
 
In Table 2.1 you can see a complete list of the identifiers having a specific role in the Object Pascal language (in Delphi 4), including keywords and other reserved words.
 
表2.1 に、キーワードと予約語も含めた、Object Pascal (Delphi4) で特殊な役割を担っている識別子の全リストを示す。
 
Table 2.1: Keywords and other reserved words in the Object Pascal language
 
表2.1: Object Pascal でのキーワードと予約語
 
Keyword      Role
キーワード    役割
 
absolute      directive 指令 (variables 変数)
abstract      directive 指令 (method)
and        operator (boolean) オペレータ
array       type 型
as         operator オペレータ(RTTI 実行時型情報)
asm        statement 文
assembler     backward compatibility (asm) 逆互換性あり。
at         statement 文 (exceptions)
automated     access specifier アクセス識別子(class)
begin       block marker ブロックのマーカー
case        statement 文
cdecl       function calling convention 関数
class        type 型
const       declaration or directive (parameters) 宣言または指令
constructor     special method 特殊メソッド
contains      operator (set) オペレータ
default       directive (property) 指令
destructor      special method 特殊メソッド
dispid       dispinterface specifier 識別子
dispinterface     type 型
div         operator オペレータ
do         statement 文
downto       statement (for) 文
dynamic      directive (method) 指令
else        statement (if or case) 文
end        block marker ブロックマーカー
except       statement (exceptions) 文
export       backward compatibility (class) 逆互換性あり
exports       declaration 宣言
external       directive (functions) 指令
far         backward compatibility (class) 逆互換性あり
file         type 型
finalization      unit structure ユニット構文
finally        statement (exceptions) 文
for         statement 文
forward       function directive 関数指令
function       declaration 宣言
goto         statement 文
if          statement 文
implementation    unit structure ユニット構文
implements      directive (property) 指令
in          operator (set) - project structure オペレータ - プロジェクト構文
index        directive (dipinterface) 指令
inherited       statement 文
initialization      unit structure ユニット構文
inline         backward compatibility (see asm asm 参照) 逆互換性あり
interface        type 型
is          operator (RTTI) オペレータ
label         declaration 宣言
library        program structure プログラム構文
message        directive (method) 指令
mod         operator (math) オペレータ
name        directive (function) 指令
near         backward compatibility (class) 逆互換性あり
nil          value 値
nodefault       directive (property) 指令
not         operator (boolean) オペレータ
object        backward compatibility (class) 逆互換性あり
of          statement (case) 文
on          statement (exceptions) 文
or          operator (boolean) オペレータ
out         directive (parameters) 指令
overload       function directive 関数指令
override       function directive 関数指令
package       program structure (package) プログラム構文
packed       directive (record) 指令
pascal        function calling convention 関数
private        access specifier (class) アクセス識別子
procedure      declaration 宣言
program       program structure プログラム構文
property       declaration 宣言
protected      access specifier (class) アクセス識別子
public        access specifier (class) アクセス識別子
published      access specifier (class) アクセス識別子
raise        statement (exceptions) 文
read        property specifier プロパティ識別子
readonly      dispatch interface specifier ディスパッチインターフェイス識別子
record       type 型
register       function calling convention 関数
reintroduce     function directive 関数指令
repeat       statement 文
requires       program structure (package) プログラム構文
resident      directive (functions) 指令
resourcestring    type 型
safecall       function calling convention 関数
set         type 型
shl         operator (math) オペレータ
shr         operator (math) オペレータ
stdcall       function calling convention 関数
stored       directive (property) 指令
string        type 型
then        statement (if) 文
threadvar      declaration 宣言
to         statement (for) 文
try         statement (exceptions) 文
type        declaration 宣言
unit        unit structure ユニット構文
until        statement 文
uses        unit structure ユニット構文
var        declaration 宣言
virtual       directive (method) 指令
while       statement 文
with        statement 文
write       property specifier プロパティ識別子
writeonly     dispatch interface specifier ディスパッチインターフェイス識別子
xor        operator (boolean) オペレータ
 
 
Expressions and Operators
 
式とオペレータ
 
There isn't a general rule for building expressions, since they mainly depend on the operators being used, and Pascal has a number of operators. There are logical, arithmetic, Boolean, relational, and set operators, plus some others. Expressions can be used to determine the value to assign to a variable, to compute the parameter of a function or procedure, or to test for a condition. Expressions can include function calls, too. Every time you are performing an operation on the value of an identifier, rather than using an identifier by itself, that is an expression.
 
式を作るということに、一般的な規則というのは無い。 というのは、式はその中のオペレータによるからだ。 Pascal ではいくつかのオペレータが定義されている。 その中には、論理、数学、ブール、関係、セットや、その他の種類のオペレータがある。 式というのは、変数に割り当てられる値を決定したり、関数や手続きのパラメータを計算したり、条件を決定したりする時に使う。 式には関数を含めてもいい。 式というのは、ある識別子自体を使うのでなく、その識別子が表している値に対して何か処理をするときに使うものだ。
 
Expressions are common to most programming languages. An expression is any valid combination of constants, variables, literal values, operators, and function results. Expressions can also be passed to value parameters of procedures and functions, but not always to reference parameters (which require a value you can assign to).
 
式は、どのプログラミング言語にもある。 式というのは、定数、変数、リテラル値、オペレータ、関数の戻り値が結びついているものだ。 式は、手続きや関数の「値パラメータ」としては使えるけれど、「参照パラメータ」(君が値を与えてやらないといけない)にする場合は、いつも使えるとは限らない。
 
Operators and Precedence
オペレータと優先順位
 
If you have ever written a program in your life, you already know what an expression is. Here, I'll highlight specific elements of Pascal operators. You can see a list of the operators of the language, grouped by precedence, in Table 2.1.
 
今まで、何かプログラムを書いたことがあるなら、もう式については知っているだろう。
ここでは、オペレータに注目してみよう。 Pascal のオペレータ・リストを、優先順位に付けて、表 2.1 に示す。
 
Contrary to most other programming languages, the and and or operators have precedence compared to the relational one. So if you write a < b and c < d, the compiler will try to do the and operation first, resulting in a compiler error. For this reason you should enclose each of the < expression in parentheses: (a < b) and (c < d).
 
他の言語と違い、and と or オペレータは、他の関係オペレータより優先的に処理される。つまり、a < b and c < d と書くと、コンパイラは and オペレータを先に処理し、結果としてエラーを出してくる。 だから、こういう場合は、(a < b) and (c < d) のように、< 式を括弧でくくること。
 
Some of the common operators have different meanings with different data types. For example, the + operator can be used to add two numbers, concatenate two strings, make the union of two sets, and even add an offset to a PChar pointer. However, you cannot add two characters, as is possible in C.
 
一般的なオペレータの中には、データ型が違うと、別の意味を持つものもある。 たとえば、+ オペレータは、ふたつの数値を加算したり、String をつないだり、ふたつのセットをユニオン結合したりできる。 PChar 型のポインタへのオフセットを加算することもできる。 ただ、C でやるような、ふたつの文字を結合することはできない。
 
Another strange operator is div. In Pascal, you can divide any two numbers (real or integers) with the / operator, and you'll invariably get a real-number result. If you need to divide two integers and want an integer result, use the div operator instead.
 
変わったものといえば、div オペレータがある。 / オペレータを使い、実数とか整数を割った場合、必ず実数値が結果になる。 整数を整数で割って、結果として整数が欲しい、というのなら、div オペレータを使う必要がある。
 
Table 2.2: Pascal Language Operators, Grouped by Precedence
 
表 2.2 : Pascal のオペレータを優先順位を付けて表す。
 
Unary Operators (Highest Precedence)
 
@ Address of the variable or function (returns a pointer)
@ 変数や関数のアドレス (ポインタを返す)
 
not Boolean or bitwise not
非ブール、またはビット演算の not
 
Multiplicative and Bitwise Operators
ビット乗算
 
* Arithmetic multiplication or set intersection
* 数学的乗算、またはセットの結合
 
/ Floating-point division
/ 浮動小数点除算
 
div Integer division
div 整数除算
 
mod Modulus (the remainder of integer division)
mod モジュラス (整数除算の余り)
 
as Allows a type-checked type conversion among at runtime (part of the RTTI support)
as ランタイム時の型変換チェック (RTTI サポートの一部)
 
and Boolean or bitwise and
and ブール、およびビット加算
 
shl Bitwise left shift
shl ビットを左にシフト
 
shr Bitwise right shift
shr ビットを右にシフト
 
Additive Operators
加算オペレータ
+ Arithmetic addition, set union, string concatenation, pointer offset addition
+ 数学的加算、セットの結合、String の連接、ポインタ・オフセットの加算
- Arithmetic subtraction, set difference, pointer offset subtraction
- 数学的減算、セットの差、ポインタ・オフセットの減算
 
or Boolean or bitwise or
or ブール、またはビットor
 
xor Boolean or bitwise exclusive or
xor ブール、またはビット排他or
 
Relational and Comparison Operators (Lowest Precedence)
関係および比較オペレータ (優先順位最低)
= Test whether equal
= 同一か
<> Test whether not equal
<> 同一でないか
< Test whether less than
< より小さいか
> Test whether greater than
> より大きいか
<= Test whether less than or equal to, or a subset of a set
<= より小さいか、同一か、またはセットのサブセットか
>= Test whether greater than or equal to, or a superset of a set
>= 大きいか、同一か、またはセットのスーパーセットか
in Test whether the item is a member of the set
in その要素はセットのメンバーか
is Test whether object is type-compatible (another RTTI operator)
is 対象とするものは、型互換性があるか (もうひとつの RTTI オペレータ)
 
Set Operators
 
セット オペレータ
 
The set operators include union (+), difference (-), intersection (*),membership test (in), plus some relational operators. To add an element to a set, you can make the union of the set with another one that has only the element you need. Here's a Delphi example related to font styles:
 
セット・オペレータには、和 (+)、差 (-)、積 (*)、メンバーに含まれているかどうか (in)
に加え、関係オペレータがある。 セットに要素を加えるには、必要とする要素を含む他のセットとの和を取ればいい。 Delphi のフォント・スタイルで見てみれば、
 
Style := Style + [fsBold];
Style := Style + [fsBold, fsItalic] - [fsUnderline];
 
As an alternative, you can use the standard Include and Exclude procedures, which are much more efficient (but cannot be used with component properties of the set type, because they require an l-value parameter):
 
別のやり方として、標準の Include、Exclude プロシージャが使える。 これはずっと強力だが、コンポーネントのプロパティでは使えない。 というのも、パラメータをひとつしか取れないからだ。
 
Include (Style, fsBold);
 
Conclusion
 
結論
 
Now that we know the basic layout of a Pascal program we are ready to start understanding its meaning in detail. We'll start by exploring the definition of predefined and user defined data types, then we'll move along to the use of the keywords to form programming statements.
 
さて、Pascal のプログラムについて、基礎的なものはマスターしたので、詳細に移ろう。
まず、既に用意されているデータ型や、ユーザーが定義できるデータ型について見ていこう。 それから、プログラミング文を組み上げているキーワードについて、探ってみよう。
 
Next Chapter: Types, Variables, and Constants
 
次章:型、変数、定数
 
 
Marco Cantu's
Essential Pascal Chapter 3
Types, Variables, and Constants
 
第3章 
型、変数、定数
 
The original Pascal language was based on some simple notions, which have now become quite common in programming languages. The first is the notion of data type. The type determines the values a variable can have, and the operations that can be performed on it. The concept of type is stronger in Pascal than in C, where the arithmetic data types are almost interchangeable, and much stronger than in the original versions of BASIC, which had no similar concept.
 
オリジナルの Pascal は、現在のプログラミング言語から言えば「当たり前」、と思われるような、シンプルな考え方をベースにしていた。 第一が、データ型という考え方だ。 型は、変数が保持する値を定義し、その値になんらかの処理ができるようになる。 Pascal の型に対する考え方は、数値データ型が殆ど互換で使える C より強いし、型に対する考え方自体が無い、オリジナルの BASIC より遥かに強い。 
 
Variables
 
変数
 
Pascal requires all variables to be declared before they are used. Every time you declare a variable, you must specify a data type. Here are some sample variable declarations:
 
Pascal では、変数を使う前に、その変数を宣言しておく必要がある。 また、変数を宣言する時には、必ずデータ型も決めておかないといけない。 変数宣言の見本を挙げておこう。
 
var
 Value: Integer;
 IsCorrect: Boolean;
 A, B: Char;
 
The var keyword can be used in several places in the code, such as at the beginning of the code of a function or procedure, to declare variables local to the routine, or inside a unit to declare global variables. After the var keyword comes a list of variable names, followed by a colon and the name of the data type. You can write more than one variable name on a single line, as in the last statement above.
 
var キーワードは、コードのあちこちで使え、関数や手続きの始めの部分など、その処理のみで使われるローカル変数の宣言に使ったり、ユニット全体で使われるグローバル変数の宣言部で使う。 var キーワードの後には変数名のリストを置き、そのまた後にコロンとデータ型を置く。 上の最後の例のように、一行に複数の変数名を書いてもいい。
 
Once you have defined a variable of a given type, you can perform on it only the operations supported by its data type. For example, you can use the Boolean value in a test and the integer value in a numerical expression. You cannot mix Booleans and integers (as you can with the C language).
 
変数にデータ型を宣言したら、そのデータ型に許されている処理のみ、変数を対象として扱える。 たとえば、ブール型の変数と整数型の数式を、それぞれ扱える。 しかし、ブール型と整数を一緒にすることはできない( C なら可能だ)。
 
Using simple assignments, we can write the following code:
 
例を挙げれば、こういう具合に書ける。
 
Value := 10;
IsCorrect := True;
 
But the next statement is not correct, because the two variables have different data types:
 
しかし、次の文は、データ型が違うので間違いだ。
 
Value := IsCorrect; // error エラー
 
If you try to compile this code, Delphi issues a compiler error with this description: Incompatible types: 'Integer' and 'Boolean'. Usually, errors like this are programming errors, because it does not make sense to assign a True or False value to a variable of the Integer data type. You should not blame Delphi for these errors. It only warns you that there is something wrong in the code.
 
このコードをコンパイルしようとすると、Delphi は次のようなエラーを出してくる:型互換無し : 整数とブール。 整数値に、真か偽かということをくっつけること自体、意味が無いので、このようなエラーは、プログラミング・エラーと呼ばれる。 Delphi に文句を言ってはいけない。 Delphi は、君のコードはどこかおかしいよ、と言ってるだけなんだから。
 
Of course, it is often possible to convert the value of a variable from one type into a different type. In some cases, this conversion is automatic, but usually you need to call a specific system function that changes the internal representation of the data.
 
もちろん、ある変数の型を、他の型に変えることは良くある。 この変換が自動的に行われる時もあるが、変換するには特別のシステム関数を呼び出し、データ型を変える必要がある。
 
In Delphi you can assign an initial value to a global variable while you declare it. For example, you can write:
 
Delphi では、グローバル変数には、宣言時に初期値を設定することができる。 こういう具合だ。
 
var
 Value: Integer = 10;
 Correct: Boolean = True;
 
This initialization technique works only for global variables, not for variables declared inside the scope of a procedure or method.
 
この技法は、手続きやメソッドの内部で使う変数には使えず、グローバル変数の初期化にだけ使える。 
 
Constants
 
定数
 
Pascal also allows the declaration of constants to name values that do not change during program execution. To declare a constant you don't need to specify a data type, but only assign an initial value. The compiler will look at the value and automatically use its proper data type. Here are some sample declarations:
 
Pascal では、プログラムの実行中、その値を変えないものを、定数として宣言できる。 定数宣言には、型を特定する必要は無く、初期値のみを示す。 コンパイラはその値を見て、自動的にその値に合致するデータ型を選び出す。 サンプルを示そう。
 
const
 Thousand = 1000;
 Pi = 3.14;
 AuthorName = 'Marco Cantu';
 
Delphi determines the constant's data type based on its value. In the example above, the Thousand constant is assumed to be of type SmallInt, the smallest integral type which can hold it. If you want to tell Delphi to use a specific type you can simply add the type name in the declaration, as in:
 
Delphi は、定数のデータ型を、その値から判断している。 上の例で言えば、Thousand 定数は一番範囲の狭い整数である SmallInt 型だと判断する。 Delphi に任せるのでなく、はっきりデータ型を指定したい場合は、下のように型名を宣言に加える。
 
const
 Thousand: Integer = 1000;
 
When you declare a constant, the compiler can choose whether to assign a memory location to the constant, and save its value there, or to duplicate the actual value each time the constant is used. This second approach makes sense particularly for simple constants.
 
定数宣言すると、コンパイラは、その定数にメモリを割り当て、そこに値を格納するか、定数が使われるたびに実際の値を複製するか、を選択できる。 後者のやり方の方が、単純な定数の場合は、理にかなっている。
 
Note: The 16-bit version of Delphi allows you to change the value of a typed constant at run-time, as if it was a variable. The 32-bit version still permits this behavior for backward compatibility when you enable the $J compiler directive, or use the corresponding Assignable typed constants check box of the Compiler page of the Project Options dialog box. Although this is the default, you are strongly advised not to use this trick as a general programming technique. Assigning a new value to a constant disables all the compiler optimizations on constants. In such a case, simply declare a variable, instead.
 
注意:16 ビットバージョンの Delphi では、実行時に、あたかも変数であるかのように、型付き定数の値を変更できた。 32 ビットでも、過去との互換性を保つため、$J コンパイラ指令が使えるとき、またはプロジェクト・オプション・ダイアログ・ボックスのコンパイラ・ページで、対応する型付き定数にチェックを入れれば、この機能が使える。 但し、これは欠点と言うべきもので、通常のプログラミングでは、この手法を使わないよう、強く薦める。 定数に新たに値を設定することは、定数に対してコンパイラが行っている最適化を、不可能にすることだ。 どうしても、と言うのなら、初めから変数として宣言することだ。
 
Resource String Constants
 
リソース・ストリング定数
 
When you define a string constant, instead of writing:
 
ストリング定数は、下記のように宣言するが、
 
const
 AuthorName = 'Marco Cantu';
 
starting with Delphi 3 you can write the following:
 
Delphi 3 からは、次のように書くことができる。
 
resourcestring
 AuthorName = 'Marco Cantu';
 
In both cases you are defining a constant; that is, a value you don't change during program execution. The difference is only in the implementation. A string constant defined with the resourcestring directive is stored in the resources of the program, in a string table.
 
どちらでも、定数を定義していることに変わりは無い。 つまり、プログラムの実行中、その値を変えない、と言うことだ。 違いは実装にある。 リソースストリング指令と共に定義されたストリング定数は、プログラムのリソースである、ストリング・テーブルに格納される。
 
To see this capability in action, you can look at the ResStr example, which has a button with the following code:
 
どういうことかは、ResStr サンプルを見れば分かる。 これには、ボタンと下記のコードがある。
 
resourcestring
 AuthorName = 'Marco Cantu';
 BookName = 'Essential Pascal';
 
procedure TForm1.Button1Click(Sender: TObject);
begin
 ShowMessage (BookName + #13 + AuthorName);
end;
 
The output of the two strings appears on separate lines because the strings are separated by the newline character (indicated by its numerical value in the #13 character-type constant).
 
ふたつのストリングは、改行文字( 文字型定数 #13 で示す )があるので、2行に分かれている。
 
The interesting aspect of this program is that if you examine it with a resource explorer (there is one available among the examples that ship with Delphi) you'll see the new strings in the resources. This means that the strings are not part of the compiled code but stored in a separate area of the executable file (the EXE file).
 
このプログラムで面白いのは、リソース・エクスプローラ( Delphi の examples として標準付属)で見てみれば、リソースに新しいストリングが入っているのが分かる。 つまり、このストリングは、コンパイルされたコードの一部ではなく、実行可能ファイル( EXE ファイル)の、分離された部分に収められている、ということだ。
 
Note: In short, the advantage of resources is in an efficient memory handling performed by Windows and in the possibility of localizing a program (translating the strings to a different language) without having to modify its source code.
 
注意:手短に言えば、リソースの長所は、Windows でのメモリ使用を最適化できるということと、他国語にプログラムを変換する場合、ソース・コードを変える必要が無い、ということだ。
 
Data Types
 
データ型
 
In Pascal there are several predefined data types, which can be divided into three groups: ordinal types, real types, and strings. We'll discuss ordinal and real types in the following sections, while strings are covered later in this chapter. In this section I'll also introduce some types defined by the Delphi libraries (not predefined by the compiler), which can be considered predefined types.
 
Pascal には、既に定義されたデータ型がいくつかあり、3つのグループに分けることができる。 順序型、実数型、それにストリングだ。 次のセクションで順序型と実数型を扱い、ストリングはこの章の最後の部分で扱うことにする。 また、Delphi ライブラリ (コンパイラが定義しているものではなくて) に定義してあるものをいくつか、紹介する。 これらも既に定義してあるもの、と考えられるからだ。
 
Delphi also includes a non-typed data type, called variant, and discussed in Chapter 10 of this book. Strangely enough a variant is a type without proper type-checking. It was introduced in Delphi 2 to handle OLE Automation.
 
Delphi には、型無しのデータ型もある。 これはバリアントと呼ばれ、10章で扱う。 おかしなことに、バリアントは、適切な型チェックを行わない。 これは Delphi 2 で OLE Automation を扱うために導入されたものだ。
 
Ordinal Types
 
順序型
 
Ordinal types are based on the concept of order or sequence. Not only can you compare two values to see which is higher, but you can also ask for the value following or preceding a given value or compute the lowest or highest possible value.
 
順序型というのは、順序や順番という考え方が元になっている。 この考え方では、ふたつの値のどちらが大きいか、比較できるだけでなく、どちらが先かも分かるし、また最も小さい値、または大きい値も計算できる。
 
The three most important predefined ordinal types are Integer, Boolean, and Char (character). However, there are a number of other related types that have the same meaning but a different internal representation and range of values. The following Table 3.1 lists the ordinal data types used for representing numbers.
 
最も重要な順序型が、Integer、Boolean、Char(Character) だ。 もちろんこれ以外にも、同様の意味を持ち、違う内部処理をされるものや、値の範囲が違う類似型が多数ある。 表 3.1 に、順序型のリストを示す。
 
Table 3.1: Ordinal data types for numbers
 
表 3.1: 数値用順序型
 
Size Signed  Range Unsigned        Range
符号付サイズ 符号無し範囲        範囲
8 bits    ShortInt -128 to 127      Byte 0 to 255
16 bits   SmallInt -32768 to 32767    Word 0 to 65,535
32 bits   LongInt -2,147,483,648 to 2,147,483,647 
            (32 bits): LongWord (since Delphi 4)0 to 4,294,967,295
64 bits   Int64
16/32 bits  Integer            Cardinal
 
As you can see, these types correspond to different representations of numbers, depending on the number of bits used to express the value, and the presence or absence of a sign bit. Signed values can be positive or negative, but have a smaller range of values, because one less bit is available for the value itself. You can refer to the Range example, discussed in the next section, for the actual range of values of each type.
 
これで分かるように、これらの型は、値を示すビット数や、符号ビットのある無しによって、それぞれ固有の数値範囲に対応している。 符号付きの場合は、正も負も扱えるが、扱える範囲は小さくなる。 これは、符号用に1ビット取られるからだ。 次のセクションで扱う、Range サンプルで、それぞれの型の値の範囲がはっきりするだろう。
 
The last group (marked as 16/32) indicates values having a different representation in the 16-bit and 32-bit versions of Delphi. Integer and Cardinal are frequently used, because they correspond to the native representation of numbers in the CPU.
 
最後のグループ (16/32 と書いてあるもの) は、Delphi の 16 と 32 ビット・バージョンでは、別の範囲をカバーするものだ。 Integer と Cardinal は、多くの CPU のネイティブな値の範囲と合致するので、よく使われている。
 
Integral Types in Delphi 4
 
Delphi 4 での統合型
 
In Delphi 3, the 32-bit unsigned numbers indicated by the Cardinal type were actually 31-bit values, with a range up to 2 gigabytes. Delphi 4 introduced a new unsigned numeric type, LongWord, which uses a truly 32-bit value up to 4 gigabytes. The Cardinal type is now an alias of the new LongWord type. LongWord permits 2GB more data to be addressed by an unsigned number, as mentioned above. Moreover, it corresponds to the native representation of numbers in the CPU.
 
Delphi 3 の Cardinal で現される 32 ビット符号無し数値は、実のところ 31 ビットで、2 ギガバイトまで扱える。 Delphi 4 では、32 ビット丸ごと使い、4 ギガバイトまで扱える、新しい符号無し数値型、LongWord が導入された。 Cardinal は、現在ではこの新しい LongWord のエリアス (別名) になっている。 LongWord では、符号無しであれば、このように 2GB 以上のデータにアクセスが可能だ。 それに、これは CPU がネイティブに扱っている数値と合致している。
 
Another new type introduced in Delphi 4 is the Int64 type, which represents integer numbers with up to 18 digits. This new type is fully supported by some of the ordinal type routines (such as High and Low), numeric routines (such as Inc and Dec), and string-conversion routines (such as IntToStr). For the opposite conversion, from a string to a number, there are two new specific functions: StrToInt64 and StrToInt64Def.
 
Delphi 4 で導入されたのはこれだけでなく、18 ディジットまでの整数値を扱う、Int64 もある。 この新しい型は、順序型のルーチン (High と Low) や、数値ルーチン (Inc や Dec など)、ストリング変換ルーチン (IntToStr) などで完全にサポートされている。 逆の変換、ストリングから数値には、新たに StrToInt64、StrToInt64Def のふたつの関数が導入された。
 
Boolean
 
ブール型
 
Boolean values other than the Boolean type are seldom used. Some Boolean values with specific representations are required by Windows API functions. The types are ByteBool, WordBool, and LongBool.
 
ブール型以外のブール値を使うことは、まずない。 このブール値は、Windows API 関数を扱うときに必要になる。 型としては、ByteBool、WordBool、LongBool がある。
 
In Delphi 3 for compatibility with Visual Basic and OLE automation, the data types ByteBool, WordBool, and LongBool were modified to represent the value True with -1, while the value False is still 0. The Boolean data type remains unchanged (True is 1, False is 0). If you've used explicit typecasts in your Delphi 2 code, porting the code to later versions of Delphi might result in errors.
 
Delphi 3 では、Visual Basic や OLE オートメーションとの互換性を考え、ByteBool、WordBool、LongBool の意味が、True では -1、False では 0 に変更された。 通常のブール型では、今までどおり (True は 1、False は 0) だ。 Delphi 2 で、明確に型キャストを指定していると、そのまま後のバージョンの Delphi にコードを移し変えた時、エラーになる場合もある、ということだ。
 
Characters
 
文字
 
Finally there are two different representation for characters: ANSIChar and WideChar. The first type represents 8-bit characters, corresponding to the ANSI character set traditionally used by Windows; the second represents 16-bit characters, corresponding to the new Unicode characters supported by Windows NT, and only partially by Windows 95 and 98. Most of the time you'll simply use the Char type, which in Delphi 3 corresponds to ANSIChar. Keep in mind, anyway, that the first 256 Unicode characters correspond exactly to the ANSI characters.
 
最後に、文字には2種類ある。 ANSIChar と WideChar だ。 ANSIChar では、Windows で伝統的に使われてきた 8 ビットの ANSI 文字セットを使う。 WideChar は、Windows NT や、一部ではあるが、Windows 95、98 に用いられている、新しい 16 ビットの Unicode 文字セットを使う。 Delphi 3 では、Char と書くと、そのまま ANSIChar と判断される。 もちろん、Unicode の最初の 256 文字は、そっくり ANSI 文字セットに合致している、ということは、知っているだろう。
 
Constant characters can be represented with their symbolic notation, as in 'k', or with a numeric notation, as in #78. The latter can also be expressed using the Chr function, as in Chr (78). The opposite conversion can be done with the Ord function.
 
定数としての文字は、シンボルとして 'k' と書いてもいいし、数値的に #78 と書いてもいい。 後者は Chr 関数を使い、Chr(78) としてもいい。 逆にしたい場合は、Ord 関数を使う。
 
It is generally better to use the symbolic notation when indicating letters, digits, or symbols. When referring to special characters, instead, you'll generally use the numeric notation. The following list includes some of the most commonly used special characters:
 
一般的には、文字、ディジット、シンボルを表すときには、シンボル的な書き方がいい。 特殊文字が必要なときには、数値的な書き方がいい。 特殊文字を書くときにどうするか、一般的な用法を示してみよう。
 
#9 tabulator  タブ
#10 newline  改行
#13 carriage return (enter key)  キャリッジリターン( Enter キー)
 
The Range Example
 
範囲のサンプル
 
To give you an idea of the different ranges of some of the ordinal types, I've written a simple Delphi program named Range. Some results are shown in Figure 3.1.
 
順序型が扱う範囲の違いを分かってもらうために、Range という名前の簡単な Delphi プログラムを書いてみた。 その結果を、図 3.1 に示す。
 
FIGURE 3.1: The Range example displays some information about ordinal data types (Integers in this case).
 
図 3.1: 順序型についての Range サンプル ( この場合は、Integer )
 
The Range program is based on a simple form, which has six buttons (each named after an ordinal data type) and some labels for categories of information, as you can see in Figure 3.1. Some of the labels are used to hold static text, others to show the information about the type each time one of the buttons is pressed.
 
このプログラムは、シンプルなフォームと、順序データ値が示された6つのボタン、カテゴリーのラベルでできている。 ラベルは2種類用意し、変更しない、スタティックなテキストを示すものと、ボタンが押されたときに、それぞれの型が表示されるものを用意した。
 
Every time you press one of the buttons on the right, the program updates the labels with the output. Different labels show the data type, number of bytes used, and the maximum and minimum values the data type can store. Each button has its own OnClick event-response method because the code used to compute the three values is slightly different from button to button. For example, here is the source code of the OnClick event for the Integer button (BtnInteger):
 
右側のボタンを押すと、プログラムがラベル表示を更新する。 ラベルには、データ型、バイト数、そのデータ型が保持できる最大・最小値が示される。 それぞれのボタンには、押されるボタンによって3つの値の計算に差が出るように、OnClick イベントに対応するメソッドを書いてある。 たとえば、Integer ボタン (BtnInteger) の OnClick イベントに対応するソース・コードは、
 
procedure TFormRange.BtnIntegerClick(Sender: TObject);
begin
 LabelType.Caption := 'Integer';
 LabelSize.Caption := IntToStr (SizeOf (Integer));
 LabelMax.Caption := IntToStr (High (Integer));
 LabelMin.Caption := IntToStr (Low (Integer));
end;
 
If you have some experience with Delphi programming, you can examine the source code of the program to understand how it works. For beginners, it's enough to note the use of three functions: SizeOf, High, and Low. The results of the last two functions are ordinals of the same kind (in this case, integers), and the result of the SizeOf function is always an integer. The return value of each of these functions is first translated into strings using the IntToStr function, then copied to the captions of the three labels.
 
君が Delphi でプログラムした経験があれば、このコードがどういう具合に動くか、すぐに分かるだろう。 初心者は、3つの関数を使っていることを知ってもらうだけでいい。 それは SizeOf、High、Low だ。 この High と Low 関数の結果は、同じ種類の序数(この場合は Integer )で、SizeOf 関数の結果は、常に整数だ。 これらの関数の戻り値は、まず始めに IntToStr 関数で string に変換され、続いて、3つのラベルのキャプションにコピーされる。
 
The methods associated with the other buttons are very similar to the one above. The only real difference is in the data type passed as a parameter to the various functions. Figure 3.2 shows the result of executing this same program under Windows 95 after it has been recompiled with the 16-bit version of Delphi. Comparing Figure 3.1 with Figure 3.2, you can see the difference between the 16-bit and 32-bit Integer data types.
 
他のボタンに関連付けられているメソッドも、ほぼ同じようなものだ。 最大の違いは、それぞれの関数にパラメータとして渡される、データ型の違いにある。 16 ビット版の Delphi でコンパイルし直したプログラムを、Windows 95 で実行させた時の結果を、図 3.2 に示す。 図 3.1 と図 3.2 を比較すれば、16 ビットと 32 ビットの整数型の違いが分かるだろう。
 
FIGURE 3.2: The output of the 16-bit version of the Range example, again showing information about integers.
 
図 3.2: 整数値について、16 ビット版を使用した場合の結果
 
The size of the Integer type varies depending on the CPU and operating system you are using. In 16-bit Windows, an Integer variable is two bytes wide. In 32-bit Windows, an Integer is four bytes wide. For this reason, when you recompile the Range example, you get a different output.
 
整数型のサイズは、君の使っている CPU と OS によって変わる。 16 ビットの Windows では、整数値は 2 バイトの大きさだ。 32 ビットの Windows では、整数値は 4 バイトの大きさになる。 だから、この Range プログラムを違った環境でコンパイルすると、違った結果が生じることになる。
 
The two different representations of the Integer type are not a problem, as long as your program doesn't make any assumptions about the size of integers. If you happen to save an Integer to a file using one version and retrieve it with another, though, you're going to have some trouble. In this situation, you should choose a platform-independent data type (such as LongInt or SmallInt). For mathematical computation or generic code, your best bet is to stick with the standard integral representation for the specific platform--that is, use the Integer type--because this is what the CPU likes best. The Integer type should be your first choice when handling integer numbers. Use a different representation only when there is a compelling reason to do so.
 
このように、整数値の表す範囲が違う、ということは、整数のサイズが問題になるようなプログラムで無い限り、問題とはならない。 だから、ある整数をファイルに保存し、他のバージョンのプログラムで読み込んだりすれば、もちろん、問題になる。 こういうことが必要なら、プラットフォームに無関係なデータ型 ( LongInt や SmallInt など) を使えばいい。 数学計算や、普遍的なコードを書くときには、標準の整数型、すなわち Integer 、を使うことだ。 CPU は、一番この型がお気に入りだから。 他の型を使うのは、どうしてもそうするより他に方法がない、という場合に限るべきだ。
 
Ordinal Types Routines
 
順序数のルーチン
 
There are some system routines (routines defined in the Pascal language and in the Delphi system unit) that work on ordinal types. They are shown in Table 3.2. C++ programmers should notice that the two versions of the Inc procedure, with one or two parameters, correspond to the ++ and += operators (the same holds for the Dec procedure).
 
序数用のシステム・ルーチン ( Pascal と Delphi の system ユニットで定義されているもの) もある。 これを表 3.2 に示す。 C++ プログラマなら、パラメータをひとつか二つ取る、Inc プロシージャがあることに気づくだろう。 これはC++ なら、++、または += オペレータに相当するものだ。 Dec プロシージャも、同様だ。
 
Table 3.2: System Routines for Ordinal Types
 
表 3.3: 序数値のシステム・ルーチン
 
Routine    Purpose
ルーチン   目的
 
Dec      Decrements the variable passed as parameter, by one or by the value of the          optional second parameter. 
       パラメータとして渡される値を、ひとつ、またはオプションである第2パ       ラメータの値分、減算する。
 
Inc      Increments the variable passed as parameter, by one or by the specified value. 
       パラメータとして渡される値を、ひとつ、または特定値分、加算する。
 
Odd      Returns True if the argument is an odd number. 
       引数が奇数ならば、True を返す。
 
Pred      Returns the value before the argument in the order determined by the data type,        the predecessor. 
       データ型により定まる順番で、引数のひとつ前の値を返す。
 
Succ     Returns the value after the argument, the successor. 
       引数のひとつ後の値を返す。
 
Ord      Returns a number indicating the order of the argument within the set of values of        the data type. 
       そのデータ型の値の範囲内で、引数の該当する順番を返す。
 
Low     Returns the lowest value in the range of the ordinal type passed as its parameter.       パラメータとして渡される順序型の範囲での、一番小さな値を返す。
 
High     Returns the highest value in the range of the ordinal data type. 
       パラメータとして渡される順序型の範囲での、一番大きな値を返す。
 
Notice that some of these routines, when applied to constants, are automatically evaluated by the compiler and replaced by their value. For example if you call High(X) where X is defined as an Integer, the compiler can simply replace the expression with the highest possible value of the Integer data type.
 
これらのルーチンの中には、定数に適用されると、自動的にコンパイラがそのルーチンを判断し、該当する値に変換してしまうものもある。 たとえば、High(X) とし、その X が Integer だったら、コンパイラは X を、Integer 型の最大値に変換してしまう。
 
Real Types
 
実数型
 
Real types represent floating-point numbers in various formats. The smallest storage size is given by Single numbers, which are implemented with a 4-byte value. Then there are Double floating-point numbers, implemented with 8 bytes, and Extended numbers, implemented with 10 bytes. These are all floating-point data types with different precision, which correspond to the IEEE standard floating-point representations, and are directly supported by the CPU numeric coprocessor, for maximum speed.
 
実数型は、様々な書式で浮動小数点値を表す。 数字ひとつで表される、もっとも小さい値のメモリ・サイズは、4 バイトだ。 この他には、8 バイトの Double 浮動小数点値、10 バイトの Extended がある。 これは浮動小数点型に、精度の違うものがあるからで、それぞれ IEEE 標準の浮動小数点に対応しており、最大速度での計算が可能なよう、CPU 数値演算コプロセッサが、直接サポートしている。
 
In Delphi 2 and Delphi 3 the Real type had the same definition as in the 16-bit version; it was a 48-bit type. But its usage was deprecated by Borland, which suggested that you use the Single, Double, and Extended types instead. The reason for their suggestion is that the old 6-byte format is neither supported by the Intel CPU nor listed among the official IEEE real types. To completely overcome the problem, Delphi 4 modifies the definition of the Real type to represent a standard 8-byte (64-bit) floating-point number.
 
Delphi 2 と 3 は 16 ビット・バージョンであったため、Real 型は同じ長さ、48 ビット型になっていた。 ただ、Borland はこの Real を嫌い、Single、Double、Extended 型を使うように薦めている。 この理由は、古い 6 バイト・フォーマットは、Intel CPU でもサポートされないし、IEEE の公式実数型のリストにも挙がっていないからだ。 この問題に完全に対応するため、Delphi 4 では、Real 型を標準となっている 8 バイト (64 ビット) 浮動小数点値を指すように改良した。
 
In addition to the advantage of using a standard definition, this change allows components to publish properties based on the Real type, something Delphi 3 did not allow. Among the disadvantages there might be compatibility problems. If necessary, you can overcome the possibility of incompatibility by sticking to the Delphi 2 and 3 definition of the type; do this by using the following compiler option:
 
標準に沿わせるようにした利点に加え、このことで、コンポーネントがプロパティを公開する場合に、Real 型が使えるようになった。 これは Delphi 3 では、できなかったことだ。 欠点といえば、互換性の問題がある。 Delphi 2 や 3 で作ったコードを、どうしても使いたい、ということなら、下記のコンパイラ・オプションを使うといい。
 
{$REALCOMPATIBILITY ON}
 
There are also two strange data types: Comp describes very big integers using 8 bytes (which can hold numbers with 18 decimal digits); and Currency (not available in 16-bit Delphi) indicates a fixed-point decimal value with four decimal digits, and the same 64-bit representation as the Comp type. As the name implies, the Currency data type has been added to handle very precise monetary values, with four decimal places.
 
あとふたつ、変わったデータ型がある。 Comp は非常に大きな 8 バイトの整数 ( 18 桁ものディジットが扱える ) が扱え、Currency ( 16 ビットの Delphi では不可 ) は、小数点以下 4 桁の固定小数点値を扱え、Comp と同様、64 ビット長だ。 名前が示すように、非常に精緻な、小数点以下 4 桁の金額を扱える。
 
We cannot build a program similar to the Range example with real data types, because we cannot use the High and Low functions or the Ord function on real-type variables. Real types represent (in theory) an infinite set of numbers; ordinal types represent a fixed set of values.
 
Range サンプルと同様のプログラムを、実数型で作るわけには行かない。 というのは、High や Low、Ord 関数などを、実数型変数に使うわけには行かないからだ。 実数型は、( 理屈上は ) 無限の数値を扱うのに、順序数というのは、有限個の数値を扱うに過ぎないから。
 
Note: Let me explain this better. when you have the integer 23 you can determine which is the following value. Integers are finite (they have a determined range and they have an order). Floating point numbers are infinite even within a small range, and have no order: in fact, how many values are there between 23 and 24? And which number follows 23.46? It is 23.47, 23.461, or 23.4601? That's really hard to know!
 
注:こう言うと、もっと分かり易いかも知れない。 整数 23 と言った場合、君は次の数が何か、言えるだろう。 整数、と言うのは有限 ( 特定の範囲があり、順序がある ) だ。 浮動小数点値というのは、たとえ小さな範囲で扱うとしても、実際は無限で、順序が無い。 23 と 24 の間には、一体いくつ数値があるんだろう? 23.46 の次の数値、というのはいくつなんだろう? 23.47 だろうか、23.461 なのか、23.4601 かな? 誰も分からないね!
 
For this reason, it makes sense to ask for the ordinal position of the character w in the range of the Char data type, but it makes no sense at all to ask the same question about 7143.1562 in the range of a floating-point data type. Although you can indeed know whether one real number has a higher value than another, it makes no sense to ask how many real numbers exist before a given number (this is the meaning of the Ord function).
 
そういうことだから、Char 型の範囲で、w の次の文字を尋ねるのはいい。 しかし、浮動小数点型の範囲内で、7143.1562 の次は、と聞くのは、馬鹿げている。 もちろん、どちらの実数が別の実数より大きいか、を知ることはできるが、与えられた数値の前に、どれだけの実数があるかを問うのは ( これこそが、Ord 関数の役目だ )、間違っている。
 
Real types have a limited role in the user interface portion of the code (the Windows side), but they are fully supported by Delphi, including the database side. The support of IEEE standard floating-point types makes the Object Pascal language completely appropriate for the wide range of programs that require numerical computations. If you are interested in this aspect, you can look at the arithmetic functions provided by Delphi in the system unit (see the Delphi Help for more details).
 
Real 型の役目は、コードのユーザーインターフェイス部分 ( Windows 側 ) では、限られている。 しかし、Delphi では、データベースも含め、完全にサポートされている。 IEEE 標準浮動小数点型をサポートすることで、Object Pascal は、数値計算を必要とする広範なプログラムに最適のものとなっている。 これについて興味があるなら、Delphi の System ユニットに数学関数があるので、見てみるといい。 詳細は Delphi のヘルプで。
 
Note: Delphi also has a Math unit that defines advanced mathematical routines, covering trigonometric functions (such as the ArcCosh function), finance (such as the InterestPayment function), and statistics (such as the MeanAndStdDev procedure). There are a number of these routines, some of which sound quite strange to me, such as the MomentSkewKurtosis procedure (I'll let you find out what this is).
 
注:Delphi は、Math ユニットに、高度な数学ルーチンも持っている。 これには、三角関数 ( ArcCosh 関数など )、財務関数 ( InterestPayment 関数など )、統計関数 ( MeanAndStdDev 手続きなど ) がある。 ただ、この中に、どうにも訳の分からないもの、MomentSkewKurtosis 手続き、なんてのも混じっている。 ( これがどういうものか、捜してみるといい )
 
Date and Time
 
日付と時間
 
Delphi uses real types also to handle date and time information. To be more precise Delphi defines a specific TDateTime data type. This is a floating-point type, because the type must be wide enough to store years, months, days, hours, minutes, and seconds, down to millisecond resolution in a single variable. Dates are stored as the number of days since 1899-12-30 (with negative values indicating dates before 1899) in the integer part of the TDateTime value. Times are stored as fractions of a day in the decimal part of the value.
 
Delphi は、日付と時間を扱うのにも、実数型を使っている。 もっと正確に言えば、Delphi は特殊な TDateTime データ型を持っている。 これは、年、月、日、時、分、秒、ミリ秒に至るまでの値を、ひとつの変数に格納するため、浮動小数点型になっている。 日付は、1899-12-30 ( マイナスの場合は、1899 より前の日付 ) より起算した日数で、TDateTime 値の整数部分に当たる。 時間は日付の分数、という形で、値の小数点以下に格納されている。
 
TDateTime is not a predefined type the compiler understands, but it is defined in the system unit as:
 
TDateTime は、コンパイラが理解できるネイティブではないが、System ユニットで下記のように定義してある:
 
type
 TDateTime = type Double;
 
Using the TDateTime type is quite easy, because Delphi includes a number of functions that operate on this type. You can find a list of these functions in Table 3.3.
 
Delphi は、この型を扱う関数をいくつか用意してくれているので、TDateTime を使うのは、本当に簡単だ。 この関数を、表 3.3 に示す。
 
Table 3.3: System Routines for the TDateTime Type
表 3.3: TDateTime 型のシステム・ルーチン
 
Routine     Description 
ルーチン    説明
 
Now      Returns the current date and time into a single TDateTime value. 
        現在の日付と時間を、ひとつの TDateTime 値で返す。
 
Date      Returns only the current date. 
        本日を返す。
 
Time      Returns only the current time. 
        現在の時間を返す。
 
DateTimeToStr  Converts a date and time value into a string, using default formatting; to have         more control on the conversion use the FormatDateTime function instead. 
        デフォルトの書式で、日付と時間をストリングに変換する。 書式を特        定したい場合は、FormatDateTime 関数を使う。
 
DateTimeToString Copies the date and time values into a string buffer, with default formatting.         デフォルトの書式で、日付と時間をストリング・バッファにコピーする。
 
DateToStr    Converts the date portion of a TDateTime value into a string. 
        TDateTime 値の日付部分を、ストリングに変換する。
 
TimeToStr    Converts the time portion of a TDateTime value into a string. 
        TDateTime 値の時間部分を、ストリングに変換する。
 
FormatDateTime  Formats a date and time using the specified format; you can specify which          values you want to see and which format to use, providing a complex format         string. 
        日付と時間を、指定の書式にフォーマットする。 複合フォーマットの        ストリングの場合は、書式を指定できる。
 
StrToDateTime  Converts a string with date and time information to a TDateTime value,           raising an exception in case of an error in the format of the string. 
        ストリングで与えられた日付と時間を、TDateTime 値に変換する。 な        お、ストリングの書式にエラーのある場合は、例外が発生する。
 
StrToDate    Converts a string with a date value into the TDateTime format. 
        ストリングで与えられた日付を、TDateTime 値に変換する。
 
StrToTime    Converts a string with a time value into the TDateTime format. 
        ストリングで与えられた時間を、TDateTime 値に変換する。
 
DayOfWeek    Returns the number corresponding to the day of the week of the TDateTime         value passed as parameter. 
        パラメータとして渡された TDateTime 値が、週の何番目の日に当たる        か、その順番を渡す。
 
DecodeDate    Retrieves the year, month, and day values from a date value. 
        日付から、年、月、日に分解する。
 
DecodeTime   Retrieves out of a time value. 
        時間を分解する。
 
EncodeDate   Turns year, month, and day values into a TDateTime value. 
        年、月、日を、TDateTime 値に変換する。
 
EncodeTime   Turns hour, minute, second, and millisecond values into a TDateTime value.
        時、分、秒、ミリ秒を、TDateTime 値に変換する。
 
To show you how to use this data type and some of its related routines, I've built a simple example, named TimeNow. The main form of this example has a Button and a ListBox component. When the program starts it automatically computes and displays the current time and date. Every time the button is pressed, the program shows the time elapsed since the program started.
 
このデータ型と関連するルーチンをどう使うか、TimeNow というサンプルを作ってみた。 メイン・フォームには Button と ListBox を貼り付けてある。 このプログラムを起動すると、自動的に現在の時刻と日付を計算する。 ボタンを押すたびに、このプログラムが動き始めてからの経過時間を、表示する。
 
Here is the code related to the OnCreate event of the form:
 
フォームの OnCreate イベントに書いてあるコードは :
 
procedure TFormTimeNow.FormCreate(Sender: TObject);
begin
 StartTime := Now;
 ListBox1.Items.Add (TimeToStr (StartTime));
 ListBox1.Items.Add (DateToStr (StartTime));
 ListBox1.Items.Add ('Press button for elapsed time');
end;
 
The first statement is a call to the Now function, which returns the current date and time. This value is stored in the StartTime variable, declared as a global variable as follows:
 
最初の文では、現在の時刻と日付を返す、Now 関数を呼び出している。 この値は、次のようにグローバル変数として宣言している StartTime 変数に格納される。
 
var
 FormTimeNow: TFormTimeNow;
 StartTime: TDateTime;
 
I've added only the second declaration, since the first is provided by Delphi. By default, it is the following:
 
宣言はふたつあるが、僕が宣言したのは、二番目だけだ。 最初の宣言は、Dephi が行っている。 デフォルトでは、次の形式になっている。
 
var
 Form1: TForm1;
 
Changing the name of the form, this declaration is automatically updated. Using global variables is actually not the best approach: It should be better to use a private field of the form class, a topic related to object-oriented programming and discussed in Mastering Delphi 4.
 
フォームの名前を変えると、自動的にこの宣言部分が、その名前に更新される。 ただ、グローバル変数を使うのは、ベスト、とは言いにくい。 フォーム class の private 部にする方がいい。 これはオブジェクト指向プログラミング、というもので、これについては僕の著書、Mastering Delphi 4 で扱っている。
 
The next three statements add three items to the ListBox component on the left of the form, with the result you can see in Figure 3.3. The first line contains the time portion of the TDateTime value converted into a string, the second the date portion of the same value. At the end the code adds a simple reminder.
 
次の3つの文は、フォームの左側にある ListBox コンポーネントに、項目を3つ加えるものだ。 実行結果を図 3.3 に示す。 最初の行には、ストリングに変換される TDateTime 値があり、次の行には、同じ値の日付部分がある。 最後の行は、経過ボタンを押してもらわないと始まらないので、注意を促すものだ。
 
FIGURE 3.3: The output of the TimeNow example at startup.
 
図 3.3: TimeNow サンプルのスタート時
 
This third string is replaced by the program when the user clicks on the Elapsed button:
 
3番目の表示ストリングは、経過ボタンを押すと、プログラムによって変更される。
 
procedure TFormTimeNow.ButtonElapsedClick(Sender: TObject);
var
 StopTime: TDateTime;
begin
 StopTime := Now;
 ListBox1.Items [2] := FormatDateTime ('hh:nn:ss',   // 最終行に、経過時間を表示。
  StopTime - StartTime);
end;
 
This code retrieves the new time and computes the difference from the time value stored when the program started. Because we need to use a value that we computed in a different event handler, we had to store it in a global variable. There are actually better alternatives, based on classes.
 
このコードで、現在の時間を取得し、プログラムがスタートしたときからの経過時間を計算する。 別のイベント・ハンドラで取得した値を使わなくてはならないので、この値はグローバル変数に格納する。 クラスを使うと、もっと良い形で処理できる。
 
Note: The code that replaces the current value of the third string uses the index 2. The reason is that the items of a list box are zero-based: the first item is number 0, the second number 1, and the third number 2. More on this as we cover arrays.
 
注:上のコードで、最終行が index 2 になっている。 これはリスト・ボックスのインデックスが、ゼロから始まるためだ。 最初が 0、次が 1、そして3番目が 2 だ。 これについては、配列のところで詳しく紹介する。
 
Besides calling TimeToStr and DateToStr you can use the more powerful FormatDateTime function, as I've done in the last method above (see the Delphi Help file for details on the formatting parameters). Notice also that time and date values are transformed into strings depending on Windows international settings. Delphi reads these values from the system, and copies them to a number of global constants declared in the SysUtils unit. Some of them are:
 
上でやったように、TimeToStr、DateToStr 以外に、この強力な FormatDateTime 関数を使えばいい。 フォーマットについては、Delphi ヘルプを参照のこと。 注意してほしいのは、時間と日付をストリングに変換する時は、Windows の「地域のオプション」の設定に従う、というこだ。 Delphi はシステムからこの値を読み出し、SysUtils ユニットに宣言してある、グローバル定数にコピーする。 この定数の例を挙げれば、
 
DateSeparator: Char;
ShortDateFormat: string;
LongDateFormat: string;
TimeSeparator: Char;
TimeAMString: string;
TimePMString: string;
ShortTimeFormat: string;
LongTimeFormat: string;
ShortMonthNames: array [1..12] of string;
LongMonthNames: array [1..12] of string;
ShortDayNames: array [1..7] of string;
LongDayNames: array [1..7] of string;
 
More global constants relate to currency and floating-point number formatting. You can find the complete list in the Delphi Help file under the topic Currency and date/time formatting variables.
 
金額や、浮動小数点のフォーマットに関する定数もある。 これについては、Delphi のヘルプで、Currency や、日付と時間のフォーマットなどの項目を見てみるといい。
 
Note: Delphi includes a DateTimePicker component, which provides a sophisticated way to input a date, selecting it from a calendar.
 
注:Delphi には、DateTimePicker コンポーネントもある。 これは、カレンダーから日付を簡単に選択できるもので、かっこいいね。
 
Specific Windows Types
 
特殊な Windows 型
 
The predefined data types we have seen so far are part of the Pascal language. Delphi also includes other data types defined by Windows. These data types are not an integral part of the language, but they are part of the Windows libraries. Windows types include new default types (such as DWORD or UINT), many records (or structures), several pointer types, and so on.
 
今まで見てきた定義済みのデータ型は皆、Pascal の一部だ。 Delphi には、Windows で定義されているデータ型もある。 この型は Pascal の主要部分ではないが、Windows ライブラリの一部になっている。 Windows 型には、デフォルト型 ( DWORD や UINT など )、レコード ( または配列 )、いくつかのポインタ型、などがある。
 
Among Windows data types, the most important type is represented by handles, discussed in Chapter 9.
 
Windows データ型の中で、最も重要なのが、9章で扱うハンドルだ。
 
Typecasting and Type Conversions
 
型キャストと型変換
 
As we have seen, you cannot assign a variable to another one of a different type. In case you need to do this, there are two choices. The first choice is typecasting, which uses a simple functional notation, with the name of the destination data type:
 
見てきたように、型の違うものに、別の型の変数を割り当てることはできない。 どうしても、という時には、やり方がふたつある。 ひとつは、型キャストで、目標とするデータ型の名前を付けて、関数のように使えばいい。
 
var
 N: Integer;
 C: Char;
 B: Boolean;
begin
 N := Integer ('X');
 C := Char (N);
 B := Boolean (0);
 
You can typecast between data types having the same size. It is usually safe to typecast between ordinal types, or between real types, but you can also typecast between pointer types (and also objects) as long as you know what you are doing.
 
サイズが同じなら、そのまま型キャストできる。 順序型どうし、実数値どうし、で型キャストを使うのがいい。 これに加え、何をやっているのか、自分でよく分かっている時に限り、ポインタ型どうしで型キャストするのもいい。
 
Casting, however, is generally a dangerous programming practice, because it allows you to access a value as if it represented something else. Since the internal representations of data types generally do not match, you risk hard-to-track errors. For this reason, you should generally avoid typecasting.
 
しかしキャストは、一般的には危険なプログラミング技法だ。 まるで他のものを表しているかのような値にアクセスする、ということだからだ。 データ型が表している意味とは本来は合致しないので、見つけるのが非常に難しいエラーになる危険がある。 だから、一般には型キャストはしないことだ。
 
The second choice is to use a type-conversion routine. The routines for the various types of conversions are summarized in Table 3.4. Some of these routines work on the data types that we'll discuss in the following sections. Notice that the table doesn't include routines for special types (such as TDateTime or variant) or routines specifically intended for formatting, like the powerful Format and FormatFloat routines.
 
もうひとつのやり方は、型変換ルーチンを使うことだ。 表 3.4 に、型変換ルーチンの一覧表を示す。 次の章以降で取り扱うデータ型への変換も、含まれている。 ただし、この表には、特殊な型 ( TDateTime やバリアントなど ) や、強力な Format や FormatFloat ルーチンなどのような、フォーマットを目的とするルーチンは含まれていない。
 
Table 3.4: System Routines for Type Conversion
 
表 3.4 : 型変換のシステム・ルーチン
 
Routine       Description
ルーチン       説明
 
Chr      Converts an ordinal number into an ANSI character. 
        数値を、ANSI 文字に変換
 
Ord      Converts an ordinal-type value into the number indicating its order. 
        通常の型の値を、順番を示す値に変換
 
Round     Converts a real-type value into an Integer-type value, rounding its value. 
        実数型の値を、値を丸め、整数型の値に変換
 
Trunc      Converts a real-type value into an Integer-type value, truncating its value. 
        実数型の値を、小数点以下を切り捨て、整数型の値に変換
 
Int       Returns the Integer part of the floating-point value argument. 
        浮動小数点値の整数部分を返す
 
IntToStr     Converts a number into a string. 
        数値をストリングに変換
 
IntToHex    Converts a number into a string with its hexadecimal representation. 
        数値を 16 進数の形式でストリングに変換
 
StrToInt     Converts a string into a number, raising an exception if the string does not          represent a valid integer. 
        ストリングを数値に変換、ただし、ストリングが有効な整数で無い場合        は、例外が発生する。
 
StrToIntDef   Converts a string into a number, using a default value if the string is not           correct. 
        ストリングを数値に変換し、もしそのストリングが正しくない場合は、        デフォルト値を使う。
 
Val       Converts a string into a number (traditional Turbo Pascal routine, available for         compatibility). 
        ストリングを数値に変換する。 ( 伝統的な Turbo Pascal のルーチンで、        互換性のために用意されている。 )
 
Str       Converts a number into a string, using formatting parameters (traditional Turbo        Pascal routine, available for compatibility). 
        フォーマット用のパラメータを使い、数値をストリングに変換する。         ( 伝統的な Turbo Pascal のルーチンで、互換性のために用意されてい          る。 )
 
StrPas     Converts a null-terminated string into a Pascal-style string. This conversion is         automatically done for AnsiStrings in 32-bit Delphi. (See the section on strings        later in this chapter.) 
        ヌル終端文字を Pascal ストリングに変換。 32 ビット版の Delphi では、        この変換で自動的に AnsiString になる。 ( この章のストリングについ        ての部分を参照のこと。 )
 
StrPCopy    Copies a Pascal-style string into a null-terminated string. This conversion is         done with a simple PChar cast in 32-bit Delphi. (See the section on strings later        in this chapter.) 
       Pascal ストリングをヌル終端文字をに変換。 32 ビット版の Delphi では、       この変換は、PChar キャストするだけでよい。 ( この章のストリングに       ついての部分を参照のこと。 )
 
StrPLCopy   Copies a portion of a Pascal-style string into a null-terminated string. 
       Pascal ストリングの一部をヌル終端文字に変換。 
 
FloatToDecimal Converts a floating-point value to record including its decimal representation         (exponent, digits, sign). 
        浮動小数点値を、十進数の情報と共にレコードに変換。 ( ベキ数、ディ        ジィット、符号 )
 
FloatToStr   Converts the floating-point value to its string representation using default           formatting. 
       浮動小数点値を、デフォルトの書式で、そのままストリングに変換。
 
FloatToStrF  Converts the floating-point value to its string representation using the specified        formatting. 
       浮動小数点値を、指定の書式で、そのままストリングに変換。
 
FloatToText  Copies the floating-point value to a string buffer, using the specified formatting.
       浮動小数点値を、指定の書式で、ストリング・バッファにコピー。
 
FloatToTextFmt As the previous routine, copies the floating-point value to a string buffer, using        the specified formatting. 
       前のルーチンと同様に、浮動小数点値を、指定の書式で、ストリング・バ       ッファにコピー。
 
StrToFloat    Converts the given Pascal string to a floating-point value. 
        Pascal ストリングを浮動小数点値に変換。
 
TextToFloat   Converts the given null-terminated string to a floating-point value. 
        ヌル終端文字を浮動小数点値に変換。
 
Note: In recent versions of Delphi's Pascal compiler, the Round function is based on the FPU processor of the CPU. This processor adopts the so-called "Banker's Rounding", which rounds middle values (as 5.5 or 6.5) up and down depending whether they follow an odd or an even number.
 
注:最近の Delphi のコンパイラでは、Round 関数は CPU の FPU プロセッサをベースにしている。 このプロセッサはいわゆる Banker's Rounding 「銀行で使われる丸め方」という手法、与えられた値が奇数か偶数かにより、中間値 ( 5.5 や 6.5 ) を切り上げたり切り捨てたりする。
 
Conclusion
 
結論
 
In this chapter we've explored the basic notion of type in Pascal. But the language has another very important feature: It allows programmers to define new custom data types, called user-defined data types. This is the topic of the next chapter.
 
この章では、Pascal の型について、基本的な考え方を見てきた。 これ以外に、Pascal は非常に重要な特徴を持っている。 それは、プログラマが自分の新しいデータ型、ユーザー定義データ型、を定義できることだ。 次の章では、これについて取り上げる。
 
Next Chapter: User-Defined Data Types
 
次章:ユーザー定義データ型
 
c Copyright Marco Cantu, Wintech Italia Srl 1995-2000
 
 
戻る