<?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>Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx</link><description>Let’s start with something simple: an immutable stack. Now, immediately I hear the objection: a stack is by its very nature something that changes. A stack is an abstract data type with the interface void Push(T t); T Pop(); bool IsEmpty { get; } You</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>MSDN Blog Postings  &amp;raquo; Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6656382</link><pubDate>Tue, 04 Dec 2007 21:28:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6656382</guid><dc:creator>MSDN Blog Postings  » Immutability in C# Part Two: A Simple Immutable Stack</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://msdnrss.thecoderblogs.com/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack/"&gt;http://msdnrss.thecoderblogs.com/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6657020</link><pubDate>Tue, 04 Dec 2007 22:42:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6657020</guid><dc:creator>TSHAK</dc:creator><description>&lt;p&gt;Correct me if I'm wrong, but with this approach if I wanted to get the traditional behavior of Stack.Pop I would need to perform a Stack.Peek before Stack.Pop as the value (i.e. &amp;quot;head&amp;quot;) is not part of the Stack returned by Stack.Pop. I find this odd, but maybe only due to how I've been programmed to think about a Stack.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6657644</link><pubDate>Tue, 04 Dec 2007 23:47:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6657644</guid><dc:creator>J D</dc:creator><description>&lt;p&gt;TSHAK:&lt;/p&gt;
&lt;p&gt;That's true, but I can't imagine it could reasonably work any other way and still preserve immutability.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6657763</link><pubDate>Wed, 05 Dec 2007 00:01:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6657763</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Any why is it the case that _looking_ at a data structure should _require_ you to change its contents? That has never made any sense to me. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6658175</link><pubDate>Wed, 05 Dec 2007 00:42:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6658175</guid><dc:creator>AdamM</dc:creator><description>&lt;p&gt;That's the same behavior as the STL's stack class. Seems reasonable (and not at all unusual) to me.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6658280</link><pubDate>Wed, 05 Dec 2007 00:57:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6658280</guid><dc:creator>Jay Bazuzi</dc:creator><description>&lt;p&gt;Cool stuff.&lt;/p&gt;
&lt;p&gt;I'd make one change, from:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private static readonly EmptyStack empty = new EmptyStack();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static IStack&amp;lt;T&amp;gt; Empty { get { return empty; } }&lt;/p&gt;
&lt;p&gt;to:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static IStack&amp;lt;T&amp;gt; Empty = new EmptyStack();&lt;/p&gt;
&lt;p&gt;As per:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/jaybaz_ms/archive/2004/04/29/123333.aspx"&gt;http://blogs.msdn.com/jaybaz_ms/archive/2004/04/29/123333.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Keep up the immutable fight! (whatever that means)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6658756</link><pubDate>Wed, 05 Dec 2007 01:36:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6658756</guid><dc:creator>Kaveh Shahbazian</dc:creator><description>&lt;p&gt;I wrote something like a Monad (which I have named it Monad!) in C# 3. I can achieve immutability throe it to an acceptable degree. &amp;nbsp;Still I am meditating on it. But it looks somehow interesting to me. Here is the code:&lt;/p&gt;
&lt;p&gt;public class Monad&amp;amp;lt;T&amp;amp;gt; : IMonad&amp;amp;lt;T&amp;amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private Monad (T inner) { this.__InnerHolder = inner; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private Monad () { }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private readonly T __InnerHolder;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private T Inner { get { return __InnerHolder; } }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static IMonad&amp;amp;lt;TInner&amp;amp;gt; Unit&amp;amp;lt;TInner&amp;amp;gt; (TInner inner) { return new Monad&amp;amp;lt;TInner&amp;amp;gt; (inner); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static Func&amp;amp;lt;A, IMonad&amp;amp;lt;R&amp;amp;gt;&amp;amp;gt; Lift&amp;amp;lt;A, R&amp;amp;gt; (Func&amp;amp;lt;A, R&amp;amp;gt; fun)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (fun.IsNull ()) throw new ArgumentNullException (&amp;quot;fun&amp;quot;, &amp;quot;'fun' can not be null.&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (a =&amp;amp;gt; Unit&amp;amp;lt;R&amp;amp;gt; (fun (a)));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static IMonad&amp;amp;lt;B&amp;amp;gt; Bind&amp;amp;lt;A, B&amp;amp;gt; (IMonad&amp;amp;lt;A&amp;amp;gt; a, Func&amp;amp;lt;A, IMonad&amp;amp;lt;B&amp;amp;gt;&amp;amp;gt; fun)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (fun.IsNull ()) throw new ArgumentNullException (&amp;quot;fun&amp;quot;, &amp;quot;'fun' can not be null.&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new Monad&amp;amp;lt;B&amp;amp;gt; (fun (a.Monad.Inner).Monad.Inner);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;#region IMonad&amp;amp;lt;T&amp;amp;gt; Members&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Monad&amp;amp;lt;T&amp;amp;gt; IMonad&amp;amp;lt;T&amp;amp;gt;.Monad&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return this; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;#endregion&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public static partial class MonadExtra&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static IMonad&amp;amp;lt;TInner&amp;amp;gt; Unit&amp;amp;lt;TInner&amp;amp;gt; (this TInner inner) { return Monad&amp;amp;lt;object&amp;amp;gt;.Unit&amp;amp;lt;TInner&amp;amp;gt; (inner); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static Func&amp;amp;lt;A, IMonad&amp;amp;lt;R&amp;amp;gt;&amp;amp;gt; Lift&amp;amp;lt;A, R&amp;amp;gt; (this Func&amp;amp;lt;A, R&amp;amp;gt; fun)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Monad&amp;amp;lt;object&amp;amp;gt;.Lift&amp;amp;lt;A, R&amp;amp;gt; (fun);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static IMonad&amp;amp;lt;B&amp;amp;gt; Bind&amp;amp;lt;A, B&amp;amp;gt; (this IMonad&amp;amp;lt;A&amp;amp;gt; a, Func&amp;amp;lt;A, IMonad&amp;amp;lt;B&amp;amp;gt;&amp;amp;gt; fun)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Monad&amp;amp;lt;object&amp;amp;gt;.Bind&amp;amp;lt;A, B&amp;amp;gt; (a, fun);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public interface IMonad&amp;amp;lt;T&amp;amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Monad&amp;amp;lt;T&amp;amp;gt; Monad { get; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6659391</link><pubDate>Wed, 05 Dec 2007 02:35:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6659391</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Quite fine. You can think of C# 3.0 query expressions as using a form of monads. I might do a series of blog articles about that at some point.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6662137</link><pubDate>Wed, 05 Dec 2007 07:39:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6662137</guid><dc:creator>grauenwolf</dc:creator><description>&lt;p&gt;Why doesn't Pop use a ref variable to return the value?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6662832</link><pubDate>Wed, 05 Dec 2007 09:29:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6662832</guid><dc:creator>Kaveh Shahbazian</dc:creator><description>&lt;p&gt;Sorry for scrambled code! I have replaced &amp;lt; and &amp;gt; with html literals. It seems that i was wrong about them!&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6663482</link><pubDate>Wed, 05 Dec 2007 10:50:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6663482</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;For the third time: why should it be necessary to _change_ the data structure in order to _look_ at it? A Pop which returns the value is a design flaw; it conflates two logically distinct and separate operations into one method. &lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6664793</link><pubDate>Wed, 05 Dec 2007 13:11:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6664793</guid><dc:creator>silky</dc:creator><description>&lt;p&gt;this is fine enough i suppose, but it seems that it just pushes management to someone else.&lt;/p&gt;
&lt;p&gt;i.e. you, the caller, now have to constantly pass the stack around, instead of having it as a member variable or similar.&lt;/p&gt;
&lt;p&gt;i mean it's immutable, fine. but so what? now life is harder when we use it. it kind of seems like this sort of strategy would be more error prone; pushing more work back onto the caller.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6666564</link><pubDate>Wed, 05 Dec 2007 16:46:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6666564</guid><dc:creator>Anthony Jones</dc:creator><description>&lt;p&gt;Silky, &amp;nbsp;This only part 2. &amp;nbsp;You have to be patient and wait for the Journey to unfold.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6666595</link><pubDate>Wed, 05 Dec 2007 16:48:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6666595</guid><dc:creator>Steve</dc:creator><description>&lt;p&gt;@Jay:&lt;/p&gt;
&lt;p&gt;public static readonly IStack&amp;lt;T&amp;gt; Empty = new EmptyStack();&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6667636</link><pubDate>Wed, 05 Dec 2007 18:46:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6667636</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;I don't understand; why can you not have an immutable stack as a member variable? (In a couple posts I'll be having data structures with immutable stacks as member variables.)&lt;/p&gt;
&lt;p&gt;Integers are also immutable. What are the disadvantages of immutable stacks which are shared by immutable integers? How do immutable integers &amp;quot;push work back onto the caller&amp;quot;?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6668054</link><pubDate>Wed, 05 Dec 2007 19:34:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6668054</guid><dc:creator>Tom Kirby-Green</dc:creator><description>&lt;p&gt;I think silky was objecting to:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;this.s = this.s.Push(...);&lt;/p&gt;
&lt;p&gt;rather than just:&lt;/p&gt;
&lt;p&gt; &amp;nbsp;this.s.Push(...);&lt;/p&gt;
&lt;p&gt;Given the advantages that immutability gives you when thinking about systems in flight I think the tiny bit of extra syntax is a very small code tax.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6668652</link><pubDate>Wed, 05 Dec 2007 20:37:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6668652</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;If that is really your concern then you are clearly not yet in the &amp;quot;immutable&amp;quot; mindset. Why would you change the value of a member variable? Design the system so that the &amp;quot;variable&amp;quot; is immutable, and you never need to worry about changing it!&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6669458</link><pubDate>Wed, 05 Dec 2007 22:11:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6669458</guid><dc:creator>Thomas Danecker</dc:creator><description>&lt;p&gt;I'd love to see such immutable objects in the framework soon. The implementation is that easy and straight-forward and it's so nice to work with immutable things when doing parallel stuff. It's really a very important requirement (in addition to the parallel extenstions) of .net to be successfull in the future where heavy parallelism is as important as oop.&lt;/p&gt;
&lt;p&gt;If someone really can't live with the IMO clean distinction between pop and peek, it's as easy as one simple extension method to have what you want:&lt;/p&gt;
&lt;p&gt;public static IStack&amp;lt;T&amp;gt; Pop(this IStack&amp;lt;T&amp;gt; stack, out T value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;value = stack.Peek();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return stack.Pop();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6671829</link><pubDate>Thu, 06 Dec 2007 02:17:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6671829</guid><dc:creator>silky</dc:creator><description>&lt;p&gt;Tom: Yes, that's what I mean.&lt;/p&gt;
&lt;p&gt;Eric: Look at what tom has done; his usage there shows that he's kind of bypassed the point of your immutable stack. From his apps perspective &amp;quot;this.s&amp;quot; is not immutable. It changes because it's constantly re-assigned to something new.&lt;/p&gt;
&lt;p&gt;The point is that he still needs to use the stack in a non-immutable fashion (i.e. changes need to be reflected in other parts of the code, etc).&lt;/p&gt;
&lt;p&gt;My point was that an immutable stack is fine (I understand the concept) but it still requires you to use it differently (which you could do just as fine with a regular stack). The only difference is that it forces you to. Which is, I suppose, your point, but to me it seems more interesting to perhaps focus on the mysterious surrounding implementation that will make the application using this stack more &amp;quot;Thread Safe&amp;quot; then one that doesn't. It'll still require a smart programmer to do it, IMHO.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6677319</link><pubDate>Thu, 06 Dec 2007 12:36:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6677319</guid><dc:creator>Gabe</dc:creator><description>&lt;p&gt;I think Thomas has a good idea. It's pretty rare that I want to remove an item from a stack without looking at it. Hence, it makes sense to combine those two operations. That combination of operations is traditionally called Pop, thus I expect an operation called Pop to both remove an item from a stack and tell me what it is. Otherwise you've just reimplemented a List, only with methods named Peek/Pop/Push instead of car/cdr/cons.&lt;/p&gt;
&lt;p&gt;All stacks should have a Push and Peek that work as implemented here. However, I've never seen something called a Stack with an operation that discards the top item and doesn't return it. The Pop function that doesn't return the item should be called something like Discard.&lt;/p&gt;
&lt;p&gt;It would be nice if I could do this in C#:&lt;/p&gt;
&lt;p&gt;Stack&amp;lt;T&amp;gt; s; var v;&lt;/p&gt;
&lt;p&gt;s, v = s.Pop();&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6677659</link><pubDate>Thu, 06 Dec 2007 13:08:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6677659</guid><dc:creator>Gonzalo</dc:creator><description>&lt;p&gt;Wouldn't it be better to define Pop as follows?&lt;/p&gt;
&lt;p&gt;public T Pop(out IStack&amp;lt;T&amp;gt; stack)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; stack = tail;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return head;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6678034</link><pubDate>Thu, 06 Dec 2007 13:50:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6678034</guid><dc:creator>Thomas Danecker</dc:creator><description>&lt;p&gt;The benefit of the other implementation (I think the pattern also has a name but I don't remember) is, that you can do the following:&lt;/p&gt;
&lt;p&gt;T one, two, tree;&lt;/p&gt;
&lt;p&gt;stack.Pop(out one).Pop(out two).Pop(out three)...&lt;/p&gt;
&lt;p&gt;This pattern is very nice.&lt;/p&gt;
&lt;p&gt;Eric, it would be nice to have same typical usage pattern of this immutable stack. The community here doesn't see the possibilities, an immutable stack would offer to us.&lt;/p&gt;
&lt;p&gt;Though I also can't imagine of anything really concrete yet.&lt;/p&gt;
&lt;p&gt;The stack would be useful if you're passing it to other methods. But you'd be required to wait for the invoked method till it returns a stack without the element popped by this method. Not really a parallel workflow.&lt;/p&gt;
&lt;p&gt;IMO, for a parallel workflow, you'd rather need an atomic pop method which also returns the popped element.&lt;/p&gt;
&lt;p&gt;With this immutable stack you'll have to do something like this:&lt;/p&gt;
&lt;p&gt;T item = stack.Peek();&lt;/p&gt;
&lt;p&gt;tellOthers(stack.Pop());&lt;/p&gt;
&lt;p&gt;This task can't be run in parallel without any synchronization.&lt;/p&gt;
&lt;p&gt;I like immutable types and I think there's really a way to use them in a reasonable fashion. I also like Eric's implementation in this post, really nice.&lt;/p&gt;
&lt;p&gt;So my question to Eric: How is this stack used? I just can't imagine...&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6678164</link><pubDate>Thu, 06 Dec 2007 14:03:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6678164</guid><dc:creator>Thomas Danecker</dc:creator><description>&lt;p&gt;That's how it could be used in a parallel world:&lt;/p&gt;
&lt;p&gt;Stack&amp;lt;T&amp;gt; temp;&lt;/p&gt;
&lt;p&gt;do&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; temp = stack;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;while(Interlocked.CompareExchange(ref stack, temp.Pop(), temp));&lt;/p&gt;
&lt;p&gt;T value = temp.Peek();&lt;/p&gt;
&lt;p&gt;It's nicer than the usage of a mutable stack, but not nice.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6678898</link><pubDate>Thu, 06 Dec 2007 15:21:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6678898</guid><dc:creator>silky</dc:creator><description>&lt;p&gt;IMHO a Pop without looking at the contents of the stack is actually quite common.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6680224</link><pubDate>Thu, 06 Dec 2007 17:22:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6680224</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I think I've actually used stacks that use the Peek/Pop style more than the Pop-only style. &amp;nbsp;For example, STL's stack template has a top() to grab the top element and a pop() that returns nothing. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6681177</link><pubDate>Thu, 06 Dec 2007 18:53:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6681177</guid><dc:creator>Olmo</dc:creator><description>&lt;p&gt;Hi Thomas, I was just tinking in a solution like yours. Would be possible to make a helper method for this common operation for any kind or inmutable data structures: &lt;/p&gt;
&lt;p&gt;Assign(ref stack, s=&amp;gt;s.Pop());&lt;/p&gt;
&lt;p&gt;Where &lt;/p&gt;
&lt;p&gt;void Assign&amp;lt;T&amp;gt;(ref variable, Func&amp;lt;T,T&amp;gt; fun)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;T val;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;do&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;temp = variable;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;while(Interlocked.CompareExchange(ref variable, fun(temp), temp));&lt;/p&gt;
&lt;p&gt;} &lt;/p&gt;
&lt;p&gt;God bless lambdas :)&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6681289</link><pubDate>Thu, 06 Dec 2007 19:04:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6681289</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Re: don't you always want to see what you're popping?&lt;/p&gt;
&lt;p&gt;No, you don't. For example, consider the stack used by the script engine runtime to keep track of temporary variables. We have separate operations for &amp;quot;peek at the temporary variables&amp;quot; and &amp;quot;these temporaries are dead, remove them from the stack&amp;quot;. &amp;nbsp;We have no need to know what the _values_ of the temps are when they become dead!&lt;/p&gt;
&lt;p&gt;I am not sure why I have to keep on saying this. Peeking and popping are logically distinct operations. If you want to combine them, then you can always write an extension method which does so. &amp;nbsp;But if you package them together and later want to split them apart, that's hard to do. &amp;nbsp;A well-designed interface is factored so that things which are logically distinct are actually distinct.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6681316</link><pubDate>Thu, 06 Dec 2007 19:07:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6681316</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Re: when would you use immutable objects?&lt;/p&gt;
&lt;p&gt;I wrote a program just the other day that has two threads. One constantly recalculates what comes down to an immutable bitmap. The other thread takes whatever bitmap is the latest available and displays it. I do not need to ever worry that the drawing thread's &amp;quot;copy&amp;quot; will ever be updated halfway through rendering, because the structure in question is immutable. Likewise, the calculation thread doesn't ever have to worry that it can't continue to do calculations because it is waiting on the rendering thread to unlock the resource for writing.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6681364</link><pubDate>Thu, 06 Dec 2007 19:09:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6681364</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Or, here's another use. Suppose you have a database consisting of an immutable search tree. You add and delete a bunch of stuff from the tree. If you have an immutable stack of immutable search trees, then you can do undo/redo operations trivially and efficiently on add/delete operations on the search tree. Undo/redo is usually both more efficient and much more straightforward to implement if you have immutable state.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6738772</link><pubDate>Wed, 12 Dec 2007 00:29:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6738772</guid><dc:creator>J. Daniel Smith</dc:creator><description>&lt;p&gt;How about enabling Code Analysis? &amp;nbsp;(throw InvalidOperationException() instead of a raw Exception()).&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6739465</link><pubDate>Wed, 12 Dec 2007 01:45:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6739465</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Showcasing well-designed exception handling is not the point of this series of articles. &lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6752576</link><pubDate>Thu, 13 Dec 2007 02:18:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6752576</guid><dc:creator>David</dc:creator><description>&lt;p&gt;@Eric,&lt;/p&gt;
&lt;p&gt;But in your bitmap example, there still has to be a thread-safe way for the drawing thread to 1) replace its current copy with the latest copy, or 2) retrieve the current copy on every iteration (depending on how it is implemented). This of course is not difficult to do, but once you have done it, what advantage does the immutability of the data structure really offer you? Your threads are never accessing the same object at the same time, so what difference does it make whether it is immutable or not? All you have really made a case for is duplicating objects rather than making them thread-safe; that is not the same thing as making them immutable. I think you have still yet to make a compelling case for why building thread-safety into C# and/or the CLR is an advantage. &lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6825746</link><pubDate>Fri, 21 Dec 2007 13:02:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6825746</guid><dc:creator>Ruxo</dc:creator><description>&lt;p&gt;Look at this Stack abused class:&lt;/p&gt;
&lt;p&gt;class WorkQueue {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;private IStack&amp;lt;WorkItem&amp;gt; workQueue = Stack&amp;lt;WorkItem&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public void EnqueueWorkItem(WorkItem item) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;workQueue = workQueue.Push(item); &amp;nbsp; &amp;nbsp;// No lock needed because it's immutable??&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public WorkItem DequeueWorkItem() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WorkItem topItem = workQueue.Peek();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;workQueue = workQueue.Pop(); &amp;nbsp; &amp;nbsp;// No lock needed because it's immutable??&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Does this mean &amp;quot;thread-safe&amp;quot; ability of immutable objects can't apply here, doesn't it? &amp;nbsp;Because, in practical use, you still have to sync those operations in parallel world.&lt;/p&gt;
&lt;p&gt;So what is the really benefit of immutable object? Isn't it just syntactic sugar?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6829098</link><pubDate>Fri, 21 Dec 2007 20:48:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6829098</guid><dc:creator>Michael Parker</dc:creator><description>&lt;p&gt;Ruxo, the stack in your example is still thread-safe - it is WorkQueue that isn't thread-safe, and you'll notice that it isn't immutable...&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6832731</link><pubDate>Sat, 22 Dec 2007 07:25:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6832731</guid><dc:creator>Ruxo Zheng</dc:creator><description>&lt;p&gt;Thanks Michael. &amp;nbsp;Point taken. &amp;nbsp;But, then, where's the real benefit of Stack's &amp;quot;thread-safe&amp;quot;? &amp;nbsp;The point is, I think, working with a stack, we usually need to access one with the most updated data. &amp;nbsp;So we need synchronization at some place. &amp;nbsp;This immutable stack so seems like new way to express the usage. &amp;nbsp;Would it finally become style preference fight?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6961536</link><pubDate>Thu, 03 Jan 2008 04:22:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6961536</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;I love this series. &lt;/p&gt;
&lt;p&gt;&amp;lt;nit picky complaint&amp;gt;&lt;/p&gt;
&lt;p&gt;The only part I don't like is that Stack&amp;lt;T&amp;gt;.Pop returns an IStack&amp;lt;T&amp;gt;. &amp;nbsp;Instead have Stack&amp;lt;T&amp;gt; be non-sealed and define virtual members. &amp;nbsp;Let EmptyStack overrdie the bad ones that need to throw (Pop,Push,Peek). &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Since you're no longer dealing with a sealed class, the Jitter won't be able to avoid virtual calls for the Pop,Push,Peek methods. &amp;nbsp;However since most methods in the original implementation return interfaces. &amp;nbsp;AFAIK (which doesn't mean it's still true) all mtehod calls on interfaces require virtual dispatch because it could be a Transparent Proxy. &amp;nbsp;This was true ages ago though so I'm not suer if it's still valid. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The upside is that you don't have to worry about Stack&amp;lt;T&amp;gt; vs IStack&amp;lt;T&amp;gt;. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;lt;/nit picky complaint&amp;gt;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6967817</link><pubDate>Thu, 03 Jan 2008 16:47:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6967817</guid><dc:creator>roaan</dc:creator><description>&lt;p&gt;Would one still define this stack as immutable if T is mutable.&lt;/p&gt;
&lt;p&gt;It probably depends what one defines as the &amp;quot;state&amp;quot; of the object.&lt;/p&gt;
&lt;p&gt;If one was to take a snapshot of the object (call it s1), then call stack.Peek.DoSomethingMutable(), then take another snapshot (s2), then s1 != s2.&lt;/p&gt;
&lt;p&gt;(Again, depending on the definition of equals)&lt;/p&gt;
&lt;p&gt;This then begs the question how deep must the &amp;quot;immutablility&amp;quot; be (e.g. member variables, member variables of member variables, member variables of member variables of member variables) in order for an object do be defined as immutable.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6969146</link><pubDate>Thu, 03 Jan 2008 19:04:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6969146</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;That question was the subject of part one of this series. Did you start with part two?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6969636</link><pubDate>Thu, 03 Jan 2008 19:48:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6969636</guid><dc:creator>roaan</dc:creator><description>&lt;p&gt;Yes :$&lt;/p&gt;
&lt;p&gt;Found part one, thanks&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6978781</link><pubDate>Fri, 04 Jan 2008 15:23:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6978781</guid><dc:creator>Jeff Hartmann</dc:creator><description>&lt;p&gt;This stack has no utility to multithreaded programming when more then one thread is getting objects from the stack. &amp;nbsp;Peek and Pop are traditionally combined into a single operation (which is atomic or locked in the multithreaded case) so that an external lock around both the peek and pop operation is not needed. &amp;nbsp;If one thread is putting things on the stack and two others are getting work from it in a LIFO fashion then they can both get the same piece of information to work on.&lt;/p&gt;
&lt;p&gt;Of course this is most likely not the problem this stack was designed to solve.&lt;/p&gt;
&lt;p&gt;Immutable data structures can be nice, but well designed syncronized/atomic operations still need to be used to communicate between the threads.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#6980862</link><pubDate>Fri, 04 Jan 2008 19:35:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6980862</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;&amp;gt; Of course this is most likely not the problem this stack was designed to solve.&lt;/p&gt;
&lt;p&gt;Correct. There are (at least) two kinds of thread safety:&lt;/p&gt;
&lt;p&gt;1) Objects are &amp;quot;thread safe&amp;quot; because all changes made by all threads are visible to all threads.&lt;/p&gt;
&lt;p&gt;2) Objects are &amp;quot;thread safe&amp;quot; because changes made by one thread are invisible to other threads.&lt;/p&gt;
&lt;p&gt;Immutable stacks implement the latter kind of safety. A safe-in-the-latter-sense stack has the property that it has predictable behaviour. &amp;nbsp;You push something on it, it gets one bigger. &amp;nbsp;A safe-in-the-first-sense stack does not have this property -- you push something on it, and you have absolutely no idea what the size of that thing is because n other threads might have been pushing and popping it at the same time. &lt;/p&gt;
&lt;p&gt;And, combining this with the comments above -- in a stack designed to have the first kind of thread safety, it makes more sense to combine &amp;quot;peek&amp;quot; with &amp;quot;pop&amp;quot;. After all, you have no idea after &amp;quot;peeking&amp;quot; the stack that the item you just peeked is still on the top of the stack, or even on the stack at all. Eliminating peek means that you can at least guarantee that popping the stack gives you an element that was once at the top of the stack and is no longer.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7024789</link><pubDate>Tue, 08 Jan 2008 09:52:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7024789</guid><dc:creator>Koos Dering</dc:creator><description>&lt;p&gt;One would think Eric wants an object-type between 'struct's and 'class'es: (at compilers' discretion) having reference-implementation but always value-semantics. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7036123</link><pubDate>Wed, 09 Jan 2008 07:17:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7036123</guid><dc:creator>Francois Tanguay</dc:creator><description>&lt;p&gt;Two things:&lt;/p&gt;
&lt;p&gt;Regarding Pop returning the Head:&lt;/p&gt;
&lt;p&gt;public static class StackExtensions&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;public static IStack&amp;lt;T&amp;gt;(this IStack&amp;lt;T&amp;gt; stack, out T value)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;value = stack.Peek();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return stack.Pop();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And as to users who want &amp;quot;mutable usage&amp;quot;, you could do a mutable wrapper&lt;/p&gt;
&lt;p&gt;public class MutableStack&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;private IStack&amp;lt;T&amp;gt; innerStack;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public MutableStack(IStack&amp;lt;T&amp;gt; stack) { innerStack = stack; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public void Push(T value) { innerStack = innerStack.Push(value); }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public T Pop() { T value; innerStack = innerStack.Pop(out value); return value; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public bool IsEmpty { return innerStack.IsEmpty; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public T Peek() { return innerStack.Peek(); }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7118512</link><pubDate>Tue, 15 Jan 2008 18:18:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7118512</guid><dc:creator>Jim W.</dc:creator><description>&lt;p&gt;Thanks for the great ideas... &amp;nbsp;I'm actually thinking an immutable stack might be great as the Undo/Redo stack in a puzzle game I'm building. &amp;nbsp; It might even come in handy in the puzzle generation phase... &amp;nbsp;providing a nice way to manage and analyze alternative &amp;quot;paths&amp;quot;.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7122770</link><pubDate>Wed, 16 Jan 2008 03:18:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7122770</guid><dc:creator>Richard K.</dc:creator><description>&lt;p&gt;Eric,&lt;/p&gt;
&lt;p&gt;Thread safety is more than just 'immutable'. &amp;nbsp;In systems where performance is an issue, where thread safety is an issue, and where stack size can be significant, an immutable stack provides one type of thread safety at the cost of performance (in much the same way immutable strings can impact performance if you don't take the underlying mechanisms of strings into account).&lt;/p&gt;
&lt;p&gt;I do not deny that immutability is a way to solve these problems, but we can't oversimplify the problem of parallel programming. &amp;nbsp;The trade-offs between the use of immutable objects (strings, stacks, whatever) vs. more traditional techniques must be COMPLETELY understood by the engineers solving the problems, there is no simple 'off the shelf solution'. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you are not implying this, my apologies, I just seem to be getting this impression from many in this thread, and its dangerous thinking (there are no silver bullets).&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7122923</link><pubDate>Wed, 16 Jan 2008 03:28:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7122923</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Of course there are no silver bullets in multithreaded programming. That is a corrollary of the fact that there are no silver bullets in ANY kind of programming. A point that I return to over and over again in this blog is that good design and engineering is a process of making and identifying tradeoffs.&lt;/p&gt;
&lt;p&gt;My point in this series is merely to point out that designing, implementing and using immutable data structures gives you yet another tool in your toolbox which you can consider when faced with a tough problem, particularly a problem involving multithreading. Whether it is the right tool or not depends upon the task at hand.&lt;/p&gt;
</description></item><item><title>Immutability in C#</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7136549</link><pubDate>Thu, 17 Jan 2008 02:45:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7136549</guid><dc:creator>Community Blogs</dc:creator><description>&lt;p&gt;For some reason, there's been a lot of buzz lately around immutability in C#. If you're interested in&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7143930</link><pubDate>Fri, 18 Jan 2008 00:41:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7143930</guid><dc:creator>Richard K</dc:creator><description>&lt;p&gt;Eric,&lt;/p&gt;
&lt;p&gt;Excellent.. then you and I are in agreement, since there are most definitely uses for immutability in the toolbox... as long as they are intelligently applied.&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7209616</link><pubDate>Wed, 23 Jan 2008 18:35:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7209616</guid><dc:creator>Siderite</dc:creator><description>&lt;p&gt;I have to agree with Kaveh Shahbazian on this one: fun cannot be null. :)&lt;/p&gt;
&lt;p&gt;My question is why change a way of programming in order to teach people immutability when you can change the compiler to take care of things like that in care of concurency and multi-processor computing? I think the whole FP and immutability buzz is generated by people thinking to abstractly to be able to debug anything :)&lt;/p&gt;
</description></item><item><title>PipeSingleReaderNoLock</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7996047</link><pubDate>Mon, 03 Mar 2008 08:46:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7996047</guid><dc:creator>jaredpar's WebLog</dc:creator><description>&lt;p&gt;Previously we discussed a multi-thread safe queue like data structure using locks as an internal synchronization&lt;/p&gt;
</description></item><item><title>PipeSingleReaderNoLock</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#7996137</link><pubDate>Mon, 03 Mar 2008 08:55:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7996137</guid><dc:creator>Noticias externas</dc:creator><description>&lt;p&gt;Previously we discussed a multi-thread safe queue like data structure using locks as an internal synchronization&lt;/p&gt;
</description></item><item><title>Immutability</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#8291174</link><pubDate>Mon, 17 Mar 2008 19:17:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8291174</guid><dc:creator>Steve's Tech Talk</dc:creator><description>&lt;p&gt;Functional programming is in. There is a lot of buzz around the future of functional programming as applications&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#8764078</link><pubDate>Tue, 22 Jul 2008 17:11:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8764078</guid><dc:creator>Gustav</dc:creator><description>&lt;p&gt;This is genius...&lt;/p&gt;
&lt;p&gt;I'm using this algorithm to store the numbers 1-40 in a immutable stack:&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s0 = Stack&amp;lt;int&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s1 = s0.Push(1);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s2 = s1.Push(2);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s3 = s2.Push(3);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s4 = s3.Push(4);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s5 = s4.Push(5);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s6 = s5.Push(6);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s7 = s6.Push(7);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s8 = s7.Push(8);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s9 = s8.Push(9);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s10 = s9.Push(10);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s11 = s10.Push(11);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s12 = s11.Push(12);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s13 = s12.Push(13);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s14 = s13.Push(14);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s15 = s14.Push(15);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s16 = s15.Push(16);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s17 = s16.Push(17);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s18 = s17.Push(18);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s19 = s18.Push(19);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s20 = s19.Push(20);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s21 = s20.Push(21);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s22 = s21.Push(22);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s23 = s22.Push(23);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s24 = s23.Push(24);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s25 = s24.Push(25);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s26 = s25.Push(26);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s27 = s26.Push(27);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s28 = s27.Push(28);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s29 = s28.Push(29);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s30 = s29.Push(30);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s31 = s30.Push(31);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s32 = s31.Push(32);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s33 = s32.Push(33);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s34 = s33.Push(34);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s35 = s34.Push(35);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s36 = s35.Push(36);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s37 = s36.Push(37);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s38 = s37.Push(38);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s39 = s38.Push(39);&lt;/p&gt;
&lt;p&gt;			IStack&amp;lt;int&amp;gt; s40 = s39.Push(40);&lt;/p&gt;
&lt;p&gt;programming was never easier before, but the best is, it's threadsafe ;) no one will ever change the stuff i stored in the stack.&lt;/p&gt;
&lt;p&gt;thank you Eric, made my day...&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9060344</link><pubDate>Tue, 11 Nov 2008 18:26:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9060344</guid><dc:creator>Daniel Earwicker</dc:creator><description>&lt;p&gt;I'm coming to this really late, and I'm probably failing to see the joke, so this is a waste of time, but I can't resist informing Gustav that he could have written:&lt;/p&gt;
&lt;p&gt;Enumerable.Range(1, 40).Aggregate(Stack&amp;lt;int&amp;gt;.Empty, (s, n) =&amp;gt; s.Push(n));&lt;/p&gt;
</description></item><item><title>ImplicitStyleManager: Under the Hood</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9138852</link><pubDate>Mon, 24 Nov 2008 23:46:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9138852</guid><dc:creator>Microsoft Weblogs</dc:creator><description>&lt;p&gt;In this post I'll delve into the functional programming techniques that were used to develop ImplicitStyleManager&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9146068</link><pubDate>Thu, 27 Nov 2008 12:20:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9146068</guid><dc:creator>Saw</dc:creator><description>&lt;p&gt;IStack&amp;lt;int&amp;gt; s1 = Stack&amp;lt;int&amp;gt;.Empty;&lt;/p&gt;
&lt;p&gt;IStack&amp;lt;int&amp;gt; s2 = s1.Push(10);&lt;/p&gt;
&lt;p&gt;s2 = s2.Push(15); &amp;nbsp;//??&lt;/p&gt;
&lt;p&gt;would it possible or should the above statement exists in immutable world?&lt;/p&gt;
&lt;p&gt;I tried to run the statement, and it appear that the s2 now has head with value 15, and it's tail have a IStack which the head value is 10. Kinda strange to me as I still new to this world.&lt;/p&gt;
&lt;p&gt;It is consider that the s2 been mutated?&lt;/p&gt;
</description></item><item><title>re: Immutability in C# Part Two: A Simple Immutable Stack</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9149061</link><pubDate>Fri, 28 Nov 2008 04:32:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9149061</guid><dc:creator>Saw</dc:creator><description>&lt;p&gt;Here is a link talk about the rules for immutable type:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.bluebytesoftware.com/blog/2007/11/11/ImmutableTypesForC.aspx"&gt;http://www.bluebytesoftware.com/blog/2007/11/11/ImmutableTypesForC.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Rules number five:&lt;/p&gt;
&lt;p&gt;5. Immutable types must not leak the ‘this’ reference during construction.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IStack&amp;lt;T&amp;gt; Push(T value) { return new Stack&amp;lt;T&amp;gt;(value, this); }&lt;/p&gt;
&lt;p&gt;Wonder if the above statement break the rules?&lt;/p&gt;
</description></item><item><title>InvocationExpression and LINQ to Entities</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9181965</link><pubDate>Sun, 07 Dec 2008 17:19:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9181965</guid><dc:creator>meek</dc:creator><description>&lt;p&gt;I talked a little bit about patterns using InvocationExpression in a previous post (you might want to&lt;/p&gt;
</description></item><item><title>InvocationExpression and LINQ to Entities</title><link>http://blogs.msdn.com/ericlippert/archive/2007/12/04/immutability-in-c-part-two-a-simple-immutable-stack.aspx#9723451</link><pubDate>Wed, 10 Jun 2009 09:20:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9723451</guid><dc:creator>VS2010学习</dc:creator><description>&lt;p&gt;I talked a little bit about patterns using InvocationExpression in a previous post (you might want to&lt;/p&gt;
</description></item></channel></rss>