Monday, May 4, 2009

What does a dynamic language look like?

This intro is for those developers have not yet the chance of struggling with a great dynamic language like Python or Ruby. They may find out the different paradigm of programming in dynamic languages.
Here, anything is an Object. The concept of 'Object' is far away from what we are used to call in pure OOP languages like C# & Java. For instance, in Python, a class or type is an object! You know in C# we declare variables of classes and then construct them to make an object. The concept seems confusing. For clarification, I have to write some example:

s='This is a test'
t=type(s)
class myClass(t):
pass

As you can see, we have the type of string (str) in a variable named t.As you may know, python inherits a parent class using parenthesis.But here, we have not provided the compiler with name of the class. Instead, we have given a variable containing the class. We could define myClass type as follows:

class myClass(str):
pass

I think you may have found out the real meaning of 'dynamic' language. In static languages like C#, we cant by no means do this. Even reflection does not do the same thing for us, since we have to do anything on the fly, and we cant add code to our function.

Type t = typeof(string)
.
.
.
public class myClass : t ' >>>>>>> Error!
{
}

No comments:

Post a Comment