mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6847 from ethereum/style-fixes-libraries
[DOCS] Make libraries code examples conform to style guide
This commit is contained in:
commit
b836079006
@ -49,46 +49,48 @@ more advanced example to implement a set).
|
||||
|
||||
pragma solidity >=0.4.22 <0.7.0;
|
||||
|
||||
|
||||
library Set {
|
||||
// We define a new struct datatype that will be used to
|
||||
// hold its data in the calling contract.
|
||||
struct Data { mapping(uint => bool) flags; }
|
||||
// We define a new struct datatype that will be used to
|
||||
// hold its data in the calling contract.
|
||||
struct Data { mapping(uint => bool) flags; }
|
||||
|
||||
// Note that the first parameter is of type "storage
|
||||
// reference" and thus only its storage address and not
|
||||
// its contents is passed as part of the call. This is a
|
||||
// special feature of library functions. It is idiomatic
|
||||
// to call the first parameter `self`, if the function can
|
||||
// be seen as a method of that object.
|
||||
function insert(Data storage self, uint value)
|
||||
public
|
||||
returns (bool)
|
||||
{
|
||||
if (self.flags[value])
|
||||
return false; // already there
|
||||
self.flags[value] = true;
|
||||
return true;
|
||||
}
|
||||
// Note that the first parameter is of type "storage
|
||||
// reference" and thus only its storage address and not
|
||||
// its contents is passed as part of the call. This is a
|
||||
// special feature of library functions. It is idiomatic
|
||||
// to call the first parameter `self`, if the function can
|
||||
// be seen as a method of that object.
|
||||
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 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];
|
||||
}
|
||||
function contains(Data storage self, uint value)
|
||||
public
|
||||
view
|
||||
returns (bool)
|
||||
{
|
||||
return self.flags[value];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract C {
|
||||
Set.Data knownValues;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user