Constants and Literals

Allowable Types for Constants
常量允许的类型

Slice allows you to define constants for the following types:

An integral type (bool, byte, short, int, long)
A floating point type (float or double)
string
enum

Slice 允许定义以下类型的常量:
整型(bool、byte、short、int、long)
浮点类型(float 或 double)
字符串
枚举

Here are a few examples:

1
2
3
4
5
6
7
8
9
10
11
module M
{
const bool AppendByDefault = true;
const byte LowerNibble = 0x0f;
const string Advice = "Don't Panic!";
const short TheAnswer = 42;
const double PI = 3.1416;

enum Fruit { Apple, Pear, Orange }
const Fruit FavoriteFruit = Pear;
}

The syntax for literals is the same as for C++ and Java (with a few minor exceptions).
文字的语法与 C++ 和 Java 相同(有一些小例外)。

Boolean constants
Boolean constants can only be initialized with the keywords false and true. (You cannot use 0 and 1 to represent false and true.)
布尔常量只能使用关键字 false 和 true 进行初始化。 (不能用 0 和 1 来表示 false 和 true。)

Integer literals
Integer literals can be specified in decimal, octal, or hexadecimal notation.
整数可以用十进制、八进制或十六进制表示法指定。

For example:

1
2
3
const byte TheAnswer = 42;
const byte TheAnswerInOctal = 052;
const byte TheAnswerInHex = 0x2A;

Be aware that, if you interpret byte as a number instead of a bit pattern, you may get different results in different languages. For example, for C++, byte maps to unsigned char whereas, for Java, byte maps to byte, which is a signed type.

Note that suffixes to indicate long and unsigned constants (l, L, u, U, used by C++) are illegal:

请注意,如果将字节解释为数字而不是位模式,则在不同语言中可能会得到不同的结果。 例如,对于 C++,字节映射到 unsigned char,而对于 Java,字节映射到 byte,这是一种有符号类型。
请注意,表示长整型和无符号常量(C++ 使用的 l、L、u、U)的后缀是非法的:

1
2
const long Wrong = 0u;          // Syntax error
const long WrongToo = 1000000L; // Syntax error

The value of an integer literal must be within the range of its constant type, as shown in the Built-In Basic Types table; otherwise the compiler will issue a diagnostic.
整数的值必须在其常量类型的范围内,如内置基本类型表所示; 否则编译器将发出诊断信息。

Floating-point literals
Floating-point literals use C++ syntax, except that you cannot use an l or L suffix to indicate an extended floating-point constant; however, f and F are legal (but are ignored).
浮点文字使用 C++ 语法,但不能使用 l 或 L 后缀来指示扩展浮点常量; 然而,f 和 F 是合法的(但被忽略)。
Here are a few examples:

1
2
3
4
5
6
const float P1 = -3.14f;    // Integer & fraction, with suffix
const float P2 = +3.1e-3; // Integer, fraction, and exponent
const float P3 = .1; // Fraction part only
const float P4 = 1.; // Integer part only
const float P5 = .9E5; // Fraction part and exponent
const float P6 = 5e2; // Integer part and exponent

Floating-point literals must be within the range of the constant type (float or double); otherwise, the compiler will issue a diagnostic.
浮点变量必须在常量类型(float 或 double)的范围内; 否则,编译器将发出诊断信息。

String literals
Slice string literals support the same escape sequences as C++, with the exception of hexadecimal escape sequences that are limited to two hexadecimal digits.
Slice字符串变量支持与 C++ 相同的转义序列,但十六进制转义序列除外,该序列仅限于两个十六进制数字。

Escape Sequence Name Corresponding ASCII or Unicode Code Point Notes
\' single quote 0x27
\" double quote 0x22
\? question mark 0x3f
\\ backslash 0x5c
\a audible bell 0x07
\b backspace 0x08
\f form feed 0x0c
\n line feed 0x0a
\r carriage return 0x0d
\t horizontal tab 0x09
\v vertical tab 0x0b
\nnn octal escape sequence 1 to 3 octal digits (0-7) that represent a byte value between 0 and 255
\xnn hexadecimal escape sequence 1 to 2 hexadecimal digits (0-9, a-f, A-F)
\unnnn universal character name U+nnnn Exactly 4 hexadecimal digits. Use the \Unnnnnnnn notation for astral characters.
\Unnnnnnnn universal character name U+nnnnnnnn Exactly 8 hexadecimal digits.

A backslash () followed by another character is simply preserved as is.

Octal and hexadecimal escape sequences can represent ASCII characters (ordinal value 0 to 127) or the UTF-8 encoding of non-ASCII characters.

A string literal can contain printable ASCII characters (including the escape sequences presented above) and non-ASCII characters; non-printable ASCII characters (such as an unescaped tab) are not allowed.

反斜杠 () 后跟另一个字符将按原样保留。
八进制和十六进制转义序列可以表示 ASCII 字符(序数值 0 到 127)或非 ASCII 字符的 UTF-8 编码。
字符串文字可以包含可打印的 ASCII 字符(包括上面介绍的转义序列)和非 ASCII 字符; 不允许使用不可打印的 ASCII 字符(例如未转义的制表符)。

Here are some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const string AnOrdinaryString = "Hello World!";

const string DoubleQuote = "\"";
const string TwoSingleQuotes = "'\'"; // ' and \' are OK
const string QuestionMark = "\?";
const string Backslash = "\\";
const string AudibleBell = "\a";
const string Backspace = "\b";
const string FormFeed = "\f";
const string Newline = "\n";
const string CarriageReturn = "\r";
const string HorizontalTab = "\t";
const string VerticalTab = "\v";

const string OctalEscape = "\007"; // Same as \a
const string HexEscape1 = "\x07"; // Ditto
const string HexEscape2 = "\x41F"; // Same as AF
const string Universal1 = "\u0041"; // Same as A
const string Universal2 = "\U00000041"; // Ditto

const string EuroSign1 = "€"; // Euro sign (U+20AC)
const string EuroSign2 = "\u20AC"; // Euro sign as a short universal character name
const string EuroSign3 = "\U000020ac"; // Euro sign as a long universal character name
const string EuroSign4 = "\xe2\x82\xAC"; // Euro sign in UTF-8 encoding, using hex escape sequences
const string EuroSign5 = "\342\202\254"; // Euro sign in UTF-8 encoding, using octal escape sequences
const string EuroSign6 = "\342\x82\254"; // Euro sign in UTF-8 encoding, using a mix or hex and octal escape sequences
1
const string NullString = null;    // Illegal!

Null strings simply do not exist in Slice and, therefore, do not exist as a legal value for a string anywhere in the Ice platform. The reason for this decision is that null strings do not exist in many programming languages.
空字符串根本不存在于 Slice 中,因此在 Ice 平台中的任何位置都不作为字符串的合法值存在。 做出此决定的原因是许多编程语言中不存在空字符串。

Constant Expressions
A constant definition may also refer to another constant. It is not necessary for both constants to have the same Slice type, but the value of the existing constant must be compatible with the type of the constant being defined.

常量定义也可以引用另一个常量。 两个常量不必具有相同的 Slice 类型,但现有常量的值必须与正在定义的常量的类型兼容。

Consider the examples below:

1
2
3
4
5
const int SIZE = 500;

const int DEFAULT_SIZE = SIZE; // OK
const short SHORT_SIZE = SIZE; // OK
const byte BYTE_SIZE = SIZE; // ERROR

The DEFAULT_SIZE constant is legal because it has the same type as SIZE, and SHORT_SIZE is legal because the value of SIZE (500) is within the range of the Slice short type. However, BYTE_SIZE is illegal because the value of SIZE is outside the range of the byte type.
DEFAULT_SIZE 常量是合法的,因为它与 SIZE 具有相同的类型,SHORT_SIZE 是合法的,因为 SIZE (500) 的值在 Slice Short 类型的范围内。 但是,BYTE_SIZE 是非法的,因为 SIZE 的值超出了字节类型的范围。