Created
September 25, 2020 07:56
-
-
Save Mayur1496/34e7fa306e202d42fc6db6f3ce38718f to your computer and use it in GitHub Desktop.
Sample Solidity Contract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.6.0 <0.8.0; | |
// This is the same code as before, just without comments | |
struct Data { mapping(uint => bool) flags; } | |
library Set { | |
function insert(Data storage self, uint value) | |
public | |
returns (bool) | |
{ | |
if (self.flags[value]) | |
return false; // already there | |
self.flags[value] = true; | |
return true; | |
} | |
function remove(Data storage self, uint value) | |
public | |
returns (bool) | |
{ | |
if (!self.flags[value]) | |
return false; // not there | |
self.flags[value] = false; | |
return true; | |
} | |
function contains(Data storage self, uint value) | |
public | |
view | |
returns (bool) | |
{ | |
return self.flags[value]; | |
} | |
} | |
contract C { | |
using Set for Data; // this is the crucial change | |
Data knownValues; | |
function register(uint value) public { | |
// Here, all variables of type Data have | |
// corresponding member functions. | |
// The following function call is identical to | |
// `Set.insert(knownValues, value)` | |
require(knownValues.insert(value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment