Functors and Autos

Joel had an interesting post today. The programming language he is talking in that post is obviously JavaScript.

JavaScript is an amazingly powerful language. Most people just don’t realize that and since it allied with the no-brainer (at least that is what geeks in ES might feel) HTML, it hasn’t got the limelight in programmers mind as say a Java or Python.

 Even I never had a pithy idea of the language capabilities till lately. Last year as a part of our project we had come up with some HTML prototypes and then I got a chance to deep-dive into JavaScript and frankly I was spellbound by the language capabilities. JavaScript is an OOPL! And you can write neat and clean OO Programs leveraging objects, inheritance and polymorphism. One of the trademark of JavaScript is that it’s a loosely typed language.

So you can have

var a = 1;

a = “Arun”;

a = new A;

Interesting? a can be a integer, string or an object of class A. No need for explicit declarations. Well some might argue that this kind of coding will lead to readability, maintainability problems. I think its just a matter of discipline and moreover you don’t expect your complete Business Logic inside a JavaScript.

Another interesting concept which Joel touches in his post is the functor. A functor is something like a function pointer (an explanation for a C guy). A functor is a function that can be manipulated as an object, or an object representing a single, generic function. So functors support functional programming and JavaScript too supports functional programming.

 Here is an example of how you define a class in JavaScript

function Foo()
this.i = 1;
this.string = “Arun”;
obj = new Foo;

 Foo() looks like a function and can be used like a function. Well Foo can also be used like a Class. So in the above example obj is the instance of class Foo. And another cool example in Joel’s post is that you can easily assign a function to a variable.

 Coming to Java; our bread and butter language. Java being a very rigid language such luxuries are a far, far cry here. Why does Java doesn’t have Auto Variables like C# or Python? See an interesting post here.

I feel Java can also imbibe some of these cool features and become more dynamic, especially concept of Auto Variables as was illustrated in above example of var a in JavaScript.

 An example problem which we faced in our project was like this

We have

class parent

{ void buildReport(params); }

 class child1 extends parent

{ void buildReport(params); }

 class child2 extends parent

{ void buildReport(params); }

 class child3 extends parent

{ void buildReport(params);}

 class child3 extends parent

{ void buildReport(params);}

  child1, child2, child3 and child4 extend parent and implements the buildReport() function. Now this was a Template Pattern implementation. So buildReport() was my template function which internally called 4 steps, which I haven’t mentioned here.

 Now the catch is the params. In each of those childs I need to pass different params. In child1  I need to pass a int. In child2 I need to pass String, In child3 I need to pass a object and in child4 an array.

 I cannot have different signatures for buildReport() because that will break the Template Pattern as well as polymorphism which we cannot do away with.

 Now the solution would be keep the params as Object. So what happens to int, use Integer() ?. Or pass it as Collection in a Vector or HashMap. And retrieve the relevant stuff from the HashMap depending on which child you are in?

 We used the 2nd approach. I think in JavaScript it (params) would have been just a var. This is just one side. Now think of the same if a function buildReport() starts returning new types for each of the child.

 So my calling program will become nasty with if else ladders checking instance of the class.

Like this

Parent p = new child3();

 if ( p instanceof child1 )

 int i = p.buildReport(); //child1 I expect int

else if ( p instanceof child2 )

 String s = p.buildReport(); //child2 I expect String.

 I think we have lost the usage of the Template Pattern here by building a if-else ladder in the main code.

 Does auto solve this? Should Java really think of becoming a bit more lenient and becoming more flexible (loosely typed) like Python or C#.

 These are just my thoughts. May be I am wrong also, let me know your thoughts if any. Meanwhile you can go and start learning JS and start getting bemused by it.


2 thoughts on “Functors and Autos

  1. Pingback: Something Bizzare » Closures in Java?

  2. To the best of my knowledge, Apple, C# is an equally strongly typed language. It has been ages since I did something with it (C#), but that is what I remember. It follows the C philosophy. Of course, you can use the Object class, but that would not solve the problem. Good post!

Leave a Reply

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

You are commenting using your 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