1.7k likes | 1.85k Views
Object Oriented Programming in APL. A New Sense of Direction (Oh, Oh!) Dan Baronet 2006 V1.01. Introduction. Interoperability & 64 bit OS New WS management New primitives & enhancements to others Object Orientation. V11 has been released. Here are the major features:.
E N D
Object Oriented Programming in APL A New Sense of Direction (Oh, Oh!) Dan Baronet 2006 V1.01
Introduction • Interoperability & 64 bit OS • New WS management • New primitives & enhancements to others • Object Orientation V11 has been released. Here are the major features: OOAPL
This talk is based on 2 things: • Why? Why should I know about this? Why am I here? • OK, I’m still here, how do I use it? OOAPL
Synopsis Why should I be here? • Reasons How do I use this? • History of OO • Definitions • The APL way • Implementation details Real Life Example (SJT) OOAPL
Reasons: why do this? • Because Object Orientation is a Valuable Tool of Thought! • To be able to Share Tools and Components more Easily • To help us Manage Complexity in all Project Phases • To use .Net more easily OOAPL
.Net was already in V10 The ability to use .Net with classes was already there in V10 but it was more difficult to use. OOAPL
History OO is not new, it’s been around for a while. Pretty much the same story as APL 50s: conception 60s: 1st implementation (SIMULA compiler) 70s: 1st user base, Smalltalk compiler 80s: into Eiffel to bridge the “gap” 90s: popularity grows (≠APL) thanks to C++ and Java OOAPL
The evolution of data • Elementary data elements (like int) and arrays of them (all the same) • Structures (like {i: int; d: date}), many elementary elements or structures (recurs) • Namespaces (structures whose elements are referred to by name) • Classes (namespaces with initializers & cleaners) OOAPL
Data in Dyalog APL (DYW) In DYW all these objects exist. You can often simulate one by another. For ex: a table of names & values can represent a namespace of variables. DYW can also store the ⎕OR of a namespace. OOAPL
The evolution of data In traditional (procedural) systems, data is separated from code: d a t a code OOAPL
The evolution of data This is the same in traditional APL systems, data is separated from code: OOAPL
private code private code d a t a d a t a The evolution of data In OO systems, data is more tightly coupled with code: Do this Do that OOAPL
private code private code d a t a d a t a The evolution of data In OO APL systems, data is also tightly coupled with code but in a more procedural manner: OOAPL
The Object Oriented paradigm Definitions OOAPL
OO fundamental concepts The Object Oriented paradigm is based on: • Classes • Instances • Members • Message passing • Inheritance • Encapsulation • Polymorphism OOAPL
OO fundamental concepts 1. Classes A class defines the abstract characteristics of a thing, akin to a blueprint. It describes a collection of variables and methods (functions), possibly with a constructor method that can be used to create objects of the class and a destructor to remove the objects thus created. OOAPL
OO fundamental concepts Classes A classcan implement one or more interfaces. An interface describes what it should do. The class describes how it should be done. If a class implements an interface it should do it completely, no partial implementation is allowed. OOAPL
Interfaces - example The following maneuvering interface describes what has to be done with a machine: Method Steer (direction); // where to go Method Accellerate (power); // go faster Method SlowDown (strength); // go slower It does NOT specify how it should be done. OOAPL
Interfaces - example This car object implements maneuvering: Class car : maneuvering; Method SteeringWheel: implements maneuvering.Steer (turn); ... Method GasPedal: implements maneuvering.Accellerate (push); ... Method BreakPedal: implements maneuvering.SlowDown (hit); ... OOAPL
Interfaces - example This plane object implements maneuvering: Class plane : maneuvering; Method Stick: implements maneuvering.Steer (move); ... Method Handle: implements maneuvering.Accellerate (pushpull); ... Method Flaps: implements maneuvering.SlowDown (degree); ... OOAPL
OO fundamental concepts 2. Instances Those are tangible objects created from a specific class. Upon creation any specific action is carried out by the constructor of the class if need be. Upon destruction, the destructor is responsible for cleaning up. OOAPL
OO fundamental concepts 3. Members This is the code and the data of a class. The code is divided into Methods (functions) and data is divided into Fields (variables). There are also Properties which are Fields implemented via functions to read/set them. All these are visible from outside when Public or invisible from outside if Private. OOAPL
OO fundamental concepts Members: Methods Those can either reside in the class (shared method) or in the instance (instance method). There are also abstract methods (e.g. in interfaces) with no code, only calling syntax. Constructors and destructors are methods. OOAPL
OO fundamental concepts Members: Fields Those can either reside in the class (shared field) or in the instance (instance field). They can also be read only. OOAPL
OO fundamental concepts Members: Properties Those are Fieldsimplemented by accessing methods, i.e. PropX←value is implemented by SET value They can either reside in the class (shared property) or in the instance (instance property). OOAPL
OO fundamental concepts 4. Message passing This is the (usually asynchronous) sending (often by copy) of a data item to a communication endpoint (process, thread, socket, etc.). In procedural languages it corresponds to a call made to a routine. OOAPL
OO fundamental concepts 5. Inheritance This is a way to avoid rewriting code by writing general classes and classes that derive from them and inherit their members. This helps achieve reusability, a cornerstone of OO. OOAPL
OO fundamental concepts Inheritance We say that a (derived) class is based on another one. All classes (but one) are derived from another one or by System.Object (the default) OOAPL
OO fundamental concepts Inheritance: Based classes (reusability) A class can be based on another based class. See classCollie based on Dog based on Animal Big Dane: Dog Instances Dog: Animal Fido (Collie) Animal Classes Collie: Dog Fish: Animal Rex (Collie) OOAPL
OO fundamental concepts Inheritance When an instance is created from a class based on another one it inherits all its members automatically. Members can be redefined but subroutines must be specifically set to override base class subroutines. OOAPL
OO fundamental concepts Inheritance: multiple inheritance A class can be based on several other classes. See class Mule made out of 2 different classes. Horse Mule Donkey OOAPL
Override concept Class B Class A M1 M2 M1 M2 M2 ‘I am B’ M2 ‘I am A’ // M1 calls its own M2: (NEW B).M1 I am B // M1 calls its own M2: (NEW A).M1 I am A OOAPL
Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2 M2 ‘I am A’ M2 ‘I am B’ // M1 calls its own M2: (NEW B).M1 I am A // M1 calls its own M2: (NEW A).M1 I am A OOAPL
Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2 M2: Override ‘I am B’ M2 ‘I am A’ // M1 calls its own M2: (NEW B).M1 I am A // M1 calls its own M2: (NEW A).M1 I am A A:M2 does not allow to be overriden OOAPL
Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2: Overridable M2: Override ‘I am B’ M2 ‘I am A’ // M1 calls its own M2: (NEW B).M1 I am B // M1 calls its own M2: (NEW A).M1 I am A A:M2 does allow to be overriden AND B wants to override it OOAPL
OO fundamental concepts 6. Encapsulation • Hides the underlying functionality. • It conceals the exact details of how a particular class works from objects (external or internal) that use its code. • This allows to modify the internal implementation of a class without requiring changes to its services (i.e. methods). OOAPL
Example: a car Consider a car: the functionality is hidden, it is encapsulated, you don’t see all the wiring inside, you’re only given specific controls to manipulate the car. OOAPL
Encapsulation To allow access to a member there are various level definitions. For example, to expose a member you must use some syntax that will declare it public. By default, private is the norm. OOAPL
OO fundamental concepts Encapsulation: private A private member cannot be seen from anywhere. Only other Methods can access it from inside the class if private shared or from within the instance if private instance. M is only seen in the pale area sub1 sub2 OOAPL
OO fundamental concepts Encapsulation: protected A protected member can only be seen from within the instance of from within instances within it. M is only seen in the pale area sub1 sub2 OOAPL
OO fundamental concepts Encapsulation: public A public member can be seen from anywhere. All Methods can access it from anywhere whether it is public shared or public instance. M is seen in all the pale area sub1 sub2 OOAPL
OO fundamental concepts Encapsulation: friend A class declared friend can see the offerer’s private and public members. Class B Class A Private Av Public Au Private Pv Public Pu Friend A Here A sees B’s members as its own members OOAPL
OO fundamental concepts 7. Polymorphism It is behavior that allows a single definition to be used with different types of data. This is a way to call various sections of code using the same name. Typed languages can tell the difference by looking at the “signature” of the functions (their number of arguments and their types) OOAPL
OO fundamental concepts Polymorphism example Foo (integer arg) { // fn called with // an integer arg Print “Int arg” } Foo (string arg) { // fn called with // a string arg Print “String arg” } Same function name, different code OOAPL
OO fundamental concepts Polymorphism A program that accepts multiple definitions is said to be overloaded. Sometimes arguments have to be coerced into a new type to achieve proper behaviour. OOAPL
OO Concepts Summary • OO is based on +-7 key concepts • Classes are blueprints • Instances are objects created from classes • They contain members (data & code) • Their access is restricted • The relation between classes is based on inheritance or specific access OOAPL
End of OO basics QUESTIONS ? OOAPL
The Dyalog APL way OOAPL
Implementation • Dyalog follows C# rules closely • The extensions to the language have been made in following with the :Keywords scheme introduced in the 90s (All new keywords start with ‘:’ ) • Some new system functions have been added and • Some new system commands also OOAPL
Implementation Let’s see how Dyalog APL does this: • Classes • Instances • Members • Message passing • Inheritance • Encapsulation • Polymorphism OOAPL