CSC/ECE 517 Spring 2013/ch1b 1i lh
- Here is a link to the writing assignment for this topic.
Meta-programming in Dynamically Typed Languages
This page discuss the implementation and uses of meta-programming in dynamically typed languages
Meta-programming
Meta-programming is a programming technique of writing computer programs that write or manipulate other programs or themselves, as their data. [1] This technique allow us to write program that modify its own behavior at run-time (as opposes to recompiling the program.)
In meta-program, the language in which the meta-program is written is called the metalanguage. The language of the programs that are manipulated is called the object language.[2]
The ability of a programming language to be its own metalanguage is called reflection or reflexivity.[3]
Application of Meta-programing
First, you can write programs that will pre-generate tables of data for use at runtime. For example, if you are writing a game and want a quick lookup table for the sine of all 8-bit integers, you can either calculate each sine yourself and hand-code it, have your program build the table at startup at runtime, or write a program to build the custom code for the table before compile-time. While it may make sense to build the table at runtime for such a small set of numbers, other such tasks may cause program startup to be prohibitively slow. In such cases, writing a program to build static data tables is usually your best answer.
Second, if you have a large application where many of the functions include a lot of boilerplate code, you can create a mini-language that will do the boilerplate code for you and allow you to code only the important parts. Now, if you can, it's best to abstract out the boilerplate portions into a function. But often the boilerplate code isn't so pretty. Maybe there's a list of variables to be declared in every instance, maybe you need to register error handlers, or maybe there are several pieces of the boilerplate that have to have code inserted in certain circumstances. All of these make a simple function call impossible. In such cases, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.
Finally, a lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.
As languages acquire more features, code-generating programs get less appealing. What is available as a standard feature of one language may be available only through a code-generating program in another language. However, inadequate language design is not the only reason for needing code-generating programs. Easier maintenance is also a reason.
Dynamically Typed Programming Languages
Programming languages can be split into two categories base on their type system, dynamically and statically typed. A programming language is said to use static typing when type checking is performed during compile-time. In comparison, dynamically typed languages deferring type checking until run-time as opposes to compile-time. [4]
In dynamically typed languages, the variables and parameters do not have a designated type and may take different values at different times. In all the operations, the operands must be type checked at run-time just before performing the operation. Dynamically typed languages don’t need to make a distinction between classes created at compile time and classes provided. It is possible to define classes at run time and in fact, classes are always defined at run time. These eliminate many developer constraints by avoiding the need of book keeping, declarations etc. Due to this flexibility these languages make an ideal candidate for prototyping and are widely used in agile development environments. However, dynamic languages are known to have performance issues. Static languages have code optimization features at compile time, but dynamic languages allow run-time code optimizations only. [5] In dynamically typed languages, the interpreter deduces type and type conversions, this makes development time faster, but it also can provoke run-time failures. These run-time failures are caught early on during compile time for statically typed languages.
Examples of dynamically typed languages:
- Perl
- Python
- JavaScript
- PHP
- Ruby
- SmallTalk
Implementation of Metaprograming in Dynamiclly Typed Languages
Shell Script
A shell script is a script written for command line interpreter of an operating system.
In the simplest form shell script can be use for metaprogramming by generating or source code or modifying than recompiling a existing program.
for example
Ruby
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. It was also influenced by Eiffel and Lisp[8]. Ruby was first designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
Example of metaprograming in Ruby
In ruby you can write methods using metaprogramming to insert code into the program.
A widely use example of such metaprogramming method is the attr_accessor method in the class named Module
In this example attr_accessor method is use to insert the accessor codes to the class
class MyClass attr_accessor :info end
Is the same as
class MyClass def variable=(value) @variable = value end
def variable @variable end end
The method method_missing() is another example of metaprograming in Ruby.
In ruby if the interpreter can’t find the method anywhere up the object’s chain of inheritance, it will go back to the object and call the method named method_missing(). The interpreter looks for method_missing() in the object’s methods and up the object’s chain of inheritance until it reaches the Object class where method_missing() is defined.
class MyClass def method_missing(name, *args) puts "#{name} was called with arguments: #{args.join(', ')}" end end
m = MyClass.new m.undefined_method("one", "two") # => undefined_method was called with arguments: one, two m.another_undefined_method("three", "four") # => another_undefined_method was called with arguments: three, four
The class MyClass does not have a method named undefined_method() or another_underfined_method() but the method_missing() method created responds to these methods