The following code doesn't compile, because compiler needs a mapping key to be of fixed-size type in order to create accessor for a public mapping. I'm using here string type. My strings are 24 byte hex strings.
My question:
How would you cast string to any fixed-size type that can be used as a mapping key?
Do you have any advice on how to make the code more effiecent?
pragma solidity ^0.4.6;
contract C {
struct User {
uint balance;
}
mapping (string => User) public accounts;
function newUser(string id, uint balance) {
accounts[id] = User(balance);
}
}
Error: Unimplemented feature: Accessors for mapping with dynamically-sized keys not yet implemented.
User
in a public function. Solidity has a nice little feature where you can return a custom struct from a public mapping, but only through a localcall
, not through an actual EVM operation. Dynamic keys mess with that