如果问题是:“但是当涉及结构等复杂类型时,分别存储和访问每个字段是否有意义?”
然后答案是,这不是一个意义上的问题,是唯一的方法。永久存储为您提供了一种方法来存储您现在使用的每个可能的数据,并且您将来会使用这些数据。为此,它为每种数据类型提供256位映射。
如果问题是如何在永久存储中模拟结构,我会给你2美分:
假设您要翻译以下代码:
struct thing{
uint a;
}
mapping(byte32 => thing) things;
insertThing(byte32 index, uint v) … {
things[index].a = v;
}
getThingValue(byte32 thingIndex){
return things[thingIndex].a;
}
</code>
到使用永久存储的代码。我的2美分如下:
[…]
EternalStorage s;
insertThing(byte32 index, uint v){
s.setUint(keccak256(“things”, index, “a”), v);
}
getThingValue(byte32 index) returns (uint){
return s.getUint(keccak256(“things”, index, “a”));
}
</code>