CSC/ECE 517 Fall 2010/ch6 6a RJ: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 80: Line 80:
==== Io and Object Oriented Programming ====
==== Io and Object Oriented Programming ====


In Io, everything is an object (including the locals storage of a block and the namespace itself) and all actions are messages (including assignment). Objects are composed of a list of key/value pairs called slots, and an internal list of objects from which it inherits called protos. A slot's key is a symbol (a unique immutable sequence) and its value can be any type of object.
Io is a prototype-based language. There are no classes and instances; there are only objects.
 
Objects are typically cloned (from Object or some other existing object) and then extended, i.e. attributes are added to it on the fly. Objects are composed of a list of key/value pairs called slots, and an internal list of objects from which it inherits called protos. A slot's key is a symbol (a unique immutable sequence) and its value can be any type of object.
   
   
=====<b>clone and init</b>=====
=====<b>clone and init</b>=====

Revision as of 04:56, 17 November 2010

Survey of selected Delegation-based Programming Languages

Delegation-based programming languages, also known as Prototype-based programming languages, represent a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as class-less, prototype-oriented or instance-based programming. Delegation is the language feature that supports prototype-based programming.

The original, and most canonical, example of a prototype-based language is the programming language Self developed by David Ungar and Randall Smith. Since the late 1990s, the classless programming style has grown increasingly popular, and has been adopted for the languages Javascript, ActionScript, Cecil, NewtonScript, Io, Moo, REBOL, Lisaac, Lua and several others.[1]. This topic will briefly review features of three Delegation-based programming languages: Lua, Io, and Actionscript.


Lua

Lua is a powerful, fast, lightweight, embeddable scripting language. It combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping [2]]. Lua is a free open-source software product.

History of Lua

Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, members of the Computer Graphics Technology Group (Tecgraf) at the Pontifical Catholic University of Rio de Janeiro]in Brazil. [3] The design of the Lua programming language was influenced by the programming languages Scheme, SNOBAL, Modula-2, CLU, and C++.

Lua and Object Oriented Programming

Lua isn't an object oriented languages in the sense that you define classes, create objects with a new method, define interfaces and declare variables to be private or protected. Lua is a tool in which you can use object oriented techniques with a freedom and flexibility that you don't have with a straitjacket language such as Java.[4]

Below is a look at some coding in Lua:

Initially, a couple of named pieces of code (functions) are defined which take a table as their parameter, and some functionality is provided within them that manipulates the table:

function effage(animule)
  return animule.age * animule.factor
  end
function stone(thecrows)
  return "as da hills"
  end

Then a number of tables are defined, each representing a single "something" [object] which contains attributes / properties / characteristic values (choose whatever you feel is the most appropriate word.) Significantly, we also add in to the table the function code, just defined above, which can operate on the other elements within that table. This can be done because functions in Lua are held in "first class variable" just like any other user supplied elements, and that an assignment copies a reference, so that we're not cloning hundreds of copies of what could be substantial code.

gypsy = {breed = "dog", age = 3, weight = 28,
  factor = 7, name = 'Gypsy Ellis', efa = effage}
rocky = {breed = "rock", weight = 28,
  type = "Granite", name = 'Rocky Bamboa', efa = stone}
charlie = {breed = "cat", age = 13, weight = 4,
  factor = 6, name = "Charlie Ellis", efa = effage}
flipper = {breed = "dolphin", age = 7, weight = 300,
  factor = 2, name = "Flipper Lichtenstein", efa = effage}

Although "structured" programmers might have chosen to have an array of breeds, an array of ages, etc, we have chosen to use a separate table for each of the disparate value types associated with an individual item - and we can now make up a table of each of those items - a table of tables, if you like, or an "array of objects" if you would like me to start using OO terminology:

pets = {gypsy, charlie, rocky, flipper}

We can then loop through our array of objects, accessing individual member elements (attributes / properties) by name, and running pieces of code which are referenced from within each of them to perform actions on the data they hold ...so that those functions have now turned (in OO terms) into methods. With different methods referenced within differing objects, you'll see that we've implemented polymorphism via the back door!

for k,v in pairs(pets) do
  print (k,v,v.name)
  print ("yay",v.efa(v))
  end


Javascript

Info about Javascript Language goes here.

Actionscript

Info about Actionscript Language goes here.

Io

Io is a dynamic prototype-based programming language. The ideas in Io are mostly inspired by Smalltalk[1] (all values are objects), Self[2] (prototype-based), NewtonScript[3] (differential inheritance), Act1[4] (actors and futures for concurrency), Lisp[5] (code is a runtime inspectable / modifiable tree) and Lua[6] (small, embeddable).

Io's purpose is to refocus attention on expressiveness by exploring higher level dynamic programming features with greater levels of runtime flexibility and simplified programming syntax and semantics. link

History of Io

The language was created by Steve Dekorte around March 7, 2002, after trying to help a friend, Dru Nelson, with his language, Cel. He found out that he really didn't know much about how languages worked, and set out to write a tiny language to understand the problems better. [xx]

Io and Object Oriented Programming

Io is a prototype-based language. There are no classes and instances; there are only objects.

Objects are typically cloned (from Object or some other existing object) and then extended, i.e. attributes are added to it on the fly. Objects are composed of a list of key/value pairs called slots, and an internal list of objects from which it inherits called protos. A slot's key is a symbol (a unique immutable sequence) and its value can be any type of object.

clone and init

New objects are made by cloning existing ones. A clone is an empty object that has the parent in its list of protos. A new instance's init slot will be activated which gives the object a chance to initialize itself. Like NewtonScript[3], slots in Io are create-on-write.

 
me := Person clone

To add an instance variable or method, simply set it:

myDog name := "rover"
myDog sit := method("I'm sitting\n" print)

When an object is cloned, its "init" slot will be called if it has one.

Inheritance

When an object receives a message it looks for a matching slot, if not found, the lookup continues depth first recursively in its protos. Lookup loops are detected (at runtime) and avoided. If the matching slot contains an activatable object, such as a Block or CFunction, it is activated, if it contains any other type of value it returns the value. Io has no globals and the root object in the Io namespace is called the Lobby. Since there are no classes, there's no difference between a subclass and an instance. Here's an example of creating the equivalent of a subclass:

Io> Dog := Object clone ==> Object_0x4a7c0 The above code sets the Lobby slot "Dog" to a clone of the Object object; the protos list of this new object contains only a reference to Object, essentially indicating that a subclass of Object has been created. Instance variables and methods are inherited from the objects referenced in the protos list. If a slot is set, it creates a new slot in our object instead of changing the protos:

 Io> Dog color := "red"
 Io> Dog
 ==> Object_0x4a7c0:
   color := "red"


Delegation-based Programming Languages compared to Class-based Programming Languages

Intro

Delegation-based Programming Languages compared to C++

Delegation-based Programming Languages compared to Java

Delegation-based Programming Languages compared to Ruby

References

[1] http://en.wikipedia.org/wiki/Prototype-based_programming