Friday, January 18, 2008

A Tale of Two Loosely Typed Languages



Disclaimer: I love JavaScript and this blog is not a defaming blog for JavaScript.


We all know that JavaScript is a loosely typed language; there are advantages of being loosely typed. You need not to think about what will be the data types of a particular variable and work with that variable. But a newbie to a loosely typed language will miss a lot of details which is required to code in such a language. (As I am writing this I consider me as newbie in most of the cases in programming). But still this blog is all about the mistakes that we all make and learn from them.

If you are reading this blog I can safely assume that you have some interest in programming in languages like JavaScript, Lisp or Scheme which are strictly speaking examples of great loosely typed language. Albeit I have doubts in my mind about ECMAScript that it might not remain as loosely typed as now with its reserve words containing char, int, long, float etc. But nevertheless I will take presently implemented format of well good JavaScript which still allows me and other programmers to write code free from types.

The most important concept while programming in any loosely typed programming language is the understanding of how my function is going to behave on certain inputs. If the function is a pure function you need not to worry, however in WWW we do not write much of a code like pure functions. But even pure functions can behave absolutely insane if you allow them to do so. How? Here is a code for a function in JavaScript which takes two numbers as its arguments and adds them (If you do not know JavaScript you can click here for a tutorial).

function add(x,y) { alert(x+y); }

I call this function as add(4,5) and as expected it pops 9. Good now if I try to call this function like this add(“hello”,5) what am I suppose to get?

Let us now stop this for a moment and try the same function in Lisp, the function in Lisp should look like:

(defun add(x y) (+ x y))

Now if I call the function in Lisp like (add 4 5) it will evaluate to 9 but if I try to call this add function with “hello” and 5, we will be in the debugger saying something like ‘Argument X is not a NUMBER: “hello”’ (my Lisp is SBCL).

Now what happens in JavaScript it will pop you a message box saying “Hello5”. That’s right, the + operator in JavaScript does two things it adds in case of a number and it also concatenates strings and moreover when it finds a string in any side of it, it forgets the other job that it can perform and it just concatenates. That is ok for me a language can do that like Java does the exact same thing. So to be sure about what you want from your function you need to check your input for certain type before you do this addition operation.

In JavaScript I as a programmer has to make sure that all the arguments to the function are as I was expecting before I pass them to an operation. In our case it is addition. In Lisp we can we will use restarts using condition handlers. We need to understand here that first way is static checking and the second one is dynamic checking at the run-time. The importance of conditional restarts are more felt where we have to take care of more complex functions.

3 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.