Are there null like thing in solidity

前端 未结 5 1937
旧巷少年郎
旧巷少年郎 2021-02-05 05:33
    struct buyer{
       uint amount;
       Status status;
    }

    mapping(address=>buyer) public buyers;
    mapping(uint=>address) buyerIndex;
    uint publi         


        
5条回答
  •  时光取名叫无心
    2021-02-05 06:11

    As Viktor said, default value for all possible values in mapping is zero. So if a buyer has not already inserted in mapping, the amount value for that address will be zero. But this approach has a flaw, if a buyer does exists but its balance became zero after some operations, you will treat it as it does not exist.

    I think the best approach is to add a exists member to the buyer struct with bool type. Default value for this member is false and when the buyer get created, you initialize it with true value. So you can check exactly if a buyer exist or not via this member.

    Buyer struct:

    struct buyer{
       uint amount;
       Status status;
       bool exists;
    }
    

    Initialize buyer:

    buyer memory b = buyer(0, status, true);
    

    Check if buyer exists:

    if(buyers[msg.sender].exists) {
      //so can buy
    }
    

提交回复
热议问题