Object oriented programming in apl
Download
1 / 168

Object Oriented Programming in APL - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

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. This presentation is based on two things:. What is Object Orientation programming? How do I use it in APL?. Another tool.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Object Oriented Programming in APL' - camila


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Object oriented programming in apl

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
This presentation is based on two things:

  • What is Object Orientation programming?

  • How do I use it in APL?

OOAPL


Another tool
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
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
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 paradigm1
Concepts of the OO paradigm

The workspace

  • It contains everything needed to run an application

OOAPL


Concepts of the oo paradigm2
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 paradigm3
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 paradigm4
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 paradigm5
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 paradigm6
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 paradigm7
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 paradigm8
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 paradigm9
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 paradigm10
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 paradigm11
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 paradigm12
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 paradigm13
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 paradigm14
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 paradigm15
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 paradigm16
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 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
.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
The Object Oriented paradigm

Definitions

OOAPL


Oo fundamental concepts
OO fundamental concepts

The Object Oriented paradigm is based on:

  • Classes & Interfaces

  • Instances

  • Members

  • Message passing

  • Inheritance

  • Encapsulation

  • Polymorphism

OOAPL


Oo fundamental concepts1
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 concepts2
OO fundamental concepts

Classes

ex: House blueprint

OOAPL


Oo fundamental concepts3
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 concepts4
OO fundamental concepts

Interfaces

An interface describes how to communicate with an object.

Ex: electrical system & electrical outlets

OOAPL


Interfaces example
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 example1
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 example2
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 concepts5
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 concepts6
OO fundamental concepts

2. Instances

New House (Blue):

After the creation of the new House the

Constructor paints it in blue

OOAPL


Oo fundamental concepts7
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 concepts8
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 concepts9
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 concepts10
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 concepts11
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 concepts12
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 concepts13
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 concepts14
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 concepts15
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


Oo fundamental concepts16

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


Oo fundamental concepts17

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 concepts18
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 concepts19
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 concepts20
OO fundamental concepts

Inheritance: a derived class inherits all the base members

C1

memberA

memberB

C2: C1

memberC

memberD

OOAPL


Oo fundamental concepts21
OO fundamental concepts

Inheritance: a derived class inherits all the base members

C1

memberA

memberB

memberC

C2: C1

memberC

memberD

OOAPL


Oo fundamental concepts22
OO fundamental concepts

Inheritance

Initialization is performed by the constructor.

If a class is derived, the base class' constructor will also be called if there is one.

OOAPL


Oo fundamental concepts23
OO fundamental concepts

Inheritance

Some classes may be sealed to prevent other classes from inheriting them.

OOAPL


Override concept
Override concept

Class B

Class A

M1

M2

M1

M2

M2

‘I am B’

M2

‘I am A’

// M1 calls B’s M2:

(NEW B).M1

I am B

// M1 calls A’s M2:

(NEW A).M1

I am A

OOAPL


Based classes
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 A’s M2:

(NEW B).M1

I am A

// M1 calls A’s M2:

(NEW A).M1

I am A

OOAPL


Based classes1
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 A’s M2:

(NEW B).M1

I am A

// M1 calls A’s M2:

(NEW A).M1

I am A

A:M2 does not allow to be overridden

OOAPL


Based classes2
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: Overridable

‘I am A’

// M1 calls B’s M2:

(NEW B).M1

I am B

// M1 calls A’s M2:

(NEW A).M1

I am A

A:M2 does allow to be overridden AND B wants to override it

OOAPL


Oo fundamental concepts24
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
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 access
Encapsulation (access)

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 concepts25
OO fundamental concepts

Encapsulation: private

A private member cannot be seen from anywhere. Methods can only access it from inside the class.

Nested or derived objects can't see it either.

M is only

seen in the

white area

sub1

sub2

OOAPL


Oo fundamental concepts26
OO fundamental concepts

Encapsulation: private

Analogy names are private in a D-fn:

a←{z←99 ⋄ b ⍵}

b←{z+⍵}

a 2

VALUE ERROR

b[0] b←{z+⍵}

OOAPL


Oo fundamental concepts27
OO fundamental concepts

Encapsulation: protected

A protected member can only be seen from within the instance of from nested or derived objects.

M is only

seen in the

white area

sub1

sub2

OOAPL


Oo fundamental concepts28
OO fundamental concepts

Encapsulation: protected

Analogy names are protected in a T-function:

∇r←a x;z

[1] z←99 ⋄ r←b x

∇r←b x

[1] r←z+x

a 2

101

OOAPL


Oo fundamental concepts29
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 concepts30
OO fundamental concepts

Encapsulation: friend

A class declared friend can see the offerer’s private and public members.

Class B

Class A

Private Apv

Public Apu

Private Bpv

Public Bpu

Friend A

Here A sees B’s members

as its own members

OOAPL


Oo fundamental concepts31
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 concepts32
OO fundamental concepts

Polymorphism example

Foo (integer arg)

{

// fn called with

// an integer arg

Print 42 //“Int arg”

}

Foo (string arg)

{

// fn called with

// a string arg

Print “String arg”

}

Same function name, different code

OOAPL


Oo fundamental concepts33
OO fundamental concepts

Polymorphism

A program that accepts multiple definitions

is said to be overloaded.

Basic language example: + is used to add and catenate

Print 2+2

4

Print ‘aaa’+’bbb’

aaabbb

OOAPL


Oo fundamental concepts34
OO fundamental concepts

More concepts

There are many more concepts.

Those presented here are the most common ones.

For example, Casting is not an OO concept but it is often used.

OOAPL


Oo c oncepts summary
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 and/or specific access

OOAPL


End of oo basics
End of OO basics

QUESTIONS

?

OOAPL


The

Dyalog APL

way

OOAPL


Implementation
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

OOAPL


Implementation1
Implementation

Let’s see how Dyalog APL does this:

  • Classes and interfaces

  • Instances

  • Members

  • Message passing

  • Inheritance

  • Encapsulation

  • Polymorphism

OOAPL


Implementation classes
Implementation: classes

A class can be defined using the )EDitor as in

)ED ○ MyClass

Upon exiting, MyClass will be defined in the current namespace (e.g. #)

OOAPL


Implementation classes1
Implementation: classes

In the workspace MyClass appears as a namespace:

⎕NC ‘MyClass’

9

)CLASSES

MyClass

OOAPL


Implementation members
Implementation: members

The members in a class are:

  • Field: same as a variable

  • Method: same as a function

  • Property: looks and feels like a variable but implemented as a (pair of) functions

    All can be either public or private (the default)

OOAPL


Implementation members1
Implementation: members

The class to the right shows a few fields (vars) and their access.

The access is the same for methods (fns) and properties.

OOAPL


Implementation members2
Implementation: members

The class to the right shows a few methods (fns) and their access.

<mon1> is used as a constructor. It MUST be public.

OOAPL


Implementation members3
Implementation: members

The class to the right shows 2 properties and their access.

The 1st one is private and readonly (it has no set fn)

The 2nd one is public read/write

OOAPL


Implementation instances
Implementation: instances

Classes are like blueprints: they define how things should be.

⎕NEW is a new system function that creates an object using a class as an instance.

Whatever is in the class will appear in the instance as a copy, not a reference

OOAPL


Implementation new
Implementation: ⎕NEW

⎕NEW accepts a one or two elements argument.

The 1st one is the class (not in quotes)

The 2nd, if present, is what to start/initialize the instance with. It can only be supplied if the class allows it.

OOAPL


Implementation new1
Implementation: ⎕NEW

Example:

myInstance← ⎕NEW MyClass

The display form includes [brackets] by default

myInstance

#.[MyClass]

It is another type of namespace

⎕NC ‘myInstance’

9

OOAPL


Implementation df
Implementation: ⎕DF

⎕DF allows to change the default Display Form of a namespace.

Example:

⎕←obj←⎕NEW MyClass

#.[MyClass]

obj.⎕DF ‘OO class’

obj

OO class

OOAPL


Implementation constructors
Implementation: constructors

Classes may require something to initialize the instance. Ex:

)ED ○ Animal

  • Here an animal must

  • have at least two

  • characteristics:

  • # of legs

  • the sounds it makes

OOAPL


Implementation constructors1
Implementation: constructors

Several instances can be made up of the same Animal class:

After running this

function in a clean

space we have:

)obs

fish monkey snake tiger Animal

⎕NC ‘animals’ ⍝ this is a variable holding 4 instances

2

animals.Nlegs ⍝ how many legs for each animal?

4 0 0 2

OOAPL


Implementation message passing
Implementation: message passing

In APL this merely consists in calling the methods directly either from the instance or from the class if shared, e.g.

Myclass.sharedFn args

or

myInstance.Fn args ⍝ args if required

OOAPL


Implementation inheritance

Great Dane: Dog

Instances

Dog: Animal

Fido (Collie)

Animal

Classes

Collie: Dog

Fish: Animal

Rex (Collie)

Implementation: inheritance

Inheritance: Based classes

A class can be based on another based class. See classCollie based on Dog based on Animal

OOAPL


Implementation inheritance1

Bird

Birdy (Bird)

Implementation: inheritance

Example: a bird’s generic description is ‘plain Bird’

Birdy←⎕NEW Bird

Birdy.Desc

plain Bird

When Birdy is created, Bird’s constructor is called

OOAPL


Implementation inheritance2
Implementation: inheritance

If a base class has a constructor it must be called.

Base constructors are called when the :Implements constructor statement is reached.

If several classes are based one on another their constructors’ call are made in the same fashion.

OOAPL


Implementation inheritance3

Birdy (Bird)

Bird

Parrot: Bird

Coco (Parrot)

Implementation: inheritance

Example: a parrot is a bird

Coco←⎕NEW Parrot

Coco.Desc

Not a plain Bird, a Parrot

OOAPL


Implementation inheritance4
Implementation: inheritance

We can create classes and instances at all levels

Birdy (Bird)

Bird

Coco (Parrot)

Classes

Instances

Parrot: Bird

Tweety (Macaw)

Macaw: Parrot

OOAPL


Implementation inheritance5
Implementation: inheritance

Inheritance: constructors with arguments

Using the animal kingdom as example.

Big Dane: Dog

Great Dane: Dog

Instances

Dog: Animal

Fido (Collie)

Animal

Classes

Collie: Dog

Fish: Animal

Rex (Collie)

OOAPL


Implementation inheritance6
Implementation: inheritance

pet←⎕NEW Collie ‘Rex’

pet.⎕nl -2

Name Nlegs Sounds Type

pet.(Name Type)

Rex canis familiaris

OOAPL


Implementation inheritance7
Implementation: inheritance

Let’s go over this again slowly…

pet←⎕NEW Collie ‘Rex’

pet.⎕nl -2

Name Nlegs Sounds Type

pet.(Name Type)

Rex canis familiaris

OOAPL


Implementation inheritance8
Implementation: inheritance

pet←⎕NEW Collie ‘Rex’

pet.⎕nl -2

Name Nlegs Sounds Type

pet.(Name Type)

Rex canis familiaris

OOAPL


Implementation encapsulation
Implementation: Encapsulation

Conceals the exact details of how a particular class works from objects that use its code.

To allow access to a member there are various access level definitions.

OOAPL


Implementation encapsulation1
Implementation: Encapsulation

To expose a member you must make it public

For a property or a method (function) you must use the :Access public Statement

For a field (var) you must use this statement:

:Fieldpublic MyField

OOAPL


Implementation encapsulation2
Implementation: Encapsulation

The class to the right shows a few fields (vars) and their access.

The access is the same for methods (fns) and properties.

OOAPL


Implementation encapsulation3
Implementation: Encapsulation

Shared vs instance

Each instance gets a copy of each instance member.

If a member is shared it is kept in the class for all instances to see (and use)

OOAPL


Implementation encapsulation4
Implementation: Encapsulation

Shared vs instance

Example: keepCount

Here each instance has its own private ID and public name

The class keeps track of the number of instances created privately.

OOAPL


Implementation encapsulation5
Implementation: Encapsulation

Shared vs instance

'Count' remains

in the class

keepCount:

Count←0,1,…

MyID MyName

Instance members

are created in the

instance

Count

MyID=3

MyName=XYZ3

Count

MyID=1

MyName=XYZ1

Count

MyID=2

MyName=XYZ2

OOAPL


Implementation encapsulation6
Implementation: Encapsulation

Shared vs instance

in1←⎕new keepCount

I am XYZ1

in2←⎕new keepCount

I am XYZ2

in3←⎕new keepCount

I am XYZ3

keepCount.Count

3

keepCount.⎕nl-2

Count

in2.⎕nl-2

Count MyName

in1.Count

3

OOAPL


Implementation polymorphism
Implementation: Polymorphism

In APL a function CANNOT have different definitions using a single name.

OTOH, a constructor MAY have several definitions.

Since APL is typeless the only way to tell which function to use is by the NUMBER of arguments, from 0 (niladic) to any number.

OOAPL


Implementation polymorphism1
Implementation: Polymorphism

Example:

Depending on the number of arguments, the proper fn will be used.

:Class manyCtors

∇ general x

⍝ This ctor any arg

⍝ It can be anything

:Implements constructor

:Access public

∇ nil

⍝ This ctor takes no arg

:Implements constructor

:Access public

∇ mon1(a1)

⍝ This ctor takes 1 arg

⍝ It MUST be a vector

:Implements constructor

:Access public

∇ mon5(a1 a2 a3 a4 a5)

⍝ This ctor takes 5 arg

⍝ It MUST be exactly 5

:Implements constructor

:Access public

:EndClass

OOAPL


Implementation summary
Implementation summary

  • Dyalog follows C# rules closely

  • )ED to edit classes

  • classes are much like namespaces

  • their members (vars & fns) can all be accessed using public access

  • ⎕NEW makes an instance

  • classes can be based on another one

  • they use constructors to initialize them

OOAPL


End of implementation
End of implementation

There are MANY other things related to OO that have been implemented but are out of the scope of this presentation.

To find more visit Dyalog’s website at www.dyalog.com and grab the free documentation.

OOAPL


End of implementation1
End of implementation

QUESTIONS

?

OOAPL


Exercises
Exercises

Choice of exercises:

  • Simple numbers

  • Complex numbers

  • Parser

  • Pre formatted forms

  • Graphical objects

  • Telnet client

The end

OOAPL


Exercises preformatted form
Exercises – Preformatted form

Deriving from Dyalog GUI classes.

Besides classes you can write classes derived from

  • Forms

  • OLE servers/controls

  • .Net classes

OOAPL


Exercises preformatted form1
Exercises – Preformatted form

Ex:

f1←⎕new 'form' (('caption' 'zzz')('size'(100 200)))

XL←⎕new 'oleclient' (⊂'classname' 'Excel.application')

⎕using←''

present←System.DateTime.Now

OOAPL


Exercises preformatted form2
Exercises – Preformatted form

We need to write a class that will create a form, just like

f1←⎕new 'form' (('caption' 'zzz')('size'(100 200)))

But with 2 buttons already setup for SAVE and QUIT so we can do

f1←⎕new myForm ((…))

OOAPL



Exercises preformatted form4
Exercises – Preformatted form

f1←⎕new Dialog(‘pref form' (100 500) ⍬)

OOAPL


Exercises preformatted form5
Exercises – Preformatted form

This class creates an edit object:

OOAPL


Exercises preformatted form6
Exercises – Preformatted form

f1←⎕new Dialog('pref form' (200 300) ⍬)

f1.fn←f1.⎕new EditField ('First name:' '' (10 60) (⍬ 100) ⍬)

f1.ln←f1.⎕new EditField ('Last name:' '' (38 60) (⍬ 100) ⍬)

f1.ad←f1.⎕new EditField ('Address:' '' (66 60) (90 230) (⊂'style' 'multi'))

f1.(fn ln ad).Text←'Jeremy' 'Cricket'('TopFlower' 'last field')

OOAPL


Exercises1
Exercises

Improvements:

  • Use an interface

    Back to exercise choices

OOAPL


Exercises complex numbers
Exercises – Complex numbers

Create a class to do complex math.

A rational or “normal number” is a decimal number that can be viewed on the line of “real” numbers:

-10 -5 0 +5.3 +10

OOAPL


Exercises2
Exercises

The number 3J4

A complex number

is a number in the

complex plane:

2 rational numbers

are used to

represent it.

+6

+4

+2

-4 -2 0 +2 +4 +6

OOAPL


Exercises3
Exercises

Create a class to do complex math.

The class will contain 2 real numbers and 3 methods to perform Add, Multiply and return the Reciprocal of a complex number.

OOAPL


Exercises4
Exercises

Create a class to do complex math.

The methods to perform Add and Multiply will be shared (in the class) methods and the Reciprocal will be an instance method.

OOAPL


Exercises5
Exercises

Complex math definition:

C = a + bi, where i = ¯1 * 0.5

Engineers use the J notation, like this:

3j4 to denote the complex number with a real value of 3 and an imaginary value of 4.

OOAPL


Exercises6
Exercises

Complex math definition:

Addition of 2 complex numbers

C1 + C2 = (a1+a2) + (b1+b2) i

OOAPL


Exercises7
Exercises

Complex math definition:

Multiplication of 2 complex numbers

C1 a1b1i

x

C2 a2b2i

=(a1×a2) + (b1i × b2i) + (a1×b2i) + (b1i×a2)

=((a1×a2) - (b1 × b2)) + ((a1×b2) + (b1×a2))i

OOAPL


Exercises8
Exercises

Complex math definition:

Reciprocal of a complex number is 1÷C

such that C × R = 1 = 1 + 0 i

real = ((a1×a2) - (b1 × b2)) = 1

img = ((a1×b2) + (b1×a2)) i= 0

Solving:

Ra = Ca÷((Ca×Ca)-(Cb×Cb))

Rb = -Cb÷((Ca×Ca)-(Cb×Cb))

OOAPL


Exercises9
Exercises

The first thing to do:

)ED ○ complex

OOAPL


Exercises10
Exercises

Enter the fields:

OOAPL


Exercises11
Exercises

Enter the constructor, it takes 2 numbers as arguments, it merely sets the 2 fields:

OOAPL


Exercises12
Exercises

Test

c1←⎕NEW complex (3,4)

c1.real

To ease creation of complex numbers:

j←{⎕new complex (⍺ ⍵)}

c2←5 j 12

c2.img

OOAPL


Exercises13
Exercises

Test

Typing c1 does not provide useful information.

It would be nice if we could find what the numbers are without having to use

c1.(real img)

OOAPL


Display form
Display form

  • The display form can be improved

  • We can use ⎕DF to show a different string, e.g. 3J4

  • Try c1. ⎕DF '3J4' (or whatever)

  • Try including the setting in the constructor

OOAPL


Display form1
Display form

  • assignment does not change the display

    Try c1.img←22 and verify that ⎕←c1 has the same display form

  • use properties to change that

  • create 2 properties

  • use ⎕DF to change the display in the <set> function

OOAPL


Exercises14
Exercises

Enter the methods, first, <plus>:

OOAPL


Exercises15
Exercises

Test <plus>:

cs←c1 complex.plus c2

cs.real ?

OOAPL


Exercises16
Exercises

Enter the methods, 2nd, <times>:

OOAPL


Exercises17
Exercises

Test <times>:

ct←c1 complex.times c2

ct.real ?

OOAPL


Exercises18
Exercises

Enter the methods, 3rd, <reciprocal>:

OOAPL


Exercises19
Exercises

Try it!

complex.plus/4⍴c1 ⍝ if funny display, use

dj←{⍵.(real img)}

dj complex.plus/4⍴c1 ⍝ 12 16?

dj complex.times\3⍴c1 ⍝ 3 4 ¯7 24...

dj complex.plus/c1 complex.times¨4⍴c1

OOAPL


Exercises20
Exercises

Try it!

dj c1.reciprocal

⍝ c1 ÷ c1 or c1 × ÷c1:

dj c1 complex.times c1.reciprocal

dj c2 complex.times c1.reciprocal

2.52 0.64?

OOAPL


Exercises21
Exercises

Improvements:

  • other functions (magnitude?)

  • accept a single (real) number

    Back to exercise choices

OOAPL


Exercises22
Exercises

Create a class to do simple math with 2 integers representing fractions.

Using a Numerator & a Denominator

Ex.: 3 / 7th

OOAPL


Exercises23
Exercises

Create a class to do simple math.

The class will contain 2 integer numbers and 3 methods to perform Add, Multiply and return the Reciprocal of a pair of simple numbers representing a fraction.

OOAPL


Exercises24
Exercises

Create a class to do simple math.

The methods to perform Add and Multiply will be shared (in the class) methods and the Reciprocal will be an instance method.

OOAPL


Exercises25
Exercises

Simple math definition:

  • The 1st number will be called N

  • The 2nd number will be called D

    They will be stored in a vector (2 elem)

    There will be two properties to access them individually.

OOAPL


Exercises26
Exercises

Simple math definition:

Addition of 2 simple numbers (fractions)

S1 + S2 = (n1 x d2 + n2 x d1) , (d1 x d2)

Multiplication of 2 simple numbers

S1 x S2 = (n1 x n2) , (d1 x d2)

Reciprocal of a simple number

÷S1 = (d1) , (n1)

OOAPL


Exercises27
Exercises

The first thing to do:

)ED ○ simple

OOAPL


Exercises28
Exercises

Enter the field:

OOAPL


Exercises29
Exercises

Enter the constructor, it takes 2 numbers as arguments, it merely sets the field:

OOAPL


Exercises30
Exercises

At this point the class does not do anything.

‘Numbers’ is private and cannot be accessed from outside.

Let’s create properties to access it.

OOAPL


Exercises31
Exercises

Enter the properties, they take 1 number to set a value in the field:

N

OOAPL


Exercises32
Exercises

Test

s1←⎕NEW simple(3,4)

s1.N

To ease creation of simple numbers:

s←{⎕new simple (⍺ ⍵)}

s2←5 s 12

s2.N

OOAPL


Exercises

Now enter the second property…

OOAPL


Exercises33
Exercises

Test

Typing s1 does not provide useful information.

It would be nice if we could find what the numbers are without having to use

s1.(N D)

OOAPL


Display form2
Display form

  • The display form can be improved

  • We can use ⎕DF to show a different string, e.g. 3/4

  • Try s1. ⎕DF '3/4'

  • Try including the setting in the constructor

OOAPL


Display form3
Display form

  • assignment does not change the display

    Try s1.N←22 and verify that ⎕←s1 has the same display form it had before

  • use the properties’ code to change that

  • use ⎕DF to change the display in the <set> function

OOAPL


Exercises34
Exercises

Enter the methods, first, <plus>:

OOAPL


Exercises35
Exercises

Test <plus>:

cs←s1 simple.plus s2

cs.N ?

OOAPL


Exercises36
Exercises

Enter the 2nd method, <times>:

OOAPL


Exercises37
Exercises

Test <times>:

ct←s1 simple.times s2

ct.N ?

OOAPL


Exercises38
Exercises

Enter the methods, 3rd, <reciprocal>:

OOAPL


Exercises39
Exercises

Try it!

simple.plus/4⍴s1 ⍝ if funny display, use

dj←{⍵.(N ‘/’ D)}

dj simple.plus/4⍴s1 ⍝ 22/1?

dj simple.times\3⍴s1 ⍝ 11/2 121/4…

dj simple.plus/s1 simple.times¨4⍴s1

OOAPL


Exercises40
Exercises

Try it!

dj s1.reciprocal

⍝ s1 ÷ s1 or s1 × ÷s1:

dj s1 simple.times s1.reciprocal

dj s2 simple.times s1.reciprocal

5/66?

OOAPL


Exercises41
Exercises

Improvements:

  • other functions (square, abs?)

  • accept a single (real) number

  • make this into a complex number class

    Back to exercise choices

OOAPL


Conclusion
Conclusion

There are many advantages to using OO languages over procedural languages:

  • ease of modification

  • reduced maintenance costs

  • easier to model

  • can speed up development time

  • etc.

OOAPL


Conclusion1
Conclusion

OO languages make abstraction easy

They encourage architectures with elaborate layers.

They can be good when the problem domain is truly complex and demands a lot of abstraction.

OOAPL


Careful
Careful!

  • All OO languages show some tendency to suck programmers into the trap of excessive layering

  • Object frameworks and object browsers are not a substitute for good design or documentation

OOAPL


Careful1
Careful!

  • Too many layers destroy transparency: It becomes too difficult to see down through them and mentally model what the code is actually doing.

  • The Rules of Simplicity, Clarity, and Transparency can get violated wholesale, and the result is code full of obscure bugs and continuing maintenance problems.

OOAPL


Careful2
Careful!

Buyer beware!

OOAPL


ad