Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lyhistory/ab764cc9b000ed54b4f5213c8d5dbfa6 to your computer and use it in GitHub Desktop.
Save lyhistory/ab764cc9b000ed54b4f5213c8d5dbfa6 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >0.4.23 <0.6.0;
contract C {
uint[] data;
function f() public pure returns(uint, bool, uint){
return (7, true, 2);
}
function g() public {
// Variables declared with type and assigned from the returned tuple,
// not all elements have to be specified (but the number must match).
(uint x, ,uint y) = f();
// Common trick to swap values -- does not work for non-value storage types.
(x, y) = (y, x);
// Components can be left out (also for variable declarations).
(data.length, , ) = f(); //Sets the length to 7
}
}
pragma solidity >=0.4.0 <0.6.0;
contract InfoFeed{
function info() public payable returns(uint ret){ return 42;}
}
contract Consumer{
InfoFeed feed;
function setFeed(InfoFeed addr) public { feed = addr; }
function callFeed() public { feed.info.value(10).gas(800)();}
function acceptFunds() public payable{}
}
pragma solidity >=0.4.0 <0.6.0;
contract C {
mapping(uint => uint) data;
function f() public{
set({value: 2, key: 3});
}
function set(uint key, uint value) public{
data[key] = value;
}
}
pragma solidity ^0.5.0;
contract D {
uint public x;
constructor(uint a) public payable{
x = a;
}
}
contract C {
D d = new D(4); //will be executed as part of C's constructor
function getD() public returns(uint){
return d.x();
}
function createD(uint arg) public returns(uint){
D newD = new D(arg);
return newD.x();
}
function createAndEndowD(uint arg, uint amount) public payable returns(uint){
// Send ether along with the creation
D newD = (new D).value(amount)(arg);
return newD.x();
}
}
pragma solidity >=0.4.16 <0.6.0;
contract C {
uint[20] x;
function f() public {
g(x); //has no effect on x because it creates an independent copy of the storage value in memory
h(x); //successfully modifies x because only a reference and not a copy is passed
}
function g(uint[20] memory y) internal pure {
y[2] = 3;
}
function h(uint[20] storage y) internal {
y[3] = 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment