You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
bytes memory b;
b = abi.encodePacked("hello");
b = abi.encodePacked(b, " world");
string memory s = string(b);
Alternative method - need to compare which one is more gas efficient:
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract C {
using strings for *;
string public s;
function foo(string s1, string s2) {
s = s1.toSlice().concat(s2.toSlice());
}
}
struct User {
uint id;
string name;
}
...
User memory user = User(1, "James");
//Method 2
User memory user = User({id: 1, name: "James"}); //parameters can be out-of-order
Struct with nested mapping
How to create a new struct and put it in a mapping of struct?
Struct User {
uint id;
string name;
mapping(uint => true) isOwnerOf;
}
mapping(uint => users);
...
//cannot do this as `memory` mapping are not possible
User memory user = User(..., mapping(1 => true));
users[0] = user;
//Instead, have to leverage the fact that its possible to access any mapping entry, including non-existing one:
users[0].id = 0;
users[0].name = "James";
users[0].isOwnerOf[10] = true;
I don't think the random number generator is necessarily safe. it could be manipulated by malicious miners.