Safer Duck

No, it's not made from Latex. It can still quack like a duck and walk like a duck - but you'd be saved from trying to make it qauck like a duck. I call it a Rubberduck language (for obvious reasons).

So how do you make a safer (and faster) duck-typed language? By using regex.

This would be a good example of a rubberduck method:

public IEnumerable<string> ~Get_(<table>[^_]*)_by_(<field>[^(]*)~ GetTableRowByFieldValue(string fieldValue)
{
 if(table == "users")
   return Foo(field, fieldValue);
 else
   return Bar(field, fieldValue);
}

That defines a method that uses a regex to provide more type safety. Thus you can use the method like:

Database.Get_users_by_name("fred");

And in other languages:

Database.GetTableRowByFieldValue("users", "name", "fred");

The great thing is the compiler still has a lot of optimization oppurtunity than a simple 'dynamic resolve' method (i.e. the IDynamicType interface in C# 4.0).

What do I mean by this? The compiler would essentially emit the following types and methods:

// Used for runtime dynamic method creation.
delegate IEnumerable<string> GetTableRowByFieldValueDelegate(string fieldValue);

// Used by other languages.
public IEnumerable<string> GetTableRowByFieldValue(string table, string field, string fieldValue)
{
 if(table == "users")
   return Foo(field, fieldValue);
 else
   reutnr Bar(field, fieldValue);
}

// Used to create the method at runtime.
public GetTableRowByFieldValueDelegate Create_GetTableRowByFieldValue(string table, string field)
{
 DynamicMethod method = CreateMethod(); // etc.
 if(table == "users")  {    method.Push(method.Param("field"));    method.Push(method.Param("fieldValue"));    method.AddCall(this, "$C$0001");    method.AddReturn();  }  else  {    method.Push(method.Param("field"));    method.Push(method.Param("fieldValue"));    method.AddCall(this, "$C$0002");    method.AddReturn();  }  return method.CompileAs<GetTableRowByFieldValueDelegate>(); } // Used by method creation. public IEnumerable<string> $C$0001(string field, string fieldValue) {
// Need to do this for private fields and methods.    Foo(field, fieldValue);
// Could be skipped if the field or method is not private.
} public IEnumerable<string> $C$0002(string field, string fieldValue) {    Bar(field, fieldValue); }

Furthermore, the compiler would be able to create concrete methods if the dynamic method is called from the same assembly.

Wrapping on Specific Characters

Wrap on specfic characters during a S.D.Graphics.DrawString()

Browser != OS

I just read a ZDNet Article by Jason Hiner, and I strongly believe he is missing the point.

Before I delve into the details, I would just like to state my opinion. The browser has become frankenstein - these word processors (if you can call them that) and so forth are using the browser for things that it was never designed for. Sure you have V8 in Chrome now - but JS will never match real OOP languages like C# and Java in terms of maintainability. These are 'cool' things that should have remained 'cool.' Honestly, compare Google Docs to Word - I doubt a serious typesetter would consider using Google Docs. GMail is a great idea; Google Docs is an abomination. You need to know when to stop.

For the same reason, people are often fooled into thinking that the browser is the OS. His article is titled "Have we arrived in the post-Windows era?," however, do you see a single graph of OS popularity? Nope there goes the good old IE versus Firefox graph. That graph used to be relevant in the argument when people were complacent about the default browser on Windows.

His entire argument is based on browsers - and Firefox and Chrome install fine on my copy of Windows. He indicates that Vista and 7 bring nothing new to the table. Linux is still the same beast it was years ago. Sure you have Compiz now, but at the same time Vista has DWM - and it got a big question mark for that one. It is really hard to evolve when all the ideas are out there already; and I think Microsoft is doing a stellar job in that environment.

He says people use Windows XP because they are unwilling to upgrade to Vista. If people are using Windows XP does it not mean that Windows isn't reaching the end of its lifetime?

I wouldn't take that article seriously. He starts off claiming that Windows is dead, and then that all operating systems are dead. Typical web-head perspective - they never realise that you need something to run IIS/Apache/MSSQL/MySQL.

This must be the 20th article I read in the past 4 years claiming that Windows is meeting its demise (yes, even before Microsoft made a mess with Vista). Four years on the the corporate machine is still thriving; why? Because web-apps don't run without an OS dammnit!

Unbox Enum as Integral Value

I needed a way to get the integral value of an System.Enum that is boxed. The solution wasn't immediately obvious after my experimentation mainly because:

  • My enum was derived from byte (and I was trying to cast it to an int).
  • My .Net jitsu wasn't up to scratch.

Lucky thanks to a friendly guy called Guffa I managed to create generic solution.

As it turns out you can unbox an enum value to its base type. As such the following can be unboxed to an uint.


struct FoolishStruct : uint
{
   Gluttony,
   Pride,
   Jealousy
}

object sinner = FoolishStruct.Gluttony;
uint value = (uint)sinner; // This works.

This gave me the necessary knowledge to hash out the following solution:


    /// <summary>
/// Gets the integral value of an enum. /// </summary> /// <param name="value">The enum to get the integral value of.</param> /// <returns></returns> public static T ToIntegral<T>(this object value) { if(object.ReferenceEquals(value, null)) throw new ArgumentNullException("value"); Type rootType = value.GetType(); if (!rootType.IsEnum) throw new ArgumentOutOfRangeException("value", "value must be a boxed enum."); Type t = Enum.GetUnderlyingType(rootType); switch (t.Name.ToUpperInvariant()) { case "SBYTE": return (T)Convert.ChangeType((sbyte) value, typeof(T)); case "BYTE": return (T) Convert.ChangeType((byte) value, typeof(T)); case "INT16": return (T) Convert.ChangeType((Int16) value, typeof(T)); case "UINT16": return (T) Convert.ChangeType((UInt16) value, typeof(T)); case "INT32": return (T) Convert.ChangeType((Int32) value, typeof(T)); case "UINT32": return (T) Convert.ChangeType((UInt32) value, typeof(T)); case "INT64": return (T) Convert.ChangeType((Int64) value, typeof(T)); case "UINT64": return (T) Convert.ChangeType((UInt64) value, typeof(T)); default: throw new NotSupportedException(); } }

HTH.

Professional K2 blackpearl is out

We have finally released a K2 [blackpearl] Book called "Professional K2 blackpearl". I will be getting the book at some point so I will definitely do another post. Previous experience has shown me that these "Professional [product]" books tend to be quite good (having gone through "Professional SQL Server 2005" and "Professional SQL Server Integration Services") - so I am looking forward to it.

Ternary If Statement in C#

Sometimes you just need a ternary statement, i.e. you don't want to return a value from the condition ? true : false construct. Here is an extension method to help you out (the usage example isn't the best).
    class Program
    {
        static void Main(string[] args)
        {
            List<string> test = new List<string>();
            ArrayList source = new ArrayList();

            source.Add(1);
            source.Add("hello");

            source.Each<object>(x => 
                (x is String).IfElse
                    (
                        () => test.Add((string)x),
                        () => test.Add(x.ToString())
                    ));
        }
    }

    static class Extensions
    {
        public static void If(this bool @this, Action isTrue)
        {
            if (@this)
                isTrue();
        }

        public static void IfElse(this bool @this, Action isTrue, Action isFalse)
        {
            if (@this)
                isTrue();
            else
                isFalse();
        }

        internal static void Each<T>(this IEnumerable<T> @this, Action<T> action)
        {
            foreach (T t in @this)
                action(t);
        }

        internal static void Each<T>(this IEnumerable @this, Action<T> action)
        {
            foreach (object t in @this)
                if (t is T)
                {
                    action((T)t);
                }
        }
    }

Ruby-like each statement in C#

Funny this isn't in the provided Enumerable extension methods.

In any case, here it is:

        internal static void Each<T>(this IEnumerable<T> @this, Action<T> action)
        {
            foreach(T t in @this)
                action(t);
        }

        internal static void Each<T>(this IEnumerable @this, Action<T> action)
        {
            foreach (object t in @this)
                if (t is T)
                {
                    action((T)t);
                }
        }

An example:

   objects.Each(x => DoSomethingFunky(x, "some parameter"));

Using Is.Gd/Bitly/TinyUrl/Facebook Post in Chrome

Put your favorite bookmarklet at your fingertips.

Forcibly Closing a Start Tag with XmlTextWriter

How to get a XmlTextWriter to make one of these bad boys: ">"

ROAM landscapes with XNA

My XNA ROAM implementation.
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678