[Haskell-cafe] Hugs, dotnet, C#...

Hugh Perkins hughperkins at gmail.com
Wed Oct 10 15:50:58 EDT 2007


On 10/3/07, Peter Verswyvelen <bf3 at telenet.be> wrote:
> I needed to type at least 3 times the amount of code, much
> of which was boilerplate code, and the code is not elegant.

Reflection is your friend here.

Example.  Code before reflection:

MyConfig
{
   // some properties here

   public MyConfig()
    {
       XmlDocument dom = XmlHelper.Loadxml("config.xml");
       somevalue1 = getIntValue( dom, "value1");
       somevalue2 = getStringValue( dom, "value2");
       // blah, blah, blah...
   }
}

With reflection:

MyConfig
{
   // some properties here

   public MyConfig()
    {
       ReflectionHelper.LoadMe( this, "config.xml" );
   }
}

Just typed this in off the top of my head, so it might have errors in,
but you get the idea.

Of course, in this case you could just use Microsoft XmlSerializer,
but I quite like the flexibility of using my own serializer (it's not
that hard to write, just a hundred lines or so).

... but then, you can do something similar to display arbitrary
objects to html... or write to sql... or write to a proprietary
protocol... or ... many things!

I kindof know what you mean though.  Declaring variable types is
kindof a pain, and of course anything to do with lists is insanely
easier to write in Haskell (/Erlang).


More information about the Haskell-Cafe mailing list