I have a mapping(string => Person) public map:

struct Person {
        string name;
        string description;
        address primaryAddress;
        string linkToWebsite;
        string linkToPicture;
        address secondaryAddress;
        uint age;
}

, this struct is only made of strings, addresses and uints, but when compiling I get this error.

Internal compiler error: Accessors for mapping with dynamically-sized keys not yet implemented.

BUT,

From Solidity Features:

Strings as Mapping Keys

Strings are allowed as keys for mappings.

contract C {   mapping (string => uint) counter;   
function inc(string _s) { counter[_s]++; } }

Any help with it?

up vote 19 down vote accepted

The thing was that my mapping was public, and there are no public accessors implemented for it yet. The solution is just to not declare the mapping as public.

  • Why do you need a public accessor? Shouldn't it generate the getter? "It is possible to mark mappings public and have Solidity create a getter. The _KeyType will become a required parameter for the getter and it will return _ValueType." source – obesechicken13 Mar 6 at 20:18
  • I declared my mapping as private to solve this – AlexG Mar 10 at 10:19

Strings are allowed as keys in mapping. For example, this code works just fine testing in Pyethereum:

 from ethereum import tester as t

 code = '''
    contract Example {
       struct Person {
         string name;
       }
       mapping(string => Person) map;

       function setName(string name, string value) {
          map[name] = Person({name: value});
       }

       function getName(string key) constant returns (string) {
         return map[key].name;
       }

   }
  '''

   state = t.state()
   contract =  state.abi_contract(code, language='solidity')
   contract.setName('dave', 'hello')
   print contract.getName('dave')
   => 'hello'

There must be something going on in your actual contract code

  • what can make this error to appear? – arodriguezdonaire Mar 29 '16 at 15:41
  • I don't know without seeing the whole contract. Are you using a newer version of the solidity compiler? – dbryson Mar 29 '16 at 16:40
  • The most recently one – arodriguezdonaire Mar 29 '16 at 16:41
  • I would recommend working through incremental, simpler, versions of your contract and compiling until you find the problem. Try my code above for starters to see if it works in your setup – dbryson Mar 29 '16 at 16:47
  • I found the solution, thank you for your help – arodriguezdonaire Mar 29 '16 at 20:22

without seeing your struct it's hard to guess, is this helping ?

contract structtestmapping {
    struct myStruct {
        uint id;
    }
    mapping(string => myStruct) mapmystruct;
    myStruct s = mapmystruct['mystring'];

}

your struct might contain dynamically sized elements as the code here suggests ?

  • this is not helpful for me, I want to access to the mapping with a string key which is given by parameter, but thank you anyway. I have strings and addresses in the struct. – arodriguezdonaire Mar 29 '16 at 14:35
  • a string is considered a dynamically sized element? – arodriguezdonaire Mar 29 '16 at 15:33
  • I think so if this is correct, wouldn't bet my hand on it though github.com/ethereum/wiki/wiki/… : Re-introduce string type PT string is added as a type which behaves exactly like bytes with the following differences: index access is not allowed it does not have a length member In the ABI encoding (wiki already changed), string is a dynamic type whose "number of elements" field is the number of bytes, not the number of characters. The encoding of the string is assumed to be UTF-8, but is not yet used inside Solidity. – euri10 Mar 29 '16 at 15:45
  • Is it possible then to fix the size of the strings in my struct? – arodriguezdonaire Mar 29 '16 at 15:47
  • Also @dbryson has a string in his struct and his solution works – arodriguezdonaire Mar 29 '16 at 15:48

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.