2015-06-15 17:14 GMT+02:00 Matthieu Lacaton <matthieu.laca...@gmail.com>:

> Hello everyone,
>
> I have a nested struct, let's say "struct1" which contains "struct2" which
> contains an int field named "field".
>
> When I try to update the field from struct 1 directly I can't. What I mean
> is if I do : "struct1 struct2 field: 4" for instance, the value in field is
> not updated. Even if I inspect "struct1 struct2" and I manually set the
> "field variable by doing : "self field: 4" then if I go back to my struct1
> and do "self struct2 field" the value has not been updated.
> So basically the only way I have to update a field in a nested structure
> is to recreate the second struct entirely : "struct1 struct2: (Struct2 new
> field: 4)" and now it works.
>
> It seems odd because in C you can do "struct1.struct2.field = 4;" and it
> works fine.
>

yes, self struct2 will create a copy of the struct2 and any change to
struct2 value only applies to this copy.



>
> You can reproduce this with the NBTestNestedStructure class from
> NativeBoost itself :
>
> | myStruct |
>
> myStruct := (NBTestNestedStructure new oneByte: (NBTestStructure1byte new
> field: 1)).
> Transcript show: myStruct oneByte field; cr.
> myStruct oneByte field: 4.
> Transcript show: myStruct oneByte field; cr.
> myStruct oneByte: (NBTestStructure1byte new field: 4).
> Transcript show: myStruct oneByte field; cr.
>
> For me the Transcript shows 1 1 4 whereas in my opinion it should show 1 4
> 4.
>
> So am I doing something wrong or is this some kind of a bug ?
>

I am not sure, I think there is no other way to handle this kind of nested
structures.

Are you bound to exactly this structure?
If you can define the structure, you can use a nested structure that uses
pointers instead of values:

NBTestNestedStructure2>>
fieldsDesc

    ^ #(
        NBTestStructure1byte* oneByte;
        int otherField
        )

Now you can create and access the innerstructure like this

myStruct := (NBTestNestedStructure2 new oneByte: (NBTestStructure1byte
externalNew field: 1)).
myStruct oneByte field. " -> 1 "
myStruct oneByte field: 4.
myStruct oneByte field. " -> 4 "

Whereas I don't know if the external memory is freed.





> Thanks,
>
> Matthieu
>

Reply via email to