Skip to content

Instantly share code, notes, and snippets.

@maurelian
Last active January 17, 2018 20:54
Show Gist options
  • Save maurelian/07a0003c6376df0c7915bb3468687f54 to your computer and use it in GitHub Desktop.
Save maurelian/07a0003c6376df0c7915bb3468687f54 to your computer and use it in GitHub Desktop.
Some sample code for better understanding the implications of the storage/memory keywords when used in function arg
pragma solidity ^0.4.18;
// Some sample code for better understanding the implications of the storage/memory keywords
// when used in function args
library Libby {
struct Nums {
uint8 num;
uint8 num2;
}
function setStorageNums(Nums storage self, uint8 _num, uint8 _num2) internal {
self.num = _num;
self.num2 = _num2;
}
function setMemoryNums(Nums memory self, uint8 _num, uint8 _num2) internal {
self.num = _num;
self.num2 = _num2;
}
}
contract Fun {
using Libby for Libby.Nums;
Libby.Nums internal storageNums;
function foo() public {
Libby.Nums memory memoryNums;
storageNums.setStorageNums(1,2);
memoryNums.setStorageNums(1,2); // TypeError: Member "setStorageNums" is not available in struct Libby.Nums memory outside of storage.
}
function bar() public {
Libby.Nums memory memoryNums;
storageNums.setMemoryNums(1,2);
memoryNums.setMemoryNums(1,2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment