1

user defined value types allow creating a zero cost abstraction over an elementary value type. This is similar to an alias, but with stricter type requirements.

unfortunately, solc 0.8.9 says

TypeError: Unary operator delete cannot be applied to type MyType

type TokenId is uint256;
...
mapping(bytes32 => MyType) public myMapping;
...
delete myMapping[myId];

Is there a way to keep delete for readability, use the type definition, and avoid multiple additional wrap and unwrap ?

2
  • can you add the MyType struct/type with your question?
    – hack3r_0m
    Nov 2 '21 at 11:46
  • @hack3r_0m I read the question as they were asking the case MyType = TokenId, but I could be wrong.
    – Ismael
    Nov 3 '21 at 17:37
1
+50

I think we can not delete an entry of mapping when using User Defined Value Types. The reason here is no initial default value of user-defined types.

As explained in solidity docs here and here

A user defined value type is defined using type C is V, where C is the name of the newly introduced type and V has to be a built-in value type (the “underlying type”).

The type C does not have any operators or bound member functions. In particular, even the operator == is not defined. Explicit and implicit conversions to and from other types are disallowed.

What delete a does is it assigns the initial value for the type to a. It is important to note that delete a really behaves like an assignment to a, i.e. it stores a new object in a.

I think since user-defined types have no operators or bound members functions assigning an initial of user defined (inherited from the underlying type) type is not possible.

0

No. You can't delete a mapping entry. You can write over it or set it back to false but you can't delete.

6
  • The line delete myMapping[myId] does work when myMapping is declared as mapping(bytes32 => uint256).
    – Ismael
    Nov 1 '21 at 21:32
  • You might be mistaking with Struct and mapping that point to struct Nov 3 '21 at 2:28
  • Declaring mapping(bytes32 => uint256) public myMapping; and then executing in a function bytes32 myId = 0x1234...; delete myMapping[myId]; works in solidity v0.8.9.
    – Ismael
    Nov 3 '21 at 4:52
  • delete myMapping[myId] is possible whereas delete myMapping is not valid. Because can't set all the mapping key-value pairs to their initial value as keys are arbitrary and unknown. Whereas it is possible to delete a where a can be dynamic array or static array, it will set all elementals to their initial value. Nov 3 '21 at 23:48
  • So why is this question still open?? Nov 4 '21 at 3:42
0

I'm not sure if it's an appropriated way but that's how i create and destroy a new mapping :

contract Mapping {

    mapping (uint256 => uint256) public map;
    // workaround because i can't find a way to directly access to the map on contract MyContract
    function write(uint256 index, uint256 data) public {
        map[index] = data;
    }
}

contract MyContract {
    
    Mapping big_array = new Mapping();
    
    function write(uint256 index, uint256 data) public {
        big_array.write(index,data);
    }
    
    function read(uint256 index) public returns (uint256) {
        return big_array.map(index); // it works, i would like something like that instead of Mapping.write
    }
    
    function newMap() public {
        delete big_array;
        big_array = new Mapping();    
    }
    
}

but ofc it's only useful if you want to delete the whole entries not one in particular.

1
  • 1
    The question is about deleting an entry of the mapping.
    – Ismael
    Nov 1 '21 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.