Identity Vs Immutable Objects 
Author Message
 Identity Vs Immutable Objects

I understand that all Identity objects (char, integer, true, fasle,
symbol) are immutable.But the reverse is not always true.
Can some one explain with examples how an immutable object may or may
not be an identity object.I came up with the following example.
        Identity Object ---------- Immutable Object

$a, 123,true, false, #abc -------- YES.                         NO
-----------------------'abc', #(1 2 4)

I am not sure if this is correct.

Thanks
- Madhu Rao



Wed, 18 Jun 1902 08:00:00 GMT  
 Identity Vs Immutable Objects

Quote:

> I understand that all Identity objects (char, integer, true, fasle,
> symbol) are immutable.But the reverse is not always true.
> Can some one explain with examples how an immutable object may or may
> not be an identity object.I came up with the following example.
>         Identity Object ---------- Immutable Object

> $a, 123,true, false, #abc -------- YES.                         NO
> -----------------------'abc', #(1 2 4)

By definition, immutable objects can't be mutated.  So in all Smalltalk
implementations I know of one can't modify the number 1.  In many
implementations you can modify the number 100000000000000.  This is
because large integers are stored as byte arrays and many
implementations (VisualAge excepted) are too careless to prevent you
from modifying them.  E.g. evaluate 100000000000 basicAt: 1 put: 1;
yourself in VisualWorks and you'll get  100000000001 as the result.

Identity objects are unique.  For exampole its impossible to create two
distinct instances of a given symbol, e.g. #class.  In fact, its only a
little bit difficult to create two different instances of the symbol
#class, again because most implementations are tooo careless to prevent
wilful misbehavior.  e.g. evaluate the following in VisualWorks:

        | classSym1 classSym2 |
        classSym1 := #class.
        classSym2 := 'class'.
        classSym2 changeClassToThatOf: classSym1.
        classSym1 -> classSym2 -> (classSym1 == classSym2)

Uniqueness is either implemented by the object representation, as it is
for SmallIntegers and Characters, where specific bit patterns represent
specific instances, or by instance creation code which maintains the set
of unique instances (as for Symbol, ByteSymbol et al), or by preventing
you creating extra instances (as for UndefinedObject, True & False).

So immutability and uniqueness are two separate properties.  If you
think of objects as containers then its easy to understand that one can
have two different containers with the same contents, and its easy to
arrange that instance creation code maintains a set of instances such
that the set contains only one instance with a given contents.

In most Smalltalk implementations immutability is only guaranteed for
immediate objects (SmallIntegers & Characters) and objects with no
instance variables (e.g. nil, true and false).  VisualAge actually has
an immutable bit in each object which allows it to enforce
immutability.  Another approach is to use special classes for immutable
objects which are harder or virtually impossible to modify.  Yet another
approach is to place objects in read-only memory.

In all Smalltalk implementations, uniqueness is only guaranteed for
SmallIntegers and Characters, because they're immediate objects.  By
wilful misbehaviour you can break the uniqueness constraints for all
other objects.  But in practice this isn't a problem because people
don't usually wilfully misbehave unless they're having fun [is that a
Lewinski joke? ed.]  traditionally its been much easier to inadvertently
modify a literal, hence VisualAge fixing it.
_______________,,,^..^,,,_______________
Eliot Miranda, ParcPlace



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Literal Vs Identity Object

2. Object identity vs. equality

3. Object identity when returning an object with a function

4. immutable vs. mutable

5. immutable objects/modeling values

6. How best to implement immutable objects?

7. Immutable object & generations

8. C API: Change immutable objects? :-o

9. equality vs. identity

10. Obtaining an object's identity

11. need info on C++ vs Object Pascal vs Objective C vs etc

12. Object identity and characters

 

 
Powered by phpBB® Forum Software