A C# library to write functional code - Part III - Records

-

Other posts in the se­ries:

    This abstract class has a field of type STuple:
    
    <pre class="code"><span style="color:rgb(0,0,255);">protected</span> <span style="color:rgb(0,0,255);">readonly</span> <span style="color:rgb(43,145,175);">STuple</span>&lt;T1, T2&gt; state;
    What is a STuple? Well it is exactly the same as the Tuple described in Part II, but coded as a struct instead of a class. The reason to use a struct is to not allocate an additional object on the stack. This allows this solution to be as &#8216;performant' as simply having coded the fields on the class itself. Or at least I think so
    
    The Record class also has a constructor that simply initialize the STuple:
    
    <pre class="code"><span style="color:rgb(0,0,255);">public</span> Record(T1 t1, T2 t2) { state = <span style="color:rgb(43,145,175);">F</span>.STuple(t1, t2); }</pre>
    
    <pre class="code"><font face="Verdana">where</font></pre>
    
    <pre class="code"><span style="color:rgb(0,0,255);">internal</span> <span style="color:rgb(0,0,255);">static</span> <span style="color:rgb(43,145,175);">STuple</span>&lt;T1, T2&gt; STuple&lt;T1, T2&gt;(T1 t1, T2 t2) {
        <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(0,0,255);">new</span> <span style="color:rgb(43,145,175);">STuple</span>&lt;T1, T2&gt;(t1, t2);
    }</pre>
    
    The Equals method is very much the same as the Tuple's one, just delegating to the same DSEqual function that checks equality for Tuples.
    
    <pre class="code"><span style="color:rgb(0,0,255);">public</span> <span style="color:rgb(0,0,255);">override</span> <span style="color:rgb(0,0,255);">bool</span> Equals(<span style="color:rgb(0,0,255);">object</span> right) {
        <span style="color:rgb(43,145,175);">Utils</span>.CheckNull(right);
        <span style="color:rgb(0,0,255);">if</span> (<span style="color:rgb(0,0,255);">object</span>.ReferenceEquals(<span style="color:rgb(0,0,255);">this</span>, right))
            <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(0,0,255);">true</span>;
        <span style="color:rgb(0,0,255);">if</span> (<span style="color:rgb(0,0,255);">this</span>.GetType() != right.GetType())
            <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(0,0,255);">false</span>;
        <span style="color:rgb(0,0,255);">var</span> rightT = right <span style="color:rgb(0,0,255);">as</span> <span style="color:rgb(43,145,175);">Record</span>&lt;T1, T2&gt;;
        <span style="color:rgb(0,0,255);">return</span> <span style="color:rgb(43,145,175);">F</span>.DSEquals(<span style="color:rgb(0,0,255);">this</span>.state, rightT.state);
    }</pre>
    
    That's it. Not too difficult as most of the implementation is based on the Tuple's code. Next post will hopefully be more interesting. It is about Type Unions.

Tags

13 Comments

Comments

gOODiDEA.NET

2008-04-21T21:04:17Z

.NET RestLess - A Simple REST Framework ASP.NET AJAX Overview And Technical Tips A C# li­brary to write

.NETRestLess-ASimpleRESTFrameworkASP.NETAJAXOverviewAndTechnicalTipsAC#libraryto…

Charlie Calvert's Community Bl

2008-04-23T16:58:14Z

Welcome to the forty-third is­sue of Community Convergence. The last few weeks have been con­sumed by the

Eamon Nerbonne

2008-04-24T05:11:28Z

By the way, there’s an­other rea­son your hash-code al­go­rithm is un­wise: Assuming the com­po­nent hash-codes are uni­formly dis­trib­uted over the in­te­gers from 0 to 2^32-1 (thus in­ter­pret­ing the signed in­te­gers as un­signed in­te­gers here, for sim­plic­ity), your will not be:  a bi­nary or never low­ers the num­ber of on” bits; each bit of your re­sult hash code has only a 25% chance to be off - which low­ers the en­tropy of the over­all hash code to be around 26 bits, as op­posed to 32 bits.
Finally, tu­ples of tu­ples will hash par­tic­u­larly badly; and so will tu­ples with higher than 2-arity.

Eamon: you are right. It is bad. As I said be­fore, I was lazy.

Wow, a lit­tle more and we’ll fi­nally have the easy-to-use record con­struct of Pascal in the 1980s.  Just de­clare a Record type and you’re set.  I have (seriously) of­ten wished for this in C# in­stead of cum­ber­some, sep­a­rate struct or class de­f’s.

This’s a re­ally ex­cel­lent in­tro­duc­tion. I think I be­gin to know what the func­tional code is af­ter read­ing your posts and code. But I have some ques­tions about this:
For the GetHashCode(), why don’t you try xor” other than or”? I think xor” will get a bet­ter job than the or”~
For some­thing about all the code, and I found TypeUnion<T> is re­ally use­less! Since I’ll never have the change to use a mem­ber union!

Previous posts: Part I - Background Part II - Tuples Now that we know what Tuples are, we can start talk­ing about Record, as they use a de­riv­a­tive of Tuples un­der the cover. But first, what is a record? Well, in C# par­lance a Record is a sort of im­mutabl

adamjcooper.com/blog

2008-06-03T15:38:39Z

The Quest for Quick-and-Easy Class-Based Immutable Value Objects in C# - Part 1: Introduction

adamjcooper.com/blog

2008-06-03T17:00:02Z

The Quest for Quick-and-Easy Immutable Value Objects in C#

Luca Bolognese's WebLog

2008-07-15T05:46:24Z

Other posts in the se­ries: Part I - Background Part II - Tuples Part III - Records Part IV - Type Unions