Tuesday, February 17, 2009

Explaining Typesheet

I've been designing a language for typesheet, a technique to speeding up Groovy by enforcing type for specific calls.

Current I can enforce type of a call by writing:

method(my.package.MyClass#main(String[])) {
  call(println(*)) && args(s) {
    s ~> String;

This typesheet reads: With in the main method of class MyClass, match every call to meta-method "println" (regardless input args). For matched calls,
  1. let MOP select real methods by assuming the type of input argument to be "String", and
  2. convert values of those arguments to be "String" before passing them to the calls.
You may notice that I emphasis 2 places. There are 2 different phases of the above code to be working. Firstly, in dynamically typed languages, like Groovy, method binding is performed at runtime. So matched call messages are not the real methods. Calls to "println" there may be anything depending on the MOP engine to select.

Secondly, declaration in { s ~> String; } (I call it a type intervention block) enforces MOP to select the method according to the type of "s", which is bound to the only argument of the method. ("s" has been binding using an args predicate).

So, what's happening next? You might be guessing correctly :)
MOP will be fully by-passed for those matched calls (because related types are enforced). Therefore, speed will be increased to the level of normal Java calls.

No comments: