<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Protected Semantics, Part Five: More on immutability</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx</link><description>I asked a second follow-up question back in Part Two: Suppose you wanted to make this hierarchy an immutable collection, where "Add" and "Remove" returned new collections rather than mutating the existing collection. How would you represent the parenting</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx#8462937</link><pubDate>Tue, 06 May 2008 16:09:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8462937</guid><dc:creator>Dew Drop - May 6, 2008 | Alvin Ashcraft's Morning Dew</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.alvinashcraft.com/2008/05/06/dew-drop-may-6-2008/"&gt;http://www.alvinashcraft.com/2008/05/06/dew-drop-may-6-2008/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Protected Semantics, Part Five: More on immutability</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx#8478356</link><pubDate>Fri, 09 May 2008 11:23:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8478356</guid><dc:creator>crispy</dc:creator><description>&lt;p&gt;Eric,&lt;/p&gt;
&lt;p&gt;Could you post an example of how to actually use such immutable collections?&lt;/p&gt;
&lt;p&gt;As I understood it, you would need a mutable reference, to the current collection. If you want to share it with other classes - allowing them to have &amp;quot;write access&amp;quot; - it would have to be a dedicated container object&lt;/p&gt;
&lt;p&gt;I also seem to have missed a point on thread safety. If used in a multithreaded environment, then every modification of the reference would also have to be locked either. Given an example with a producer, a consumer, and a buffering queue, it could be easy to get unwanted results when there is no locking involved:&lt;/p&gt;
&lt;p&gt;class Example&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;Queue&amp;lt;int&amp;gt; queue = Queue&amp;lt;int&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt;void Producer(object dummy)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;const int count=100;&lt;/p&gt;
&lt;p&gt;Random r = new Random();&lt;/p&gt;
&lt;p&gt;for (int i=0; i&amp;lt;count; i++)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;int val = r.Next(1000);&lt;/p&gt;
&lt;p&gt;queue = queue.Enqueue(val);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;void Consumer(object dummy)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;while (!queue.IsEmpty)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;Queue&amp;lt;int&amp;gt;curr = queue;&lt;/p&gt;
&lt;p&gt;queue = curr.Dequeue();&lt;/p&gt;
&lt;p&gt;Thread.Sleep(curr.Peek());&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;void RunExample()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;ThreadPool.QueueUserWorkItem(new WaitCallBack(Producer));&lt;/p&gt;
&lt;p&gt;Thread.Sleep(0);&lt;/p&gt;
&lt;p&gt;ThreadPool.QueueUserWorkItem(new WaitCallBack(Consumer));&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;If the consumer finishes Queue.Dequeue() and updates the queue reference while producer is running Queue.Enqueue(), the dequeued element will be in the queue again once the producer updates the reference.&lt;/p&gt;
&lt;p&gt;And if the producer finishes Queue.Enqueue() and updates the queue ref while the consumer is running Queue.Dequeue(), the last Enqueued item is lost.&lt;/p&gt;
&lt;p&gt;So, it seems to me that using immutable objects is as thread(un)safe (requires same locking) as using mutables.&lt;/p&gt;
</description></item><item><title>re: Protected Semantics, Part Five: More on immutability</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx#8480450</link><pubDate>Fri, 09 May 2008 17:59:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8480450</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; As I understood it, you would need a mutable reference, to the current collection.&lt;/p&gt;
&lt;p&gt;Your question assumes that your program has such a thing as &amp;quot;the current collection&amp;quot;, and that the value of &amp;quot;the current collection&amp;quot; can change. Why is that a warranted assumption?&lt;/p&gt;
&lt;p&gt;If your program is modelling something with exactly one collection which can change throughout time, then a mutable collection seems like a reasonable data structure to choose. But not all programs do that.&lt;/p&gt;
&lt;p&gt;&amp;gt; Given an example with a producer, a consumer, and a buffering queue, &lt;/p&gt;
&lt;p&gt;... then I would use a threadsafe mutable collection, rather than serializing access to a mutable variable containing an immutable collection. It is probably more efficient to allow the collection to handle its own thread safety. &lt;/p&gt;
&lt;p&gt;Your making a straw man argument here. You're taking examples where mutable data structures are clearly superior and then saying &amp;quot;hey, an immutable data structure would be inferior here&amp;quot;, because you're talking about scenarios in which mutability is a key aspect of the scenario.&lt;/p&gt;
&lt;p&gt;Think outside the box of mutable programming for a bit. Let me give you a real-world example: expression trees.&lt;/p&gt;
&lt;p&gt;Expression trees are immutable trees. In programs that manipulate expression trees, there is no one central &amp;quot;current tree&amp;quot; which is then updated. Rather, you have lots of little bits of trees all over the place. You build up new trees out of old ones, you pull old trees apart, but the trees never change. &lt;/p&gt;
&lt;p&gt;This means that the trees themselves are threadsafe. &amp;nbsp;I can take an expression tree representing (a*1)+b*c, and pass it to a method that logs expression trees on one thread, and pass it to an optimizer to produce the expression tree a+b*c on another thread without worrying that the threads are going to interfere with each other. They cannot, because turning (a*1)+b*c into a+b*c does not modify the original structure at all. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;That's the kind of thread safety I'm talking about. &amp;nbsp;Mutable collections are &amp;quot;threadsafe&amp;quot; only in a very strange way -- it just doesn't seem strange because you're used to it. &amp;nbsp;For example, the following operation is NOT &amp;quot;threadsafe&amp;quot; even for a threadsafe mutable collection:&lt;/p&gt;
&lt;p&gt;Stack s = mystack;&lt;/p&gt;
&lt;p&gt;if (!s.IsEmpty) s.Pop();&lt;/p&gt;
&lt;p&gt;because of course IsEmpty might be false, and then another thread pops the stack, and now the s.Pop() throws a stack-is-empty exception. &amp;nbsp;What kind of craziness is that? &amp;nbsp;Immutable collections do not have this problem; they are truly threadsafe. If you have an immutable collection:&lt;/p&gt;
&lt;p&gt;Stack s = mystack;&lt;/p&gt;
&lt;p&gt;if (!s.IsEmpty) s = s.Pop();&lt;/p&gt;
&lt;p&gt;then you are guaranteed that this never throws. &amp;nbsp;Mutable collection &amp;quot;thread safetly&amp;quot; in fact requires you to have a full, global understanding of exactly what every thread is doing to the collection at all times. That nonlocal analysis is terribly difficult to get right. Much easier with immutable collections, where all the analysis is local. You just have to give up the whole idea of &amp;quot;the canonical mutable collection&amp;quot;, that's all.&lt;/p&gt;
</description></item><item><title>re: Protected Semantics, Part Five: More on immutability</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx#8499179</link><pubDate>Tue, 13 May 2008 09:49:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8499179</guid><dc:creator>crispy</dc:creator><description>&lt;p&gt;Thanks for your reply,&lt;/p&gt;
&lt;p&gt;my post wasn't ment as an attack, but rather stated my understanding of this (which obviously hasn't evolved much from the mutable-dominated world). I thought of the immutable approach that it could be a drop-in replacement which eases life - wich it is, of course, but not for the examples i was able to think of then...&lt;/p&gt;
</description></item><item><title>VCS Team Links for May 22, 2008</title><link>http://blogs.msdn.com/ericlippert/archive/2008/05/05/protected-semantics-part-five-more-on-immutability.aspx#8533403</link><pubDate>Thu, 22 May 2008 22:23:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8533403</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Rather than place the links to the most recent C# team content directly in Community Convergence , I&lt;/p&gt;
</description></item></channel></rss>