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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Monday, April 27, 2009 9:38 PM
Print
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910