CSC/ECE 517 Spring 2013/ch1b 1i lh: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(Created page with "*Here is a [https://docs.google.com/a/ncsu.edu/document/d/1aER6f9EDFJaUMgSpK7g5nYPiMNoUjZzwQxzY0jMW8mI/edit link] to the writing assignment for this topic. '''Metaprogramming in...")
 
No edit summary
Line 21: Line 21:


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 runtime 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 runtime code optimizations only. [http://www.slideshare.net/aminbandeali/dynamically-and-statically-typed-languages] In dynamically typed languages,  the interpreter deduces type and type conversions, this makes development time faster, but it also can provoke runtime failures. These runtime failures are caught early on during compile time for statically typed languages.
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 runtime 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 runtime code optimizations only. [http://www.slideshare.net/aminbandeali/dynamically-and-statically-typed-languages] In dynamically typed languages,  the interpreter deduces type and type conversions, this makes development time faster, but it also can provoke runtime failures. These runtime failures are caught early on during compile time for statically typed languages.
Examples of dynamically typed languages include Perl, Python,  JavaScript, PHP, Ruby, Groovy.


= Metaprogramming in Dynamiclly typed languages =
Examples of dynamically typed languages:
* Perl
* Python
* JavaScript
* PHP
* Ruby
* SmallTalk
 
= Implementation of Metaprograming in Dynamiclly Typed Languages =




Line 30: Line 37:
A [http://en.wikipedia.org/wiki/Shell_script shell script] is a script written for command line interpreter of an operating system.  
A [http://en.wikipedia.org/wiki/Shell_script 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 modifying and recompiling a existing program.
In the simplest form shell script can be use for metaprogramming by generating or source code or modifying than recompiling a existing program.
 
 
 
 
 
 
== Smalltalk ==
 


for example


== Ruby ==
== Ruby ==
Line 52: Line 53:
In this example attr_accessor method is use to insert the accessor codes to the class
In this example attr_accessor method is use to insert the accessor codes to the class


class MyClass   
class MyClass   
attr_accessor :info
  attr_accessor :info
end
end


Is the same as
Is the same as


class MyClass
class MyClass
def variable=(value)
  def variable=(value)
@variable = value
    @variable = value
end
  end


def variable
  def variable
@variable
    @variable
end
  end
end
end




The method method_missing() is another example of metaprograming in Ruby.
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.
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.
    
    
method_missing()
class MyClass
 
  def method_missing(name, *args)
class MyClass
    puts "#{name} was called with arguments: #{args.join(', ')}"
  def method_missing(name, *args)
  end
    puts "#{name} was called with arguments: #{args.join(', ')}"
end
  end
end


m = MyClass.new
m = MyClass.new
m.undefined_method("one", "two") # => undefined_method was called with arguments: one, two
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
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
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


= Implementation of Metaprograming in Dynamiclly Typed Languages =




Line 102: Line 101:
*[7] [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Fall_2010/ch2_S24_NS Expertiza Wiki Entry on Metaprogramming in Statically Typed Languages 2]
*[7] [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Fall_2010/ch2_S24_NS Expertiza Wiki Entry on Metaprogramming in Statically Typed Languages 2]
*[8] [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Fall_2010/ch2_S24_sk Expertiza Wiki Entry on Metaprogramming in What is Metaprogramming]
*[8] [http://wiki.expertiza.ncsu.edu/index.php/CSC/ECE_517_Fall_2010/ch2_S24_sk Expertiza Wiki Entry on Metaprogramming in What is Metaprogramming]
= Further Reading =
*[http://www.cs.tut.fi/~kk/webstuff/MetaprogrammingCpp.pdf Metaprogramming in C++]
*[http://en.wikipedia.org/wiki/Automatic_programming Automatic/Generative Programming]
*[http://www.slideshare.net/dogangoko/new-features-of-java7-se-presentation News in the upcoming Java 7]

Revision as of 01:54, 21 February 2013

  • Here is a link to the writing assignment for this topic.

Metaprogramming in Dynamiclly Typed Languages

Metaprogramming

Metaprogramming 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 runtime (as opposes to recompiling the progam )

In metaprogam, the language in which the metaprogram 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]


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 compareson, 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 runtime 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 runtime 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 runtime failures. These runtime 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



References