Cook Computing

XML-RPC.NET and ASP.NET MVC

Scott Hanselman makes some nice comments about XML-RPC.NET in his blog today. His mention of ASP.NET MVC and XmlRpcRoutes caught my attention:

You just need to derive from Charles' XmlRpcService and he'll handle the routing of the HTTP POSTs and the calling of the methods and tearing apart of the parameters. (Actually, as a curiosity back in the ASP.NET MVC 1.0 timeframe both Phil and I write XmlRpcRoutes and supporting samples just to see if it was possible. It is.)

I recently implemented the MetaWeblog API in my home-brew blog engine using an ASP.NET MVC route handler. In the same way as Scott describes I implemented the IMetaWeblog API in a class derived from XmlRpcServer and the required interface:

public class MetaWeblog : XmlRpcService, IMetaWeblog
{
  // ...
  // implementation of IMetaWeblog methods
  // ...
}

I then implemented an ASP.NET MVC route handler:

public class MetaWeblogRouteHandler : IRouteHandler
{
  public IHttpHandler GetHttpHandler(RequestContext requestContext)
  {
    return new MetaWeblog();
  }
}

And finally added a route for the route handler in the routing table:

public class MvcApplication : System.Web.HttpApplication
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add(new Route("{weblog}", null, 
      new RouteValueDictionary(new { weblog = "blogapi" }), 
      new MetaWeblogRouteHandler()));

    // ...
  }
 // ...
}
Posted by Charles Cook at 12:25 PM. Permalink.View Comments.

Structural Regular Expressions in C#

When I read Joe Gregorio's sregex: Structural Regular Expressions in Python last October I thought porting his code to C# would provide an interesting exercise in C# functional style coding. Structural regular expressions were originally described in a paper by Rob Pike (available here). Joe's sregex project page describes like this:

Structural regular expressions work by describing the shape of the whole string, not just the piece you want to match. Each pattern is a list of operators to perform on a string, each time constraining the range of text that matches the pattern. Examples will make this much clearer.

The first operator to consider is the x// operator, which means e(x)tract. When applied to a string, all the substrings that match the regular expression between // are passed on to the next operator in the pattern.

Given the source string "Atom-Powered Robots Run Amok" and the pattern "x/A.../" the result would be ['Atom', 'Amok']. The sregex module does that using the 'sres' function:

>>> list(sres("Atom-Powered Robots Run Amok", "x/A.../"))
['Atom', 'Amok']

A pattern can contain mulitple operators, separated by whitespace, which are applied in order, each to the result of the previous match.

>>> list(sres("Atom-Powered Robots Run Amok", "x/A.../ x/.*m$/"))
['Atom']

There are four operators in total:

  • x/regex/ - Matches all the text that matches the regular expression
  • y/regex/ - Matches all the text that does not match the regular expression
  • g/regex/ - If the regex finds a match in the string then the whole string is passed along.
  • v/regex/ - If the regex does not find a match in the string then the whole string is passed along.

I ported Joe's code to C# keeping the general structure of the code the same and using Linq where possible. The C# version is used like this:

string src = "Atom-Powered Robots Run Amok";
Console.WriteLine(string.Join(", ", sregex.sres(src, "y/ / x/R.*/")));
Console.WriteLine(sregex.sub(src, "y/( |-)/ v/^R/ g/om/", "Coal"));
Console.WriteLine(sregex.sub(src, "x/A.../", s => s.ToUpper()));

/* Outputs to console:
     
Robots, Run
Coal-Powered Robots Run Amok
ATOM-Powered Robots Run AMOK
 
*/

UPDATE 2nd Aug 2010: I've improved the code to handle the 'y' command. I'm using the EnumerableEx.Defer() method from Rx for .NET Framework to ensure that the value of the remaining variable is taken after all the matches in the range has been processed.

public class sregex
{
  static Regex CMD = new Regex("(?<cmd>[xygv])/(?<rgx>[^/]*)/");
  static Regex VALIDCMD
    = new Regex(@"^\s*([xygv]/[^/]*/)(\s+([xygv]/[^/]*/))*\s*$");
  delegate IEnumerable<Range> Func(IEnumerable<Range> f, string pattern);

  static bool IsPattern(string s)
  {
    return (s == "" || VALIDCMD.IsMatch(s));
  }

  static Range _makeRange(Range range, Match match)
  {
    return new Range(match.Index + range.Start,
      match.Index + match.Length + range.Start);
  }

  public static IEnumerable<Range> sre(string src, string pattern)
  {
    IEnumerable<Range> start = new Range[] { new Range(0, src.Length) };

    Func from_g = (f, pat) => f.Where(range =>
      Regex.Match(slice(src, range), pat).Success);

    Func from_v = (f, pat) => f.Where(range =>
      !Regex.Match(slice(src, range), pat).Success);

    Func from_x = (f, pat) => f.SelectMany(range =>
        Regex.Matches(slice(src, range), pat).Cast<Match>()
          .Select(m => _makeRange(range, m)));

    Func from_y = (f, pat) => f.SelectMany(range =>
    {
      var remaining = range;
      return Regex.Matches(slice(src, range), pat).Cast<Match>()
          .Select(m =>
          {
            Range r = new Range(remaining.Start, range.Start + m.Index);
            remaining = new Range(range.Start + m.Index + m.Length, range.End);
            return r;
          })
          .Concat(EnumerableEx.Defer(() => new Range[] { remaining }))
          .Where(r => r.Length > 0); 
    });

    if (!IsPattern(pattern))
      throw new ArgumentException("Invalid expression");

    var wrapper = new Dictionary<string, Func>();
    wrapper["g"] = from_g;
    wrapper["v"] = from_v;
    wrapper["x"] = from_x;
    wrapper["y"] = from_y;

    var ret = CMD.Matches(pattern).Cast<Match>()
      .Aggregate(start, (f, m) =>
        wrapper[m.Groups["cmd"].Value](f, m.Groups["rgx"].Value));
    return ret;
  }

  public static IEnumerable<string> sres(string src, string pattern)
  {
    return sre(src, pattern).Select(r => src.Substring(r.Start,
      r.End - r.Start));
  }

  public static string sub(string src, string pattern,
    Func<string, string> repl)
  {
    StringBuilder sb = new StringBuilder(src);
    foreach (Range range in sre(src, pattern).Reverse())
    {
      sb.Remove(range.Start, range.Length);
      sb.Insert(range.Start,
        repl(src.Substring(range.Start, range.Length)));
    }
    return sb.ToString();
  }

  public static string sub(string src, string pattern, string repl)
  {
    return sub(src, pattern, s => repl);
  }

  static string slice(string str, Range range)
  {
    return str.Substring(range.Start, range.Length);
  }
}

public struct Range
{
  public readonly int Start;
  public readonly int End;
  public int Length { get { return End - Start; } }

  public Range(int start, int end)
  {
    Start = start;
    End = end;
  }
}
Posted by Charles Cook at 06:11 AM. Permalink.View Comments.

Changes to Blog

A few weeks ago I changed the web hosting service for cookcomputing.com from WebHost4Life to DiscountASP.NET (and had a similar experience to Mike Taulty). Rather than continue with Movable Type on the new system, with all the problems of upgrading the data from a very old version of Movable Type, preserving links, etc, I decided to write my own blog engine using ASP.NET MVC. I'm going live with it today.

Posted by Charles Cook at 08:29 PM. Permalink.View Comments.

A Generic Base Class For Implementing IValueConverter

I've written some Silverlight value converters recently and I thought it would be useful to implement a base class to make this task easier, also adding some type checking at runtime. This would enable a boolean to Visibility converter to be implemented like this:

public class BooleanToVisibilityConverter 
  : BaseValueConverter<bool, Visibility>
{
  protected override Visibility Convert(bool value, 
    CultureInfo culture)
  {
    return value ? Visibility.Visible : Visibility.Collapsed;
  }
}

The two generic parameters for BaseValueConverter are the type of the value to be converted and the target type. For case where a parameter is used a different base class is used, also called BaseValueConverter, which takes an extra generic parameter specifying the type of the parameter. For example, you may want the Visibility to be based on the inverse of the boolean value, and so the class can be implemented with an extra string generic parameter to specify that the inverse should be used:

public class BooleanToVisibilityConverter 
  : BaseValueConverter<bool, Visibility, String>
{
  protected override Visibility Convert(bool value, 
    string parameter, CultureInfo culture)
  {
    if (parameter != null && parameter == "!")
          value = !value;
    return value ? Visibility.Visible : Visibility.Collapsed;
  }
}

As usual, the CLR namespace needs to be defined, for example if the class is in a namespace called Converters:

  xmlns:c="clr-namespace:Converters" 

And the converter class specified as a resource:

  <UserControl.Resources>
    <c:BooleanToVisibilityConverter 
      x:Key="BooleanToVisibilityConverter"/>
  </UserControl.Resources>

Finally, the converter resource is used in a binding. In this example TextNotVisible is a boolean value in the control's DataContext and the parameter is used to indicate that whenTextNotVisible is true, the converter should return Visibility.Visible:

    <TextBlock Text="Hello World" Margin="20" Name="Txt"
      Visibility="{Binding TextNotVisible, 
        Converter={StaticResource BooleanToVisibilityConverter}, 
        ConverterParameter=!}"

This is the implementation of BaseValueConverter:

public abstract class BaseValueConverter<V, T, P> 
  : IValueConverter
{
  public object Convert(object value, Type targetType,
    object parameter, CultureInfo culture)
  {
    if (value.GetType() != typeof(V))
      throw new ArgumentException(GetType().Name
        + ".Convert: value type not " + typeof(V).Name);
    if (targetType != typeof(T))
      throw new ArgumentException(GetType().Name
        + ".Convert: target type not " + typeof(T).Name);
    return Convert((V)value, (P)parameter, culture);
  }
 
  public object ConvertBack(object value, Type targetType,
    object parameter, CultureInfo culture)
  {
    if (value.GetType() != typeof(T))
      throw new ArgumentException(GetType().Name
        + ".ConvertBack: value type not " + typeof(T).Name);
    if (targetType != typeof(V))
      throw new ArgumentException(GetType().Name
        + ".ConvertBack: target type not " + typeof(V).Name);
    return ConvertBack((T)value, (P)parameter, culture);
  }
 
  protected virtual T Convert(V value, P parameter,
    CultureInfo culture)
  {
    throw new NotImplementedException(
      GetType().Name + "Convert not implemented");
  }
 
  protected virtual V ConvertBack(T value,
    P parameter, CultureInfo culture)
  {
    throw new NotImplementedException(
      GetType().Name + "ConvertBack not implemented");
  }
}
 
public abstract class BaseValueConverter<V, T>
  : BaseValueConverter<V, T, object>
{
  protected virtual T Convert(V value, CultureInfo culture)
  {
    throw new NotImplementedException(
      GetType().Name + "Convert not implemented");
  }
 
  protected virtual V ConvertBack(T value, CultureInfo culture)
  {
    throw new NotImplementedException(
      GetType().Name + "ConvertBack not implemented");
  }
 
  protected sealed override T Convert(V value, 
    object parameter, CultureInfo culture)
  {
    if (parameter != null)
      throw new ArgumentException(GetType().Name
        + ".Convert: binding contains unexpected parameter");
    return Convert(value, culture);
  }
 
  protected sealed override V ConvertBack(T value, 
    object parameter, CultureInfo culture)
  {
    if (parameter != null)
      throw new ArgumentException(GetType().Name
        + ".ConvertBack: binding contains unexpected parameter");
    return ConvertBack(value, culture);
  }
}

UPDATE 27/07/2010: Fixed errors pointed out by Omar Alani.

Posted by Charles Cook at 01:44 PM. Permalink.View Comments.

Daily Commute

I commute to work in Sheffield these days. I catch the train from Denby Dale each morning. Driving to the station was difficult today because of the snow and ice on the roads but the train was on time (photo taken with iPhone).

Posted by Charles Cook at 10:12 PM. Permalink.View Comments.

Static Reflection - Lambdas as Data

When I read the MSDN magazine article Functional Programming for Everyday .NET Development I was puzzled by this code:

public class AddressMap : DomainMap&lt;Address&gt;
{
  public AddressMap()
  {
    Map(a => a.Address1);
    Map(a => a.Address2);
    Map(a => a.AddressType);
    Map(a => a.City);
    Map(a => a.TimeZone);
    Map(a => a.StateOrProvince);
    Map(a => a.Country);
    Map(a => a.PostalCode);
  }
}

Until I read the explanation:

Fluent NHibernate never evaluates the expression a => a.Address1. Instead, it parses the expression to find the name Address1 to use in the underlying NHibernate mapping. This technique has spread widely through many recent open-source projects in the .NET space. Using Lambda expressions just to get at PropertyInfo objects and property names is frequently called static reflection.

So I wrote some sample code to see how it could be implemented:

using System;
using System.Linq.Expressions;
using System.Reflection;
 
class Address
{
  public string Address1 { get; set; }
  public TimeZone TimeZone { get; set; }
}
 
class AddressMap : DomainMap<Address>
{
  public AddressMap()
  {
    Map(a => a.Address1);
    Map(a => a.TimeZone);
  }
}
 
class DomainMap<T>
{
  public void Map(Expression<Func<T, object>> exp)
  {
    MemberExpression mexp = exp.Body as MemberExpression;
    PropertyInfo pinfo = mexp.Member as PropertyInfo;
    Console.WriteLine("Property name: {0} Type: {1}", 
      pinfo.Name, pinfo.PropertyType);
  }
}
 
class Program
{
  static void Main(string[] args)
  {
    AddressMap map = new AddressMap();
  }
}

This outputs:

Property name: Address1 Type: System.String
Property name: TimeZone Type: System.TimeZone

Using reflection it would be necessary to pass in the name of the property, which is not type safe, for example:

  public void Map(string propertyName)
  {
    PropertyInfo pinfo = typeof(T).GetProperty(propertyName);
    Console.WriteLine("Property name: {0} Type: {1}", 
      pinfo.Name, pinfo.PropertyType);
  }

Thinking back to my post on NOptFunc a while ago, where I discussed the hypothetical methodinfo operator, it seems that a propertyinfo operator would be even more useful:

public class AddressMap : DomainMap&lt;Address&gt;
{
  public AddressMap()
  {
    Map(propertyinfo(Address.Address1));
    Map(propertyinfo(Address.Address2));
    Map(propertyinfo(Address.AddressType));
    Map(propertyinfo(Address.City));
    Map(propertyinfo(Address.TimeZone));
    Map(propertyinfo(Address.StateOrProvince));
    Map(propertyinfo(Address.Country));
    Map(propertyinfo(Address.PostalCode));
  }
}
Posted by Charles Cook at 08:09 AM. Permalink.View Comments.

Why Does Thread Class Not Support IDisposable?

Thinking about threads in the previous post led to consider why the Thread class doesn't implement IDisposable. After all, there will be unmanaged resources associated with each thread, for example at least a thread handle (there is a distinction between logical and physical threads in .NET but I believe that there is usually a one-one relationship, except perhaps the CLR in SQL Server?). I wrote the following code to determine that at least one handle is associated with each instance of Thread and that it is presumably not closed until the Thread instance is finalized (I used class Foo to prove that the finalizer was being run):

class Program
{
  static void Main(string[] args)
  {
    PerformanceCounter counter = new PerformanceCounter(
      "Process", "Handle Count", "TestThreadDisposable");
    Console.WriteLine("initial: {0}", counter.NextValue());
    for (int i = 0; i < 100; i++)
    {
      new Thread(() => { }).Start();
    }
    Console.WriteLine("before GC: {0}", counter.NextValue());
    new Foo();
    GC.Collect(); // force garbage collection
    Thread.Sleep(4000); // allow finalizer thread to run
    Console.WriteLine("after GC: {0}", counter.NextValue());
  }
 
  class Foo
  {
    ~Foo()
    {
      Console.WriteLine("finalizer running");
    }
  }
}

The results show that a number of handles are associated with each instance of Thread but they are not released when the instance is finalized:

initial: 270
before GC: 811
finalizer running
after GC: 811

So either a reference to the Thread instance is being held internally or the handles are being cached in some other way. Either way it shows that implementing IDisposable would not make any difference, at least in the current implementation of Thread. I increased the number of threads being created and the handle count goes down at some point so there is some mechanism for closing them, as you would expect.

Regardless of this, if your program is creating so many threads that handle count becomes a problem then I guess you would have bigger problems anyway.

Posted by Charles Cook at 08:44 AM. Permalink.View Comments.

Waiting For Multiple Threads in C#

I saw a blog post yesterday which presented a C# class for waiting on multiple threads to terminate. The code was unnecessarily complex and inefficient and so I added a comment on a more efficient way of doing it. Shortly afterwards the post disappeared but in case anyone is wondering the way to do it is simply call Thread.Join() on each of the threads in turn, e.g. if you have a collection of threads:

foreach (Thread t in threads)
    t.Join();

This is my version of the post's sample code:

class Program
{
  static void Main(String[] args)
  {
    var threads = new List<Thread>();
    for (int i = 0; i < 10; i++)
    {
      Thread t = new Thread(name =>
        {
          for (int j = 0; j < 5; j++)
          {
            Console.WriteLine("Thread {0}, Count {1}", name, j);
            Thread.Sleep(500);
          }
          Console.WriteLine("Thread {0} Finished", name);
        });
      threads.Add(t);
      t.Start(i);
    }
    Console.WriteLine("Starting to wait...");
    foreach (Thread t in threads)
      t.Join();
    Console.WriteLine("All threads finished!");
    Console.ReadKey();
  }
}
Posted by Charles Cook at 10:15 AM. Permalink.View Comments.

Configuring NLog for Visual Studio 2008

I'm evaluating NLog 1.0 — the latest release — for a project and discovered it doesn't support integration with Visual Studio 2008. I had Visual Studio 2005 installed on another machine and after installing NLog with that version to see which files are installed, I determined the following steps successfully configure NLog for VS 2008:

  1. Install NLog for .NET 2.0.
  2. Download nlogtemplates.zip (contents © Jaroslaw Kowalski) and extract to My Documents\Visual Studio 2008\Templates\ItemTemplates. If you have customized the location of item templates, extract to the appropriate directory.
  3. Download nlogsnippets.zip (contents © Jaroslaw Kowalski) and extract to My Documents\Visual Studio 2008\Code Snippets. If you have customized the location of snippets, extract to the appropriate directory.
  4. Copy the file NLog.xsd from \Program Files\NLog\bin\net-2.0 to Program Files\Microsoft Visual Studio 9.0\Xml\Schemas to enable intellisense when editing NLog configuration.

Once this has been done you will see three NLog configuration templates under My Templates in the Add New Item dialog in VS 2008. They are:

  • A configuration file that defines one File Target (typical).
  • A configuration file that defines one Console Target.
  • An empty configuration file.

One final point: remember to set the Copy To Output Directory property of the NLog config file to Copy Always.

Posted by Charles Cook at 06:26 AM. Permalink.View Comments.

Blog Comments

After reading on inessential.com about Brent Simmons reverting to his own blogging system:

I tried Wordpress for a little while on this weblog since I wanted to have comments. And last night I switched off Wordpress, back to my homegrown static-rendering system.

For comments I'm trying out Disqus. The cool thing is that it works via Javascript includes, so I can still have a static-rendered site.

I decided to try Disqus too. It was very easy to install on my static pages, just a few minutes work modifying the page templates. Looking back through my archives I noticed I switched off comments over 5.5 years ago because of increasing amounts of spam, which was a pity because it was always good to get some feedback on posts.

I am currently writing my own blog publishing app using ASP.NET MVC and LINQ to SQL. I am sticking with static pages because performance can be a little slow with dynamic pages on a shared hosting system, and I don't want to break any of my existing links (I'm still on IIS 6 and so routing is not as flexible as it would be with IIS 7).

Posted by Charles Cook at 07:10 AM. Permalink.View Comments.

Trace XML-RPC Response

If you need to see the XML being returned in an XML-RPC response when you are using XML-RPC.NET, attach an event handler to the proxy's ResponseEvent and trace the content of the stream using the Debug class. If you want to see the request XML, use RequestEvent.

using System.Diagnostics;
using System.IO;
using CookComputing.XmlRpc;

[XmlRpcUrl("http://www.cookcomputing.com/xmlrpcsamples/RPC2.ashx")]
public interface IStateName : IXmlRpcProxy
{
  [XmlRpcMethod("examples.getStateName")]
  string GetStateName(int stateNumber);
}

class Program
{
  static void Main(string[] args)
  {
    IStateName proxy = XmlRpcProxyGen.Create<IStateName>();
    proxy.ResponseEvent += (sender, e) => Debug.WriteLine(
      new StreamReader(e.ResponseStream).ReadToEnd());
    string name = proxy.GetStateName(1);
  }
}

If you are using .NET 2.0 use an anonymous method:

    proxy.ResponseEvent += delegate(object sender,
      XmlRpcResponseEventArgs e)
    {
      Debug.WriteLine(new StreamReader(e.ResponseStream).ReadToEnd());
    };
Posted by Charles Cook at 11:20 AM. Permalink.View Comments.

Linq and Functional Programming

I'm writing some code to display some old blog posts and after retrieving each post as a single string containing CR-LF separated lines I needed to split the string into individual lines and wrap each line with a <p> tag. I could split the string using String.Split but I wondered if it was possible to use a StringReader to generate an enumeration of the lines in the string. It turns out that StringReader doesn't support this but I wrote an extension method to supply the required functionality:

public static partial class Extensions
{
  public static IEnumerable<string> Lines(this TextReader textReader)
  {
    string line;
    while ((line = textReader.ReadLine()) != null)
      yield return line;
  }
}

The extension method is defined for the TextReader parent class of StringReader so it will also work for StreamReader, which makes it possible, for example, to generate an enumeration of the lines in a file (you could use File.ReadAllLines but that generates the array of every line in the file when it is called, whereas using an iterator means that data is read from the file as required for each yield statement).

I also needed to concatenate the strings in the sequence of lines so I wrote another extension method:

public static partial class Extensions
{
  public static string Concatenate(this IEnumerable<string> strings,
    string separator)
  {
    StringBuilder strbldr = new StringBuilder();
    foreach (string str in strings)
    {
      if (strbldr.Length > 0)
        strbldr.Append(separator);
      strbldr.Append(str);
    }
    return strbldr.ToString();
  }
}

This then allows me to write code like this:

string txt = @"one
two
three";
StringReader rdr = new StringReader(txt);
string output = rdr.Lines()
  .Where(line => line != "")
  .Select(line => "<p>" + line + "</p>")
  .Concatenate(Environment.NewLine);

I am finding that with the influence of Linq I am using a more functional style of coding, not just for manipulating data in a database but also for in-memory objects such as arrays. Treating an array as a sequence to which you can apply functions to means you can write higher level code which is easier to understand, and which is less likely to have bugs, because the code is focused on the required functionality rather than how to implement it.

Bill Venners in How Scala Changed My Programming Style describes a similar experience. His example translates to C# as follows: the imperative version:

var nameHasUpperCase = false; 
for (int i = 0; i < name.Length; i++)
{
  if (char.IsUpper(name[i]))
  {
    nameHasUpperCase = true;
    break;
  }
}

And the functional version:

var nameHasUpperCase = name.Any(c => char.IsUpper(c));

Of course, as Raganwald says in Why Why Functional Programming Matters Matters, speaking of how functional code expresses a lot more what and a lot less how, this doesn't come for free:

In general, we think this is a good thing. But it isn't free: somewhere else there is a mass of code that supports your brevity. When that extra mass of code is built into the programming language, or is baked into the standard libraries, it is nearly free and obviously a Very Good Thing. A language that doesn't just separate the concern of how but does the work for you is very close to "something for nothing" in programming.

In the case of the code above I had to write the extension methods but in a more functionally oriented language it might not be necessary to write anything extra.

In general, I wonder if a language designed to be inherently more functional, such as F#, is worth learning; not necessarily as a language for day-to-day use —it may be sometime before it achieves widespread commercial usage, if ever — but because it might feed back into using C# more effectively, moving towards a more functional style of coding where possible.

Posted by Charles Cook at 01:33 AM. Permalink.View Comments.

Why No Top-Level Functions In C#

I've speculated for a long time about why C# doesn't have top-level functions, for example in this post, where the solution to the problem is rather ugly because the static functions have to be qualified by their class name, i.e. instead of this:

Rgx.Expr e = 
  Rgx.Seq(Rgx.Char('c'), 
    Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), 
      Rgx.Char('r')));

It would have been nicer to write this:

Expr e = 
  Seq(Char('c'), 
    Seq(Plus(Alt(Char('a'), Char('d'))), 
      Char('r')));

So I was interested to read Eric Lippert's post Why Doesn't C# Implement "Top Level" Methods? Eric discusses the cost-benefit analysis of implementing this feature, in particular:

In this particular case, the clear user benefit was in the past not large enough to justify the complications to the language which would ensue. By restricting how different language entities nest inside each other we (1) restrict legal programs to be in a common, easily understood style, and (2) make it possible to define "identifier lookup" rules which are comprehensible, specifiable, implementable, testable and documentable.

By restricting method bodies to always be inside a struct or class, we make it easier to reason about the meaning of an unqualified identifier used in an invocation context; such a thing is always an invocable member of the current type (or a base type

).

He describes how C# was originally intended to be a component-oriented language designed for large-scale application development, but that with the increasing popularity of REPL languages like F#, top-level functions are being considered for a future version of C# (with the emphasis on being considered).

Interestingly, a comment on the post notes that Java has the static import construct which allows unqualified access to static members. This allows you to import static class members either individually:

import static java.lang.Math.PI;

Or en masse:

import static java.lang.Math.*;

You can then use the imported members without qualification:

double r = cos(PI * theta);

The motivation for static import was to provide a way of avoiding the constant interface antipattern, described here by Joshua Bloch. This technique involves defining an interface which contains only static final fields. A class using these constants implements the interface and so code within the class doesn't need to qualify the constant names with a class name. Bloch provides this example:

// Constant interface antipattern - do not use!
public interface PhysicalConstants {
  // Avogadro's number (1/mol)
  static final double AVOGADROS_NUMBER   = 6.02214199e23;
  // Boltzmann constant (J/K)
  static final double BOLTZMANN_CONSTANT = 1.3806503e-23;
  // Mass of the electron (kg)
  static final double ELECTRON_MASS      = 9.10938188e-31;
}

Which is used like this:

class hello implements PhysicalConstants {
  public static void main(String[] args) {
    System.out.println("Avogadro's number is " + AVOGADROS_NUMBER);
  }
}

Fortunately or not, depending on your viewpoint, this antipattern cannot be used in C# because interfaces cannot contain fields.

Posted by Charles Cook at 09:17 AM. Permalink.View Comments.

Evening in Upper Dearne Woodlands

One of my favourite evening hikes after a day working at home is to park at Denby Dale, walk up to Upper Denby through Hagg Wood, then round to Square Wood Reservoir and up to the old Quaker settlement of High Flatts, then down to the Upper Dearne Woodlands via New House, and so back to Denby Dale. Beautiful views of the Dearne valley during the first half, then back through the woods which are particularly beautiful in the evening sunlight. About 4 miles.

Posted by Charles Cook at 08:47 AM. Permalink.View Comments.

Functional style regex engine in F#

Nick Palladinos has done a follow-up to my post Functional Style Regex Engine in C# Revisited. In his post Functional style regex engine in F# he presents just that, an F# version of my C# code:

let char c (s : string) = seq { if s.Length > 0 && s.[0] = c then yield s.Substring(1) }

let (=>) l r s = seq { for sl in l s do for sr in r sl -> sr }

let (<|>) l r s = seq { yield! l s; yield! r s }

let rec (<*>) e s = seq { yield s; yield! (e => (<*>) e) s }

let (<+>) e = e => (<*>) e

// example c(a|d)+r
let pattern = char 'c' => (<+>) (char 'a' <|> char 'd') => char 'r'

An interesting difference to my C# version is the use of custom operators, particularly the => and <|> infix operators, which make for a much nicer syntax. Compare the definition of the function pattern above to this:

Rgx.Expr e = Rgx.Seq(Rgx.Char('c'), 
               Rgx.Seq(Rgx.Plus(Rgx.Alt(Rgx.Char('a'), Rgx.Char('d'))), 
                 Rgx.Char('r')));

It's also nice to be able to define functions without having to put them in a class.

Posted by Charles Cook at 12:12 PM. Permalink.View Comments.

Running VMWare Fusion on iMac

I've been running Windows 7 RC as a VMWare Fusion guest machine for a week or so now. Windows 7 requires 1GB of memory and I was experiencing a lot of paging on my iMac, which only has 2GB of memory, when I was trying to run several other applications in Mac OS X at the same time. So I ordered a 2GB SO-DIMM and I just installed it. Things are running much more smoothly now.

Posted by Charles Cook at 07:07 PM. Permalink.View Comments.

NOptFunc

A few days ago Simon Willison posted about his optfunc command line parsing program written in Python:

Command line parsing libraries in Python such as optparse frustrate me because I can never remember how to use them without consulting the manual. optfunc is a new experimental interface to optparse which works by introspecting a function definition (including its arguments and their default values) and using that to construct a command line argument parser.

This is the example he provides:

import optfunc
    
def upper(filename, verbose = False):
    "Usage: %prog <file> [--verbose] - output file content in uppercase"
    s = open(filename).read()
    if verbose:
        print "Processing %s bytes..." % len(s)
    print s.upper()
 
if __name__ == '__main__':
    optfunc.run(upper)

And this is the resulting command-line interface:

$ ./demo.py --help
Usage: demo.py <file> [--verbose] - output file content in uppercase
    
Options:
  -h, --help show this help message and exit
  -v, --verbose 

I've recently been experimenting with C# 4.0 and I realized that the new optional parameter and default parameter value features make possible a similar style of command line parsing. So I wrote some code to do this and the result is the NOptFunc project. Using NOptFunc the code above can be written like this in C#:

using System;
using System.IO;
using CookComputing;

class Program
{
  static void Main(string[] args)
  {
    try
    {
      NOptFunc.Run(typeof(Program).GetMethod("Run"), args);
    }
    catch (Exception ex)
    {
      Console.Error.WriteLine(ex.Message);
    }
  }

  public static void Run(string filename, bool verbose = false)
  {
    string s = File.ReadAllText(filename);
    if (verbose)
      Console.WriteLine("Processing {0} bytes...", s.Length);
    Console.WriteLine(s.ToUpper());
  }
}

In comparison to the Python code the invocation of NOptFunc.Run() is quite ugly and also suffers from potentially failing at runtime if the wrong method name is supplied. It would be nice to be able to write something like this:

      NOptFunc.Run(methodinfo(Program.Run), args);

i.e. assuming that C# had a methodinfo operator along the lines of typeof, returning an instance of MethodInfo instead of Type (overloaded methods would complicate matters, requiring something like methodinfo(Program.Run(int, string)) ). Ian Griffiths discussed this in his post Getting a MethodInfo From a Method Token:

So I get a relatively warm fuzzy feeling about using typeof - I like code that will only be able to run if it can't fail. All other things being equal, I prefer this to code that has potential runtime failure modes.

I've always been mildly perplexed that there's no equivalent way of retrieving a MethodInfo object. E.g. a hypothetical methodinfo(SomeClass.SomeMethod) operator. It's not up the top of my list of language features I want added, it just seems mildly inconsistent to have the operator for getting Type objects but not the corresponding FieldInfo and MethodInfo objects. (Interestingly, there doesn't seem to be a direct way to retrieve an EventInfo in IL, so I can't really object to that one not being in the language.)

Until recently, I had never looked into the details of this. I wasn't previously sure if this missing feature was just something C# chooses not to do, or whether it's because, it can't be done. But I recently had reason to generate some IL that does exacly this, so I can now say with confidence that it's possible, and it's just that C# doesn't supply a corresponding operator.

I've still got a lot of tidying up to do with NOptFunc, for example throwing exceptions with more useful messages and supporting --help, but the basic functionality is working.

Posted by Charles Cook at 04:03 PM. Permalink.View Comments.

List<T> Enumerator Gotcha

I find I am using sequences and iterators much more these days because of the influence of Linq. Even when using manipulating arrays this often results in more robust code because you don't have to worry about boundary conditions with indices; and it is easier to pass around an iterator rather than an array and a reference to the current position within the array, or so I thought until I came across an issue with List<T>.GetEnumerator() which not is immediately obvious and which may apply to other collection classes.

I had refactored some code into a separate function, passing in the instance of List<string>.Enumerator I was using. This code illustrates the problem:

using System;
using System.Collections.Generic;

class Program
{
  static void Main(string[] args)
  {
    var list = new List<string>() { "text" };
    var iterator = list.GetEnumerator();
    iterator.MoveNext();
    Console.Write("{0} ", iterator.Current ?? "null");
    Foo(iterator);
    Console.Write("{0}", iterator.Current ?? "null");
  }

  private static void Foo(List<string>.Enumerator iterator)
  {
    iterator.MoveNext();
    Console.Write("{0} ", iterator.Current ?? "null");
  }
}

I expected that after the return from the function the state of the iterator would reflect the call to MoveNext() made in the function, i.e. in this example the output would be "text null null". But the output is actually "text null text". This seemed inexplicable until I discovered that the Enumerator<T> type returned by GetEnumerator() is a struct which means a copy of the struct is passed to the function. Presumably the position of the iterator is held in a value type, maybe an index into an array, which means that any changes to the position will not be reproduced in the original instance of the struct in the calling function.

The solution is to box the struct by casting it to an interface:

using System;
using System.Collections.Generic;

class Program
{
  static void Main(string[] args)
  {
    // ...
    var iterator = list.GetEnumerator() as IEnumerator<string>;
    // ...	
  }

  private static void Foo(IEnumerator<string> iterator)
  {
    // ...
  }
}

This ensures that the called function has access to the same instance of the struct as the calling function.

Posted by Charles Cook at 03:29 PM. Permalink.View Comments.

Answer to "NUnit isn't running VS10 code"

I just posted my first answer to stackoverflow. Brian Ball was getting the following error when trying to load a dll built using VS 2010 beta into the NUnit GUI:

This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. You may be attempting to load an assembly build with a later version of the CLR than the version under which NUnit is currently running.

My answer was:

I've downloaded the NUnit 2.5 source and opened the VS2008 solution in the VS2010 beta. Once the conversion finished I opened all the projects and changed the target framework setting for all the projects to ".NET Framework 4.0". I then built the solution without any errors. I can now use the NUnit GUI app to run tests built for .NET 4.0. I've not done exhaustive testing of this build so there may be problems, but for my purposes it works fine.

UPDATE

It is not necessary to rebuild NUnit. I discovered that if you add the following to the relevant NUnit application config file you can run a test dll built for .NET 4.0. Under <configuration> add:

<startup>
  <requiredRuntime version="v4.0.20506" />
</startup>

and under <runtime> add:

<loadFromRemoteSources enabled="true" />

Just adding the requireRuntime element is insufficient and results in an security related error message mentioning the loadFromRemoteSources switch. I found something about the switch on this social.msdn post, where David DeWinter wrote:

Caveat: I'm not on the security team but will attempt to answer this nonetheless...

What's happening here is that the build tasks for Silverlight are attempting to load an assembly that, in previous versions of the CLR, would classify it as a partial trust assembly based on its evidence (e.g. its zone) according to CAS policy.

In CLR 4.0, CAS policy is totally deprecated and is not even enabled by default. Under the circumstances, though, it appears the CLR throws an Exception when what would be a partial trust load in CLR 2.0 is a full trust load in CLR 4.0.

The loadFromRemoteSources switch the Exception message refers to is in the runtime element under configuration and looks like this:

<runtime>
   <loadFromRemoteSources enabled="true|false" />
</runtime>

This will not enable legacy CAS policy but will allow you (or, in this case, the build system) to load remote assemblies with the same permissions as the host AppDomain. In this case it seems as though you could modify the configuration for the build system (which I assume in this case would be Visual Studio: %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe.config) to enable this switch.

If you don't want to modify that configuratino then you can set the environment variable COMPLUS_EnableLegacyCASPolicy to 1, which will enable CAS Policy that was present in CLR 2.0 and also allow Silverlight to load this task.

Hope that helps. David

Posted by Charles Cook at 05:48 PM. Permalink.View Comments.

Tip: Convert Value or Reference to Sequence of Length One

In the previous post I originally created the sequences based on a single value by creating an array. For example:

static Expr Nil()
{
  return s => new string[] { s };
}

However this deviates a bit from the spirit of Linq, adds an unneccessary implementation detail, the fact that an array implements IEnumerable<T>, and hard-codes the string type into the expression. The neater way to do it is to use Enumerable.Repeat():

static Expr Nil()
{
  return s => Enumerable.Repeat(s, 1);
}
Posted by Charles Cook at 08:55 AM. Permalink.View Comments.