The names
of variables, constants, types, fields, properties, procedures, functions,
programs, units, libraries, and packages are called identifiers.
(Numeric constants like 26057 are not identifiers.) Identifiers must be declared
before you can use them; the only exceptions are a few predefined types,
routines, and constants that the compiler understands automatically, the
variable Result when it occurs inside a function block, and the variable
Self when it occurs inside a method implementation.
A
declaration defines an identifier and, where appropriate, allocates memory for
it. For example,
var
Size: Extended;
declares a variable called Size that
holds an Extended (real) value, while
function
DoThis(X, Y: string): Integer;
declares a function called DoThis
that takes two strings as arguments and returns an integer. Each declaration
ends with a semicolon. When you declare several variables, constants, types, or
labels at the same time, you need only write the appropriate reserved word
once:
var
Size: Extended;
Quantity: Integer;
Description: string;
The syntax
and placement of a declaration depend on the kind of identifier you are
defining. In general, declarations can occur only at the beginning of a block
or at the beginning of the interface or implementation section of a unit (after
the uses clause). Specific conventions for declaring variables,
constants, types, functions, and so forth are explained in the documentation
for those topics.
The hint
directives platform, deprecated, and library may be
appended to any declaration. In the case of a procedure or function
declaration, the hint directive should be separated from the rest of the
declaration with a semicolon. Examples:
procedure
SomeOldRoutine; stdcall; deprecated;
var
VersionNumber: Real library;
type
AppError = class(Exception)
...
end
platform;
When source code is compiled in the {$HINTS ON} {$WARNINGS ON} state, each reference to an identifier declared with one of these directives generates an appropriate hint or warning. Use platform to mark items that are specific to a particular operating environment (such as Windows or Linux), deprecated to indicate that an item is obsolete or supported only for backward compatibility, and library to flag dependencies on a particular library or component framework (such as VCL or CLX).
About declarations and statements
变量、常量、类型、域、属性、过程、函数、程序、单元、库、包等等,它们的名字都叫做标识符。(数字常量不是标识符,如26057。)标识符在能使用之前必须被声明,除了极少数能被编译器自动识别的预定义类型和例程。变量Result出现在函数块中,变量Self出现在方法实现中。
一个声明定义一个标识符,并且适当的情况下为标识符分配内存。例如,
var
Size: Extended;
声明了一个叫做Size的变量,该变量用于保存Extended(实数)值,而
function
DoThis(X, Y: string): Integer;
生命了一个叫做DoThis的函数,该函数接受两个串作为参数并返回一个整数。所有的声明都以分号(;)结束。要同时声明几个变量、常量、类型或标号,可以只写一次适当的保留字。
var
Size: Extended;
Quantity: Integer;
Description: string;
声明的语法和布局取决于被声明的标识符的种类。通常,声明只出现在块、接口节或实现节的首部(在uses子句之后)。对于声明变量、常量、类型、函数等,还有一些各自的约定,相关说明在相应的主题文档中。
提示指示(hint directives)字platform、deprecated和library等可以附加到任何说明中。对于过程和函数声明的情况,提示指示字应通过分号(;)与声明分开。例如:
procedure
SomeOldRoutine; stdcall; deprecated;
var
VersionNumber: Real library;
type
AppError = class(Exception)
...
end
platform;
当源代码在编译指示 {$HINTS ON} {$WARNINGS ON} 状态下编译时,对于声明中含有提示指示字的标识符,编译器对每次此类标识符的引用都产生适当的提示或警告。用platform可以标记那些具体到特别操作环境(如Windows或Linux)的声明项;用deprecated可以指出声明项是废弃的(或仅向后兼容,即向旧的版本兼容);用library可以标记对特殊库或组件框架(如VCL或CLX)的依赖关系。