Linux Journal May 2012 by Linux Journal

Linux Journal May 2012 by Linux Journal

Author:Linux Journal
Language: eng
Format: epub, mobi
Tags: Linux, Lua, App Inventor, LTSP, Pure Data, Pd, C, Python, DRBD, Pacemaker, Android, iftop, ZaReason Valta X79
Publisher: Belltown Media
Published: 2012-04-25T16:00:00+00:00


--The “points” list contains 2D and 3D points.

> printpoints(points)

(10,10)

(20,20)

(30,30,1) -- This is a 3D point!

Note how the points in the list were able to respond to the “print” message, each using its corresponding behavior, meaning 2-D points printed their (x,y) pairs, whereas the single 3-D point correctly printed its (x,y,z) tuple. This is, effectively, Polymorphism in Lua.

If you are feeling uneasy about the concept of Interfaces, remember that the true meaning of Polymorphism is not in implementing Interfaces, but rather in having objects dynamically respond to the messages received according to their type. Dynamically typed programming languages are, therefore, an ideal fit for Polymorphism.

Inheritance

The last major concept common to object-oriented programming that I’m going to tackle here is Inheritance. How can you add an Inheritance mechanism to the object-oriented model I’ve been discussing?

Unlike Polymorphism, when modeling type Inheritance, it is not enough to have the objects ascribe to the same Implicit Protocol and rely on the dynamism of Lua. In order to implement Inheritance, you need to describe a relationship effectively between the types of the base data type (the base class) and the derived data type (the derived class).

What you need to do is be able to “extend” an object type with a newer, “more concrete” type that adds specific logic.

The way to achieve this using Lua might seem strange at first, but if you bear with me, you will see it naturally matches the concepts used here thus far.

To continue with this example, let’s add to the system the ability to represent 3-D points in homogeneous coordinates. In homogeneous coordinates, each 3-D point is represented using four values (x,y,z,w).

When converting from Cartesian coordinates to homogeneous coordinates, you just need to set the w value to 1. To convert from homogeneous coordinates back to Cartesian coordinates, you need to divide all components of the point by the w value, therefore taking (x,y,z) back to Cartesian coordinates.

The 3-D point (1,1,1) in homogeneous coordinates would be (1,1,1,1), as well as (2,2,2,2), (3,3,3,3) and so on. If you are not convinced, try dividing by w in each case and see what happens.

Points in 4-D homogeneous coordinates are 3-D points, meaning, Points in homogeneous coordinates (PointH) have an “is a” relationship with class Point3D. This is an Inheritance relationship you can represent in the object model.

Assuming class Point3D exists and is similar to Point, you start by stating that PointH (4-D homogeneous coordinate points) are 3-D Points:

PointH = Point3D:new({x=0, y=0, z=0, w=1})

What I did here, conceptually, was to declare PointH to be a new class that “Inherits” all the behavior from Point3D. In particular, its metatable will point to Point3D.

What’s interesting is the fact that when you create a new PointH instance called “ph”, by using the PointH:new() method, Point3D’s constructor will be called, but with the “self” object pointing to (table) PointH instead. Therefore, the “ph” instance will have its metatable pointing to PointH and not Point3D.

This achieves a “chain” of metatables. PointH instances will have PointH as their metatable, whereas class PointH will have Point3D as its metatable.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.