简体   繁体   English

在可靠的映射中保存不可变变量?

[英]saving an immutable variable in a mapping in solidity?

我正在尝试在任何想法下面做类似这段代码的事情?

    mapping (uint256 => string immutable) public IdToAddress ;

A map is a hash table.地图是一个哈希表。 You cannot, therefore, set the value type to be immutable.因此,您不能将值类型设置为不可变。 Think of a map as a "key: value" pair where idToAddress[uint256] will return a string from the hash table in your example.将映射视为“键:值”对,其中idToAddress[uint256]将从您的示例中的哈希表返回一个字符串。 It is not an array of data where you can return data by index.它不是可以按索引返回数据的数据数组。 Not to mention that your syntax is wrong if this were to be introduced to solidity.更不用说你的语法是错误的,如果这被引入到solidity中。 Please note that you cannot set the data type after it is named.请注意,命名后不能设置数据类型。

Therefore, you cannot accomplish this with mapping.因此,您无法通过映射完成此操作。

You can read more in the solidity to find a use case that can suit your need: https://docs.soliditylang.org/en/v0.8.15/types.html?highlight=mapping#mapping-types您可以在solidity 中阅读更多内容以找到适合您需要的用例: https ://docs.soliditylang.org/en/v0.8.15/types.html?highlight=mapping#mapping-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM