CSC/ECE 517 Fall 2010/ch4 4g HW: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 27: Line 27:
===Example===
===Example===


Duck typing is a well-known and interesting form of dynamic typing that appears prominently in Ruby.
Duck typing is a well-known and interesting form of dynamic typing that appears prominently in Ruby. Here, the type of the object is ignored in favor of the usable aspects of an object. As is often said, "if it walks like a duck, and quacks like a duck, then it is a duck" [[http://en.wikipedia.org/wiki/Dynamic_typing#Duck_typing]].


==Metaprogramming==
==Metaprogramming==

Revision as of 23:24, 19 October 2010


Introduction

Drawing from the concepts discussed in Chapter 2 Section 24, Metaprogramming in Statically Typed Languages, we will continue to examine metaprogramming concepts in the environment of dynamically typed languages. As always, the focus will be on object-oriented languages to demonstrate the common usages of metaprogramming.

As you may recall from the previous chapter, metaprogramming can be defined as "the technique that allows us to write programs that can manipulate other programs or themselves as a part of their data" [[1]]. Thus it allows us to add behavior to classes or objects that was not previously specified, often done at compile time instead of runtime. The same basic concepts still apply here, although in a few different fashions for dynamically typed languages instead of those that are statically typed.

This chapter will begin with a brief overview of object-oriented dynamic programming languages, or languages with an object-oriented bent, and progress into a further analysis of dynamic typing within those dynamic languages. The majority of the chapter will then cover metaprogramming within the previous described environment, offering examples, common methods of use, and existence in real-world software projects. Examples will be given to illustrate important concepts, and will be expressed in sample object-oriented dynamically typed languages.

Dynamic Languages

Dynamic programming languages are loosely defined as languages that are designed to exhibit, or provide as extra functionality, behaviors at run-time that are otherwise normally found at compile-time. This difference from static languages is exemplified in behaviors such as (during execution) adding new methods, classes or objects, extending existing classes or objects, and modifying the type system [[2]].

Common and useful examples of object-oriented dynamic languages include Ruby, Python, and Perl.

Ruby: Ruby is considered a pure object-oriented dynamic language with dynamic typing.

Python: Python is not technically (by definition) purely object-oriented, but it was designed mainly for such purposes, is dynamic and dynamically typed, and so is relevant to our discussions.

Perl: Perl is not completely object-oriented, but has essential object-oriented elements. It was designed as procedural but has since been extended to implement object-oriented mentalities, and is also dynamic and dynamically typed.

Dynamic Typing

Most but not all dynamic programming languages are also dynamically typed. Dynamic typing describes the method of type checking that occurs mainly at run-time, instead of compile-time. Put simply, the type of a variable is not statically defined; instead, a variable refers to a value, and only the value has a strict type [[3]]. This enables a more fluid style of programming, especially in areas like metaprogramming as we soon shall see.

Example

Duck typing is a well-known and interesting form of dynamic typing that appears prominently in Ruby. Here, the type of the object is ignored in favor of the usable aspects of an object. As is often said, "if it walks like a duck, and quacks like a duck, then it is a duck" [[4]].

Metaprogramming

Definition.

How the implementation is similar to or different from static.

Common Usage

Examples

Ruby

Python

Perl

Impact and Development

Real-world examples.

Proposed improvements in the given language-examples.

Conclusion

Review everything.

References