CSC/ECE 517 Fall 2010/ch4 4g km

From Expertiza_Wiki
Jump to navigation Jump to search

Metaprogramming in dynamically typed languages


Introduction

In the previous article for which the link does not exist yet, we learn that command patterns in static and dynamic languages provide separation of objects that request actions from the objects that ultimately perform actions. This is done by encapsulating the request for an action on a specific object. These command patterns are executed at runtime and simply hide the fact that another program is being called. Another technique of encapsulating behavior is for the program to have changes initiated at compile time. This type of design requires that the program not only can call another piece of code at runtime but it can build these calls at compile time via metaprogramming. In this article we take a closer look at metaprogramming in dynamically typed languages.

What is Metaprogramming?

Metaprogramming is the ability for a computer program to manipulate itself or other programs at the time of compilation as opposed to performing this manipulations at runtime. This tends to allow for greater flexibility for a program to handle new situations and plan for growth. Metaprogramming is ideal for several scenarios to include the use of application programming interfaces (API's), programs writing programs, and working with other programs outside of itself. API's allow the run-time engine to be exposed externally where it normally would not be. When a program writes another program that just means the running program dynamically outputs strings that can later be ran as en executed program. And finally, working with other programs outside itself refers to the running program's ability to accept language descriptions that it can execute transformations of the outside language. Ideally metaprogramming has one main objective, to allow for new features.

Example of Metaprogramming

There is a classic example of use for metaprogramming that involves some form of writing a program that outputs some set of numbers in order but the trick is that it cannot be done in a loop. For this example we will ask for a program that outputs the numbers from 1 to 100 without using any looping. Ideally one would like to write the following (in pseudo code):

 for i = 1 to 100 
   print i

which outputs

 1
 2 
 ...
 100

however since a loop is not allowed, you could use the loop to write code that will write the code:

 for i = 1 to 100
   print "print i"

which outputs

 print 1
 print 2
 ...
 print 100

which then outputs

 1
 2 
 ...
 100

the elegance of this technique is that it is very easy to repeat or manipulate.

Taking the same pseudo code example, we can see how each of the following languages would incorporate this.

Python

  f = open('outputfile.py', 'w')       // Open the file for writing
  f.write('#!/usr/bin/python \n')
  for i in range(1, 100):
     f.write('print' + i + '\n')       // Write the string to a file
  f.closed                             // Close the file 

JavaScript

  f = fopen('outputfile.js', 3);       // Open the file for writing
  if(f!=-1) {                          // If the file has been successfully opened
     for (i=0;i<=100;i++) { 
        str = 'document.write' + i;
        fwrite(f, str);                // Write the string to a file
     }
     fclose(f);                        // Close the file 
  }
  

Ruby

  f = File.open(outputfile.rb, 'w')    // Open the file for writing
  1.upto 100 do |i|
     f.write("puts #{i}")              // Write the string to a file
  end
  f.close                              // Close the file

Dynamic Typed Programming Languages

Dynamic typed programming languages are often referred to as 'weak typed' languages since it is not required for the variables to bound to a particular type at compile time instead each variable is bound to an object. Another unique characteristic of dynamic languages is that while variables need to be defined before they can be used, you don’t need to define them at the beginning of the program, just define it before its first usage. These characteristics are allowed because it is not the compiler that checks for proper variable declaration, these type errors will only occur at runtime and potentially the program will suddenly stop due to a crash.

Metaprogramming in some Dynamic Languages

We have already seen how some languages can be used for programs to write programs, now lets examine how they handle one of the other characteristics of metaprogramming: Manipulate itself at runtime. Each of the following will take a class (SomeClassThatAlreadyExists) that was already defined and add an new method (some_method) to it.

Python

  class SomeClassThatAlreadyExists
     def some_method
        print "some code"
     end
  end

JavaScript

  SomeClassThatAlreadyExists.prototype.some_method = function () {
     document.write("some_code");
  }

Ruby

  class SomeClassThatAlreadyExists
     def some_method
        puts "some code"
     end
  end

Putting it all together

How does metaprogamming work with dynamic typed languages?

  • Interpreted Languages

Most dynamic languages are interpreted, not compiled. The basic idea here is for the code to be interpreted at run-time, allows generic objects to dynamically interact with each other. This interaction in itself is potentially manipulating the behavior of the program as it is executing. Generic objects are not the only uniqueness of an interpreted language, the 'eval' statement is something else to consider. The ability to evaluate an expression via the 'eval' statement allows for the program to redefine functions at run-time. These two capabilities for dynamic languages have become immensely valuable to programmers.

  • Dynamic methods

These types of methods are very flexible. The program have have an object created from a class, then you can add additional methods to the instance of that class. Therefore manipulating the program at run time by allowing for only this instance of the object to have additional methods. Not to be confused with extending classes or adding additional methods to classes.

  • Singletons classes

Singleton classes should not be mistake for the Singleton design pattern. These types of classes available in some dynamic languages allow for dynamic dispatching. An common technique often used on singleton classes is to add dynamic methods to the instance.

  • "Everything is an object"

Many languages a 'weak-typed' which means that potentially anything can be anything else. The most obvious way to have this behavior is by making everything an object. What historically are considered primitive data types now are all considered objects (e.g. string, integers, etc). In some of these languages even functions and procedures are considered objects. In some languages such as Ruby, even classes are Objects since they (as well as everything else) inherits from Object. Since classes can be used to define, extend, or redefine other classes (often referred to as a metaclass) this is also potentially changing the behavior of the original program.

Some other traits of some common dynamic languages worth mentioning are: the use of method_missing, introspection, lazy loading, declarative code, extensible type system, and method aliasing.

Conclusion

Dynamic languages are increasingly recognized for playing a major role in software development, specifically for quick prototyping because of the forgiveness of the languages letting requirements evolve. These languages are historically looked at as scripting languages that are inexpensive and not scalable. Allowing for these types of behavior comes at a price. The price is the potential for poor performance since the code is runtime optimized. Another downfall is that rapid development is great but the flexibility allowed between type conversions means that there is a higher probability for runtime errors. While understanding these limitation and accepting the risks, these languages can be very powerful specifically in the area of Metaprogramming.

What’s Next?

In the next article for which the link does not exist yet, we will look at static-analysis tools for Ruby.

References

Websites:

Books/Articles: