|
List Info
Thread: Object mutability
|
|
| Object mutability |

|
2006-04-17 19:08:17 |
|
On 4/17/2006, 8:02:17 AM, Jeremy Tregunna wrote:
>
> For a long time I've wanted the ability to make sure that a slot
> is not overridden (or updated later) based on a flag on the object.
> Currently, I've implemented this feature in Io (code attached).
>
> For example:
>
> Foo := Object clone do(blurgle :=42)
> Bar := Foo clone do(blurgle := "fourty-two")
> Bar blurgle setIsMutable(false)
> Qux := Bar clone
> Qux blurgle := 123 // results in an exception being thrown
> f := Foo clone
> f blurgle := 123 // no exception
>
> Anyway, I'm curious as to what people think of this sort of idea.
Reasonable idea, but keep in mind that there is a difference between
an immutable slot and an immutable value. Your code seems to disallow
changing a slot when its value's isMutable = false, which means that
every slot that points to that value becomes immutable.
e.g. isMutable is set for strings, so, using your code, it is
impossible to change a slot that points to a string.
You probably want to use a map to determine which slots are immutable,
or create an explicit Slot primitive, which I've suggested and been
pondering for a long while (though that would require larger changes).
Kevin
|
| Object mutability |

|
2006-04-18 05:28:37 |
|
On 17-Apr-06, at 3:08 PM, Kevin Edwards wrote: On 4/17/2006, 8:02:17 AM, Jeremy Tregunna wrote: > > For a long time I've wanted the ability to make sure that a slot > is not overridden (or updated later) based on a flag on the object. > Currently, I've implemented this feature in Io (code attached). > > For example: > > Foo := Object clone do(blurgle :=42) > Bar := Foo clone do(blurgle := "fourty-two") > Bar blurgle setIsMutable(false) > Qux := Bar clone > Qux blurgle := 123 // results in an exception being thrown > f := Foo clone > f blurgle := 123 // no exception > > Anyway, I'm curious as to what people think of this sort of idea. Reasonable idea, but keep in mind that there is a difference between an immutable slot and an immutable value. Your code seems to disallow changing a slot when its value's isMutable = false, which means that every slot that points to that value becomes immutable. e.g. isMutable is set for strings, so, using your code, it is impossible to change a slot that points to a string.
Right, this is more proof of concept. Obviously, a more complete example would also add checks for removeSlot, etc. The goal of this would be to make sure that you cannot alter the value of the slot, or remove it (though as you can see, the removeSlot checking is omitted).
You probably want to use a map to determine which slots are immutable, or create an explicit Slot primitive, which I've suggested and been pondering for a long while (though that would require larger changes).
I would actually, if I were to implement this with the intention of inclusion with Io; do it in C; tag a one bit flag onto the IoObject structure and figure out a better name.
Kevin
|
| Object mutability |

|
2006-04-18 17:53:06 |
|
On 4/18/2006, 12:28:37 AM, Jeremy Tregunna wrote:
> On 17-Apr-06, at 3:08 PM, Kevin Edwards wrote:
>>
>> Reasonable idea, but keep in mind that there is a difference
>> between an immutable slot and an immutable value. Your code seems
>> to disallow changing a slot when its value's isMutable = false,
>> which means that every slot that points to that value becomes
>> immutable.
>>
>> e.g. isMutable is set for strings, so, using your code, it is
>> impossible to change a slot that points to a string.
>
> Right, this is more proof of concept. Obviously, a more complete
> example would also add checks for removeSlot, etc. The goal of this
> would be to make sure that you cannot alter the value of the slot,
> or remove it (though as you can see, the removeSlot checking is
> omitted).
>
>> You probably want to use a map to determine which slots are
>> immutable, or create an explicit Slot primitive, which I've
>> suggested and been pondering for a long while (though that would
>> require larger changes).
>
> I would actually, if I were to implement this with the intention of
> inclusion with Io; do it in C; tag a one bit flag onto the IoObject
> structure and figure out a better name.
In this post, I'll rename your mutability flag to "isSlotMutable", in
order to distinguish it from Sequence's "isMutable".
Just to clarify, my intent was to suggest that you might not want to
hold your "isSlotMutable" flag with each value, since you aren't
enforcing mutability of the value, but rather mutability of slots. Is
your goal to make a particular (inherited) slot immutable, or every
slot that points to the same value?
Continuing your example:
> Foo := Object clone do(blurgle :=42)
> Bar := Foo clone do(blurgle := "fourty-two")
> Bar blurgle setIsSlotMutable(false)
> Qux := Bar clone
> Qux blurgle := 123 // results in an exception being thrown
> f := Foo clone
> f blurgle := 123 // no exception
f blurgle := Bar blurgle
x := "fourty-two"
x := "fourty-three" // results in an exception being thrown
(1) We want the value that is in "Bar blurgle", but that comes with
your "isSlotMutable" flag, which means that the slot "f blurgle" will
no longer be changeable. My guess is that you want the slot "Bar
blurgle" and its inheritors to be immutable, not all slots that point
to the same value.
(2) Similarly, the above code would cause the slot "x" to become
immutable, because in Io, strings with the same value are shared.
Kevin
|
[1-3]
|
|