作用域和声明(Scoping and Declarations)
- 已声明的变量将具有其字节表示为全0的初始值。
- 变量的初始值是任何类型的典型“零状态”(“zero-state)。例如,
bool
的初始值为false
。uint
或int
类型的默认值为0
。 - 对于静态大小的数组和
bytes1
到bytes32
,每个单独的元素将被初始化为对应于其类型的初始值。 - 对于动态大小的数组、
bytes
和string
,初始值是空数组或空字符串。
在函数中任何位置声明的变量,它的作用域为整个函数,与声明位置无关。这是因为Solidity继承了JavaScript的作用域规则。这与许多语言相反——变量仅作用于声明的地方直到语句块的末尾为止。
因此,下面的代码是非法的,导致编译器抛出一个错误,标识符已经声明:
// 以下代码无法编译
pragma solidity ^0.4.16;
contract ScopingErrors {
function scoping() public {
uint i = 0;
while (i++ < 1) {
uint same1 = 0;
}
while (i++ < 2) {
uint same1 = 0;// 非法,重复声明same1
}
}
function minimalScoping() public {
{
uint same2 = 0;
}
{
uint same2 = 0;// 非法,重复声明same2
}
}
function forLoopScoping() public {
for (uint same3 = 0; same3 < 1; same3++) {
}
for (uint same3 = 0; same3 < 1; same3++) {// 非法,重复声明same3
}
}
}
除此之外,如果变量被声明,则在函数的开头将其初始化为其默认值。因此,以下代码是合法的,尽管写得不好:
pragma solidity ^0.4.0;
contract C {
function foo() public pure returns (uint) {
// baz隐式初始化为0
uint bar = 5;
if (true) {
bar += baz;
} else {
uint baz = 10;// 永远不会执行
}
return bar;// 返回 5
}
}