Icon+Unicon/Intro: Difference between revisions

Content added Content deleted
Line 38:
=== Mutable and Immutable Types ===
 
Icon/Unicon has both mutable (changeable) and immutable (unchangeable) data types. All operations deal with these consistently and it isn't possible to directly discern which are which (say by returning the address of a value).
 
There are operations which can create separate copies of types and distinguish between different copies of mutable types. These operations can be applied to immutable types in an intuitive manner and they perform consistently within those contexts as is shown in the following code snippet:
 
<lang icon># copy .v. :=
 
mutable := [] # lists are mutable
immutable := "abc" # strings are not
m2 := mutable # assignment copies the reference
m3 := copy(mutable) # creates a (1 level) copy
i2 := immutable # assignment copies the reference
i3 := copy(immutable) # same as assignment
 
# value equal ( === )
 
mutable === m2 # succeeds
mutable === m3 # fails
immutable === i2 # succeeds
immutable === i3 # also succeeds</lang>
 
 
Furthermore even though strings are immutable, it is possible to construct the same string in different memory locations. You just can't tell if they are different or not.
 
== Operators and Procedures ==