Angel \”Java\” Lopez on Blog

February 15, 2010

Default methods in ajSharp

Filed under: AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:23 am

Past days, I added default methods in AjSharp, my open source interpreter. What is a default method in AjSharp jargon? I was inspired by the #doesnotunderstand message in Smalltalk Object: AjSharp is a dynamic interpreter, so you can invoke any method in advance. If the method does not exists, and a default method is defined, then this default method is invoked, receiving the name of the original method and its arguments.

First example:

object Incrementor
{
  function Increment(n) 
  {
    return n+1;
  }
}
class Proxy
{
  var Object;
  
  function Proxy(obj)
  {
    this.Object = obj;
  }
  
  default function InvokeMethod(name, parameters)
  {
    return this.Object.Invoke(name,parameters);
  }
}
proxy = new Proxy(Incrementor);
result = proxy.Increment(3); // result == 4

The keyword default marks the default method (I still reluctant to use a keyword, but no other idea, yet). Note that this proxy implementation works only on DynamicObject instances, that have .Invoke method. It should be extended with a bit more logic if you want proxy native objects.

By now, other simple example:

class Incrementor
{
  function Increment(n) 
  {
    return n+1;
  }
}
class LoadBalancer
{
  var objects;
  var random;
  
  function LoadBalancer()
  {
    this.random = new System.Random();
    this.objects = new List();
  }
  
  sub Add(obj)
  {
    this.objects.Add(obj);
  }
  
  default function InvokeMethod(name, parameters)
  {
    n = random.Next(objects.Count);
    
    return this.objects[n].Invoke(name,parameters);
  }
}
  
balancer = new LoadBalancer();
balancer.Add("Foo");
for (k=1; k<=10; k++)
  balancer.Add(new Incrementor());
  
result = 0;
for (k=0; k<4; k++)
  result = result + balancer.Increment(k);
  
// result == 1 + 2 + 3 + 4 == 10

It’s a random-based “load balancer”, it could be named “distributor”. The LoadBalancer instance sends each method call to one of its objects, at random.

I could write another example, where the balancer sends each invocation to ALL of its objects. I plan to use these features over agents, and distributed agents. I should add default method to objects (remember, AjSharp can define objects without an associated class).

As usual, the code is available at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: