1.68k likes | 1.69k Views
Explore Object-Oriented Programming in APL in this comprehensive guide. Learn about object orientation, concepts of the OO paradigm, namespaces, reusability, classes, and more. Discover how to simplify your programming using objects in APL.
E N D
Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24
Dyalog V11 was released in 2006. One of the major features was: Object Orientation OOAPL
This presentation is based on two things: • What is Object Orientation programming? • How do I use it in APL? OOAPL
Another tool Object Orientation is another tool for programmers. Users won’t see any difference. It is a way for programmers to simplify their lives by supplying each other with already made objects (almost) ready to use. OOAPL
What is an object? • An object contains dataand code to handle it • e.g. Name and surname and a function to show them nicely OOAPL
Concepts of the OO paradigm The namespace From the beginning APL had this kind of notion in the form of the workspace Namespace = Workspace? OOAPL
Concepts of the OO paradigm The workspace • It contains everything needed to run an application OOAPL
Concepts of the OO paradigm The workspace • It contains everything needed to run an application • It can be initialized (via []LX) OOAPL
Concepts of the OO paradigm The workspace • It can be initialized (via []LX) • It can be viewed as an object OOAPL
Concepts of the OO paradigm The workspace • It can serve as base and be copied into another workspace Current Workspace In memory FilesUtils FilesUtils )COPY FilesUtils OOAPL
Concepts of the OO paradigm The workspace • It contains everything needed to run an application • It can be initialized (via []LX) • It can be viewed as an object • It can serve as base and be copied into another workspace • It can be modified OOAPL
Concepts of the OO paradigm The workspace problems • Name clashing (using )COPY) Fn1 Fn3 Var1 varx Fn2 varx Fn2 varx )COPY FilesUtils OOAPL
Concepts of the OO paradigm The workspace problems • Name clashing (using )COPY) • They cannot be stored as a unit on file • Only 1 workspace item (variable or function) per component in APL file • Need to cleanup when finished OOAPL
Concepts of the OO paradigm The workspace (solutions) The package or overlay • They group items (vars & fns) together • They act as a (static) unit • They can be stored on file • They can be copied, like wss, into existing code and dispersed in it OOAPL
Concepts of the OO paradigm The workspace – packages They were only a partial solution to a more general problem • Name clashing still happened • Clean up was still needed OOAPL
Concepts of the OO paradigm The workspace – namespace In the 80s a Dyalog came up with the concept of namespace, probably from other languages like C++ and Java Namespaces are like workspaces but nested. OOAPL
Concepts of the OO paradigm DOS V1.0 APL V1.0 File1,ext var1 File2.ext fn1 Abc.dws var2 Note.txt function_2 Etc.doc etc flat structure for files and a workspace OOAPL
Concepts of the OO paradigm DOS V2.0 APL V8.0 Dir1 ns1 File2.ext var1 Abc.dws var2 Note.txt function_2 Folder2 namespace2 Etc.doc fna subf subns file1 fna filex opb Fich.log objxv Tree (nested) structure OOAPL
Concepts of the OO paradigm Each namespace is similar to a workspace - It contains functions, variables (including some system variables) - It can contain an entire application - It acts as a unit: it can be put as a single item on file - It is dynamic: it can be moved into to execute code, no need to disperse contents OOAPL
Concepts of the OO paradigm Each namespace is similar to a workspace Like a workspace, if you need to tweak one you take a copy and modify it: Workspace: Namespace: )LOAD myApp newApp<-[]NS []OR ’myApp’ A<-3 newApp.A<-3 )SAVE newApp OOAPL
Concepts of the OO paradigm Reusability This is one of the key concepts From a template you create a new version possibly modifying it 1 namespace many possibly different versions OOAPL
Concepts of the OO paradigm Classes Classes are very much like namespaces From a class you create a new version possibly modifying it 1 class many possibly different versions OOAPL
.Net and OO .Net is based on namespaces and supports many OO concepts. Dyalog APL is based on namespaces and supports many OO concepts. They go well hand in hand together. OOAPL
.Net and Dyalog This is not new! The ability to use .Net and create classes was already in V10 but it was more difficult to use. OOAPL
The Object Oriented paradigm Definitions OOAPL
OO fundamental concepts The Object Oriented paradigm is based on: • Classes & Interfaces • Instances • Members • Message passing • Inheritance • Encapsulation • Polymorphism OOAPL
OO fundamental concepts 1. Classes A class defines the abstract characteristics of some object, akin to a blueprint. It describes a collection of members (data and code) Classes can be nested OOAPL
OO fundamental concepts Classes ex: House blueprint 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
OO fundamental concepts Interfaces An interface describes how to communicate with an object. Ex: electrical system & electrical outlets 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 Yoke: implements maneuvering.Steer (move); ... Method Handle: implements maneuvering.Accellerate (pp); ... 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 (code) of the class if some sort of initialization is required. Upon destruction, the destructor is responsible for cleaning up. OOAPL
OO fundamental concepts 2. Instances New House (Blue): After the creation of the new House the Constructor paints it in blue 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. OOAPL
OO fundamental concepts Members These can reside inside the class if shared or in the instance otherwise. Ex: the .Net classDateTime has a member Now that does not require an instance to be called. e.g. DateTime.Now ⍝ straight from the class 2007/10/21 9:12:04 OOAPL
OO fundamental concepts Shared vs Instance Shared members remain in the class S1 S2 S3 I1 I2 I3 I4 Instance members are created in the instance S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 OOAPL
OO fundamental concepts Members: Methods Those can either reside in the class (sharedmethod) or in the instance (instancemethod). 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 (sharedfield) or in the instance (instancefield). 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 (sharedproperty) or in the instance (instanceproperty). 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 a procedural language (like Dyalog), 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 by avoiding to rewrite code and use what already exists. 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 from System.Object (the default) OOAPL
Great Dane: Dog Instances Dog: Animal Fido (Collie) Animal Classes Collie: Dog Fish: Animal Rex (Collie) OO fundamental concepts Inheritance: Based classes (reusability) A class can be based on another based class. See classCollie based on Dog based on Animal OOAPL
Horse Mule Donkey OO fundamental concepts Inheritance: multiple inheritance A class can be based on several other classes Here class Mule is made out of 2 different classes. OOAPL
OO fundamental concepts Inheritance: simulate multiple inheritance A class can implement several interfaces. Here class Penguin implements 2 different behaviours (interfaces): Fish (swim,eggs) • Penguin: • Swims • Flies not • 1 egg/yr • Croaks Bird (fly,eggs,sing) 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: a derived class inherits all the base members C1 memberA memberB C2: C1 memberC memberD OOAPL
OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB memberC C2: C1 memberC memberD OOAPL