为什么在 solidity 中没有将 Null 初始化为无符号整数的概念?
Why there's no concept of Null initialization to an unsigned integer in solidity?
我正在编写代码,我的无符号整数值初始化为 0,如下所示
我的代码:
// SPDX-License-Identifier: UNLICENSED
pragma solidity >= 0.5.0 < 0.9.0;
contract PT_StateVariable
{
uint256 public age ;
}
部署选项卡
但是当我试图给它一个NULL时,它报错了。
为什么在 Solidity 中没有使用 NULL 概念(就像在 C++ 中一样)?
在 Solidity 中 不存在 'NULL' 的概念,如 C++、C#、Java、...
文档是这样说的:
The concept of “undefined” or “null” values does not exist in
Solidity, but newly declared variables always have a default value
dependent on its type. To handle any unexpected values, you should use
the revert function to revert the whole transaction, or return a tuple
with a second bool value denoting success.
我建议您阅读文档中关于 declarations 的部分:
A variable which is declared will have an initial default value whose
byte-representation is all zeros. The “default values” of variables
are the typical “zero-state” of whatever the type is. For example, the
default value for a bool is false. The default value for the uint or
int types is 0. For statically-sized arrays and bytes1 to bytes32,
each individual element will be initialized to the default value
corresponding to its type. For dynamically-sized arrays, bytes and
string, the default value is an empty array or string. For the enum
type, the default value is its first member.
更多信息here。
我正在编写代码,我的无符号整数值初始化为 0,如下所示
我的代码:
// SPDX-License-Identifier: UNLICENSED
pragma solidity >= 0.5.0 < 0.9.0;
contract PT_StateVariable
{
uint256 public age ;
}
部署选项卡
但是当我试图给它一个NULL时,它报错了。
为什么在 Solidity 中没有使用 NULL 概念(就像在 C++ 中一样)?
在 Solidity 中 不存在 'NULL' 的概念,如 C++、C#、Java、... 文档是这样说的:
The concept of “undefined” or “null” values does not exist in Solidity, but newly declared variables always have a default value dependent on its type. To handle any unexpected values, you should use the revert function to revert the whole transaction, or return a tuple with a second bool value denoting success.
我建议您阅读文档中关于 declarations 的部分:
A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false. The default value for the uint or int types is 0. For statically-sized arrays and bytes1 to bytes32, each individual element will be initialized to the default value corresponding to its type. For dynamically-sized arrays, bytes and string, the default value is an empty array or string. For the enum type, the default value is its first member.
更多信息here。