TVM-Solidity-Compiler icon indicating copy to clipboard operation
TVM-Solidity-Compiler copied to clipboard

Consider adding unit type to ton-solidity

Open medved239 opened this issue 3 years ago • 1 comments

Suppose we want to store a set of some entries: for example, a set of already presented hashes for reply-protection. The conventional solidity-way is to make a map like mapping(uint256 => bool) hashUsed and set hashUsed[h] = true whenever hash h is presented. It is quite straight-forward because in Solidity there is no way to check whether a specific key is presented in the map -- they all are presented and initialized with default value. However, in Ton-Solidity we have .exists() method, so it doesn't make sense to store a boolean value in the map -- we can just store nothing! Thus, using unit type (which unique value serializes to empty builder) in mapping(uint256 => Unit) efficiently allows us to use maps as sets. You can even add a new type set(...), internally represented as mapping with Unit value type. After all, it could allow to write more structured code.

medved239 avatar Jun 14 '21 13:06 medved239

Actually it could be implemented as empty structure struct Unit{}, but compiler tells that defining empty structures is disallowed.

medved239 avatar Jun 14 '21 13:06 medved239

You can use mapping(uint256 => TvmSlice)

IgorKoval avatar Apr 19 '23 09:04 IgorKoval