Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] WTF Solidity极简入门: 9. 常数 constant和immutable #855

Open
1 task
banxiu opened this issue Jan 8, 2025 · 2 comments
Open
1 task

[Doc] WTF Solidity极简入门: 9. 常数 constant和immutable #855

banxiu opened this issue Jan 8, 2025 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@banxiu
Copy link

banxiu commented Jan 8, 2025

Details(细节)

https://www.wtf.academy/docs/solidity-101/Constant/
wtf solidity 101 的 常量那里描述的 immutable 初始化之后 ,构造函数里重新赋值会报编译错的问题,可能有点错误。
本地测试:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract SolidityConstant {
// 利用constructor初始化immutable变量,因此可以利用
// immutable变量可以在constructor里初始化,之后不能改变
uint256 public immutable IMMUTABLE_NUM = 9999999999;
constructor() {
// Right use case
IMMUTABLE_NUM = 123; // 这里编译通过 Remix 并没有提示编译错误
}
}

Are you willing to submit a PR?(你愿意提交PR吗?)

  • Yes, I am willing to submit a PR!(是的, 我愿意提交PR)
@banxiu banxiu added the documentation Improvements or additions to documentation label Jan 8, 2025
@banxiu banxiu changed the title [Doc] [Doc] WTF Solidity极简入门: 9. 常数 constant和immutable Jan 8, 2025
@XdpCs
Copy link
Collaborator

XdpCs commented Jan 8, 2025

在Solidity v8.0.21以后,immutable变量不需要显式初始化。反之,则需要显式初始化。 若immutable变量既在声明时初始化,又在constructor中初始化,会使用constructor初始化的值。

@XdpCs
Copy link
Collaborator

XdpCs commented Jan 8, 2025

请提供更详细的信息 @banxiu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants