Friday, 20 May 2011

Checking for null in generic methods

My OptionType<T> class indicates whether the underlying reference type is null or not whilst retaining all the good type information and forcing the hand of the developer to think about checking for null, at the very least (unfortunately the C# compiler won't let me force the check :-( ). But this didn't work very well for string types because string.Empty is also considered as a null value in many circles, including this one, hence the prevalence of string.IsNullOrEmpty checks all over our codebase.

Now the problem arose when I had an OptionType<string> which was used to represent string.Empty. The simple check I had in place was nothing more than an equality check with null which was clearly unsatisfactory here. What I needed was to check using string.IsNullOrEmpty, but only when T is a string.

The obvious way to approach this would be to abstract out the null checking behaviour, not into a separate parameter because that would be nasty but into a set of functions from which the compiler would make the appropriate selection. Mind you, non-generic calling code worked just fine! Typical!

And so I introduced


public static class Check
{
public static bool IsNull(string s)
{
return string.IsNullOrEmpty(s);
}

public static bool IsNotNull(string s)
{
return !IsNull(s);
}

public static bool IsNull(T t) where T : class
{
return t == null;
}

public static bool IsNotNull(T t) where T : class
{
return !IsNull(t);
}
}


To my dismay, however, the call to Check always selected the generic method, even when the generic type was actually a string. Argh! And so we end up with


public static class Check
{
public static bool IsNull(string s)
{
return string.IsNullOrEmpty(s);
}

public static bool IsNotNull(string s)
{
return !IsNull(s);
}

public static bool IsNull(T t) where T : class
{
return t == null;
}

public static bool IsNotNull(T t) where T : class
{
return !IsNull(t);
}

// We need these two because OptionType cannot call IsNull(string), it can only call IsNull(string)
public static bool IsNullString(string s)
{
return IsNull(s);
}
public static bool IsNotNullString(string s)
{
return IsNotNull(s);
}
}


which is called from the OptionType constructor in the following manner:


if ((typeof(T) == typeof(string) && Check.IsNotNullString(t as string)) || (typeof(T) != typeof(string) && Check.IsNotNull(t)))
{
_t = t;
_isEmpty = false;
}


Not pretty, but it does work. Let me know if you come up with a neater way to achieve this.

Wednesday, 18 May 2011

Null values in dictionaries

Eeek! Why on Earth would someone insert null values into a dictionary? Especially when one sees code like


Dictionary<A,B> d = new Dictionary<A,B>();


blah


TryGetValue( key, out value);
if(value==null) throw new Exception();


That is to say that the return value from TryGetValue is routinely ignored and instead the null-ness of the output value is used to indicate failure. Clearly this has been written because the original author didn't want to wrap dictionary access in try ... catch(), which is understandable, but ...

So I propose that null indicates failure. With this in mind, we can rewrite TryGetValue as follows:


public static Nullable<V> TryGetValue(Dictionary<K,V> d, K key)
{
V v;
bool s = d.TryGetValue(key, out v);
return s ? v : (Nullable<V>)null;
}


This is, of course, absolutely spiffing because now TryGetValue is a function which returns the sought value and so lends itself to a declarative programming style.

Also, with the parameters in the order key then dictionary we allow ourselves the possibility of function pipelining.

Monday, 9 May 2011

Overriding the database access in CSMApi

Sophis' CSMApi class provides the method GetDBAccess(CMString). At first blush this is not going to help because the CMString is passed in and not out of the method. However, the CMString is a tricky class, and it has the method SetString() which allows you to change the underlying string representation even if the object itself has all the appearances of being constant, or at least non-modifiable. Another good reason to avoid CMString.

Anyway, the upshot is that you can override GetDBAccess to provide your own connection details. Interestingly, it turns out that if your CSMApi derivative is in the exe this all works fine but if it is in a dll then, oh dear, it doesn't work. I don't know why at this point, but I assume that there are statics in CSMApi which have some evil order-dependencies...

I'll let you know if I find out what's going on.

Wednesday, 20 April 2011

Curried functions in C# 2.0

This wasn't as difficult as I thought it might have been. Below I reproduce two examples of currying in C#. Easy!

        bool fn<B, C>(B b, C c) { return b.Equals(c); }
        Functional.fn<C, bool> curried_fn<B, C>(B b)
        {
            return delegate(C c) { return fn(b, c); };
        }

        [Test]
        public void CurriedFnTest1()
        {
            bool test1a = fn(1, 2);
            bool test1b = curried_fn<int, int>(1)(2);
            Assert.AreEqual(test1a, test1b);
        }

        Functional.fn<int, int> curried_adder_int(int c)
        {
            return delegate(int p) { return FunctionalHelpers.adder_int(c, p); };
        }

        [Test]
        public void CurriedFnTest2()
        {
            int[] a = { 1, 2, 3, 4, 5 };
            List<int> b = Functional.map<int, int>(delegate(int a1) { return FunctionalHelpers.adder_int(2, a1); }, a);
            List<int> c = Functional.map<int, int>(curried_adder_int(2), a);
            Assert.IsTrue(CollectionComparer.SetEqual(b, c));
        }

Monday, 28 March 2011

C# 2.0 - Functional composition and closures

Sadly, it seems that anonymous functions / delegates cannot hold state so although we can imitate closures if the delegate is defined inline, we cannot do the below:

int diff = 2;
Functional.filter2_fn<int, int> f = delegate(int a, int b) { return Math.Abs(a - b) > diff; };
diff = 3;
bool retval = Functional.forAll2(f, l, m);

because, by the time we come to use
f
, the value stored in
diff
has changed to 3.

C# 2.0 - Functional composition

Yes! It's possible, just! Get your thinking gear around this!

        public static filter_fn<A> not<A>(filter_fn<A> f)
        {
            return delegate(A a) { return !f(a); };
        }

        public static bool forAll<A>(filter_fn<A> f, IEnumerable<A> input)
        {
            return !exists(not(f), input);
        }


and an example:

        public void CompositionTest1()
        {
            int[] i = { 1, 2, 3, 45, 56, 6 };

            bool allOdd = Functional.forAll(Functional.dIsOdd, i);
            bool notAllOdd = Functional.exists(Functional.not(Functional.dIsOdd), i);

            Assert.IsFalse(allOdd);
            Assert.IsTrue(notAllOdd);
        }

Thursday, 24 March 2011

C# is soooo verbose

I have this:
List<ReportItem2> value = new List<ReportItem2>();
IEnumerable<MComponentDetails> components = SwapUtils.GetBasketComponents(swap);
foreach (MComponentDetails detail in components)
{
    string sedol = InstrumentUtils.GetSedol(detail.fSicovam);
    string instrName = InstrumentUtils.GetInstrName(detail.fSicovam);
    value.Add(new ReportItem2(sedol, instrName, columnConfig));
}

and I'd like to have this:
List<ReportItem2> value = new List<ReportItem2>();
IEnumerable<MComponentDetails> components = SwapUtils.GetBasketComponents(swap);
value.AddRange(Functional.map(sec(columnConfig), components));

but there are two problems. One is that the code required in order to build
sec
and enable it to take a parameter (instead of simply being a delegate) is far too much; the second is that, unbelievably, the compiler will not implicitly cast from
IEnumerable<MComponentDetails>
to
IEnumerable<object>
Unbelievable!

By way of illustrating the verbosity of C#, here is my preliminary version of the loop innards:

private class sec
{
    private static ExportColumnConfiguration _config;
    public static Functional.fn f(ExportColumnConfiguration config)
    {
        _config = config;
        return sec_func;
    }

    private static Functional.fn sec_func = CreateReportItem2FromSecurity;
    private static ReportItem2 CreateReportItem2FromSecurity(MComponentDetails detail)
    {
        string sedol = InstrumentUtils.GetSedol(detail.fSicovam);
        string instrName = InstrumentUtils.GetInstrName(detail.fSicovam);
        return new ReportItem2(sedol, instrName, _config);
    }
}


Aha, but I discover that C# 2.0 supports anonymous delegates which are, effectively, lambdas and closures.

Splendid!