Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

Tuples Part 7: Mutable Tuples

Part 6 left us with comparable tuples.  At this point, the Tuple class is functionally complete.  There will be a little more done with the debugability and overall fit into larger projects.  But otherwise it is sound. 

Now the focus shifts to generating mutable tuples.  Immutability is nice for threading, memoization, etc ...  However it's not always practical to use immutable objects.  Often an algorithm does not benefit from immutability and lends itself to a more mutable type. 

Mutable tuples behave like a Tuple in every other way except that they're ... mutable.  This includes implementing interfaces as well as inheritance structure. 

public sealed class MutableTuple<TA, TB> : 
    ITuple<TA, TB>, 
    IEquatable<MutableTuple<TA, TB>>, 
    IComparable<MutableTuple<TA, TB>>, 
    IComparable
{

As such the script already used will be sufficient to generate mutable classes in addition to the ones its already doing.  The majority of the code difference is just in the naming of the classes.  The only functional differences exist in the properties and indexer.  Both of these add a setter method.  Below is the modified code for generating the property and indexer. 

function script:Gen-TupleAccess
{
    param ( [int] $count = $(throw "Need a count"), [bool]$mutable )
    "public int Count { get { return $count; } }"
    "public object this[int index] { get { switch (index){ "
    0..($count-1) | %{ "case $($_): return m_$($lowerList[$_]);" }
    "default: throw new InvalidOperationException(""Bad Index"");"
    "} }"

    if ( $mutable )
    {
        "set { switch (index) {"
        0..($count-1) | %{ "case $($_): m_$($lowerList[$_]) = (T$($upperList[$_]))value; break;" }
        "default: throw new InvalidOperationException(""Bad Index"");"
        "} } "
    }
    "}"
}

function script:Gen-Property
{
    param ( [int] $index  = $(throw "Need an index"), [bool]$mutable = $false)

    if (-not $mutable )
    {
@"
    private readonly T{0} m_{1};
    public T{0} {0} {{ get {{ return m_{1}; }} }}

"@ -f $upperList[$index],$lowerList[$index]
    }
    else
    {
@"
    private T{0} m_{1};
    public T{0} {0} {{ get {{ return m_{1}; }} set {{ m_{1} = value; }} }}

"@ -f $upperList[$index],$lowerList[$index]
    }
}

Now creating a mutable tuple is the same as the immutable tuple with just a name tweak.

            var t1 = MutableTuple.Create("foo", 42);
            t1.A = "again";

Published Wednesday, January 23, 2008 2:56 AM by Jared Parsons

Filed under: , , ,

Comments

# Travel Jones &raquo; Tuples Part 7: Mutable Tuples @ Wednesday, January 23, 2008 4:18 AM

PingBack from http://www.travel-hilarity.com/travel_jones/?p=8735

Travel Jones » Tuples Part 7: Mutable Tuples

New Comments to this post are disabled
Page view tracker