[squeak-dev] Immutability, what it is, why

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Immutability, what it is, why

Janko Mivšek
Dear Squeakers,

A bit explanation of immutability will help you understand what it is
good for and why it is good to be introduced in Squeak.

    1. immutable object = read only object. That means you can read it
but not write/change its state. Where is this good for?

    2. Literals should be immutable by default. Like 'this is a literal
string'. Most of debate so far were about such immutable objects.

    3. But the immutability become really powerful for persistence
support. That is, as transparent persistence as possible, without any
special commands for storing objects into a database. How can this be
achieved with immutability?

    4. With a clever trick: object to be saved into a database
(persistent object) is declared as immutable. When we try to change it,
an exception occurs. In exception handler we temporary declare object as
read/write, allow the change then declare object back to immutable. We
also put that object into a set of changed or so called 'dirty' objects.
All we need to do now is to save them to a database during a commit.

    5. That's how the Gemstone persistency is implemented completely
transparently on VisualWorks, which supports immutability for a while.

    6. Consider how transparent can become Magma with immutability in
Squeak!

    7. Consider many other uses when you need to detect object change.
We can for instance implement logging of changes into a file and
reverting in case of image crash, just as we are doing now with the code
changes.

In any case introduction of immutability opens us many new
opportunities, which outnumber the small performance penalty it introduces.


Best regards
Janko


--
Janko Mivšek
AIDA/Web
Smalltalk Web Application Server
http://www.aidaweb.si

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Immutability, what it is, why

Randal L. Schwartz
>>>>> "Janko" == Janko Mivšek <[hidden email]> writes:

Janko> A bit explanation of immutability will help you understand what it is
Janko> good for and why it is good to be introduced in Squeak.

This is absolutely a good thing.  I'm perhaps a little surprised
that it hasn't happened already. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Immutability, what it is, why

Igor Stasenko
In reply to this post by Janko Mivšek
2008/7/30 Janko Mivšek <[hidden email]>:

> Dear Squeakers,
>
> A bit explanation of immutability will help you understand what it is good
> for and why it is good to be introduced in Squeak.
>
>   1. immutable object = read only object. That means you can read it but not
> write/change its state. Where is this good for?
>
>   2. Literals should be immutable by default. Like 'this is a literal
> string'. Most of debate so far were about such immutable objects.
>
>   3. But the immutability become really powerful for persistence support.
> That is, as transparent persistence as possible, without any special
> commands for storing objects into a database. How can this be achieved with
> immutability?
>
>   4. With a clever trick: object to be saved into a database (persistent
> object) is declared as immutable. When we try to change it, an exception
> occurs. In exception handler we temporary declare object as read/write,
> allow the change then declare object back to immutable. We also put that
> object into a set of changed or so called 'dirty' objects. All we need to do
> now is to save them to a database during a commit.
>
>   5. That's how the Gemstone persistency is implemented completely
> transparently on VisualWorks, which supports immutability for a while.
>
>   6. Consider how transparent can become Magma with immutability in Squeak!
>
>   7. Consider many other uses when you need to detect object change. We can
> for instance implement logging of changes into a file and reverting in case
> of image crash, just as we are doing now with the code changes.
>
> In any case introduction of immutability opens us many new opportunities,
> which outnumber the small performance penalty it introduces.
>
>
As i said before, an immutability should be provided through behavior
(by disabling/removing methods which can mutate an object) not by
state (hidden mutability flag).
All is good, when you sitting in single process doing everything in
old-fashioned single-headed realm of computation.
But if you try to use immutability in parallel tasks , you'll find
things very-very odd.
Suppose you having two processes A and B, running in parallel.

Process A works with OODB and runs inside a transaction which
temporary sets an immutable bit of interesting object and catching
exceptions to track its changes.
But Process B doesn't know nothing about any transaction and plays
with object in own ways, assuming that it is mutable.
Now, when you try to modify an object state in process B, an exception
will be thrown, but since there is no handler (which is set up only
for Process A) - process will simply fall into debugger.
So, you failed to handle the exception, moreover , you trashed the
working code to error.

Now compare with replacing a class of object:
(consider a pseudo-code)
barrier := WriteBarrier session: myOODBSession.
barrier putOn: someObject. (which replacing an object's class with
itself and stores original class in its inst var).

Now even if you try mutate object in another process - there is no
chance that it will be not handled.


--
Best regards,
Igor Stasenko AKA sig.


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Immutability, what it is, why

Igor Stasenko
Also, things become pretty odd when there is another party playing
with immutability in parallel to OODB.

But! If we, instead, implement a proper support in core classes to get
a ways for user how to ensure immutability through compiler , and
object's behavior, then we are safe from many more pitfalls.



--
Best regards,
Igor Stasenko AKA sig.