Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | isabela-ackerley |
View: | 218 times |
Download: | 1 times |
Dyalog implemented Object Orientation (OO) in APL.
New territory was uncovered when venturing into OO.
In keeping with the dynamic nature of APL a lot of questions were raised.
And many were answered.
Implementation details
Classes' system functions/commands
All Interfaces, Classes & some Namespaces (ICN) come in scripted form.
• ⎕SRC: Just like NR returns the ⎕representation of a function in nested form, SRC returns the representation of an ICN in ⎕
nested form.
• ⎕FIX: Just like FX creates a function, FIX ⎕ ⎕creates an ICN.
• )ED ∘Interf /○Cl/ Ns⍟
Implementation details
Classes' system functions/commands
• monadic CLASS returns the tree of references⎕• dyadic CLASS casts an instance into another ⎕
ns• )CLASSES lists classes in the namespace• :Implements identifies what it implements• ⎕DF change the display form of a NS• )INTERFACES lists all interfaces in the
current namespace
Implementation details
Classes:A class can implement one or more
interfaces.An interface describes what it should
do.The class describes how it should be
done.
Implementation details
This maneuvering interface describes what has to be done with a machine:
)ed ∘maneuvering
It does NOT specify how it should be done.
Implementation details
This car class implements maneuvering:
VW← new Car⎕ VW.GasPedal 60 Going up to 60 (maneuvering class VW).Steer 140⎕Heading NW (maneuvering class VW).Accellerate 120⎕Going up to 120 VW.GasPedal 100 Going down to 100 VW.BreakPedal 1 Breaking...
Implementation details
This plane class implements maneuvering:
C172← new Plane⎕ C172.Stick 23 Heading 20 C172.Handle 200 Going up to 200 C172.Stick 23 11 Climbing, Heading 20 (maneuvering class C172).Steer 210⎕Heading 210 (maneuvering class C172).SlowDown 20⎕Flaps 20 degrees
Implementation details
Members' system functions/commandsMembers are Fields (variables), Methods
(functions), properties and other classes.
• ⎕INSTANCES lists the instances of a class• ⎕NEW creates an instance• Special Property cases• Triggers
Implementation details
PropertiesIn C# they are implemented as a pair of
functions. In APL the 'name←' case is handled naturally. It
is called the SIMPLE case and is the default.A second case where indexing is made using
any object (as opposed to numbers) is also handled easily. This is the KEYED case.
The array nature of APL introduced a notion of NUMBERED property. This is the third case.
Implementation details
Implementation detailsProperties: SIMPLE caseWhen a property is set, its SET function is called.When retrieved, its GETfunction is called.The argument is a namespacecontaining the name of theproperty (Name) and its value(NewValue).
Properties: KEYED case‘pa’ also contains the indices
of the property (Indexers).The property MUST be used
with [brackets] and their contents cannot be elided.
Their shape must conform to APL rules for assignment and result.
Implementation details
Properties: NUMBERED case
The set/get function call is made, ONCE per index.
‘pa’ also contains the index to deal with (Indexers).
It may be used with indices. The SHAPE function is used to check and generate indices.
Their shape must conform to APL rules for assignment and result.
Implementation details
Default PropertiesThere can be only one.If a default property exist, [indexing] can be
done directly on the instance. Doinginstance[index] is the same as instance.defProp[index]
If a property is the default then using “squad indexing” applies to the whole property
Implementation details
Multiple PropertiesIf several properties are similar the
definition statement may include several names separated by commas, like this:
:Property P1,P2,P3
The Get/Set functions’ argument will contain the name in ‘Name’ (as in arg.Name)
Implementation details
Triggers t← NEW trigger1 ('Dan' ⎕'Baronet')
Full name is Dan BaronetFull name is Dan Baronet t.FirstName←'Daniel'Full name is Daniel Baronet
Implementation details
Inheritance: new system functions/commands
• :Base used to call base class constructor• ⎕BASE used to call base class functions• override/able• Destructors are called when the instance is destroyed• ⎕NC/ NL have been extended⎕• ⎕THIS is the same as ( NS'').##⎕• :implements, :access• can be GUI based (ex: :Class F: ‘form’)• :include
Implementation details
pet← NEW Collie ‘Rex’⎕pet. nl -2⎕
Name Nlegs Sounds Typepet.(Name Type)
Rex canis familiaris
Implementation details: :Base
⎕Base is used to call base function like :base is used to call the constructor with an argument.This is used when the base function is shadowed by a class member. As in
s1← new square⎕s1.surface 20
400
Implementation details: Base⎕
Override and overridable
These concepts allow a base class code to be overridden.
The following slides will explain this in more details.
Implementation details
Non-Based classes
⍝ M1 calls its own M2:( NEW A).M1⎕
I am A
⍝ M1 calls its own M2:( NEW B).M1⎕
I am B
Non-Based classes
⍝ M1 calls its own M2:( NEW A).M1⎕
I am A
⍝ M1 calls its own M2:( NEW B).M1⎕
I am B
M1
M2 M2‘I am A’
M1
M2 M2‘I am B’
Class A Class B
Based classes
⍝ M1 calls its own M2:( NEW B).M1⎕
I am A
M1
M2 M2‘I am A’
There is no M1 in Bso A’s (on which B is based) M1 is used
M2‘I am B’
Class A Class B: A
Overridden classes
⍝ M1 calls its own M2:( NEW B).M1⎕
I am A
M1
M2 M2‘I am A’
There is no M1 in Bso A’s (on which B is based) M1 is used
M2‘I am B’
Class A Class B: A
M2‘I am B’
Overridden classes
⍝ M1 calls B’s M2:( NEW B).M1⎕
I am B
M1
M2 M2‘I am A’
Class A
There is no M1 in Bso A’s (on which B is based) M1 is used
Class B: A
Implementation detailsDestructorsThey are used to clean-up
after an instance is destroyed:
kk← new life ‘Kong'⎕ King Kong is born
)erase kk Kong is dead, long live the
king.
Implementation details: NC/ NL ⎕ ⎕⎕NC/ NL extensions⎕
Those 2 system functions have been extended to deal with the new objects.
Since classes and instances are a type of namespace their basic classification is 9:⎕NC ‘myClass’
9To find their sub-class they need to be enclosed:⎕NC ⊂‘myClass’
9.4
⎕NC/ NL extensions⎕
⎕NL has also been extended to report all objects of a given sub-class:⍴⍴⎕NL 9.4
2
Furthermore, if one of the argument to NL is ⎕negative the result is returned in vector format:⍴⍴⎕NL -9.4
1
Implementation details: NC/ NL⎕ ⎕
⎕NC/ NL extensions⎕
⎕NL and NC are symmetric such that⎕
n∊ NC NL n⎕ ⎕and
vn∊ NL NC¨vn vn is a list of names⎕ ⎕ ⍝
Implementation details: NC/ NL⎕ ⎕
⎕NC/ NL extensions⎕
⎕NL and NC also apply to variables and functions.⎕The complete list is:
2 3/4 9 n.1 Variable Traditional fn/op NS created by
NS ⎕ n.2 Field D-fns or –op Instance (could be form) n.3 Property Derived/Primitive - n.4 - - Class n.5 - - Interface n.6 External/shared External External Class
n.7 - - External Interface
Implementation details: NC/ NL⎕ ⎕
To see usevars or fields: )varsfns or methods:
)fns/methodsclasses: )classesinterfaces: )interfacesevents: )eventsproperties: )props
Implementation details: System commands
Implementation details: NC/ NL ⎕ ⎕⎕NC/ NL extensions⎕
⎕NL also includes instance members when used with a negative argument
instance. NL -2⎕ .2 report all fields⍝
Types of Namespaces and their NC⎕
The first two already existed in V10• Regular NS 9.1• Forms 9.2• Instances 9.2 (NOT 9.3!)• Classes 9.4• Interfaces 9.5
Implementation details
Implementation details: DF⎕
The default display form of a namespace is its path:←⎕ 't1.s1' NS ''⎕
#.t1.s1
We can change this using DF:⎕t1.s1. df '(S1: son of t1)'⎕t1.s1
(S1: son of t1)
Display form of namespaces
abc←t1.s1 even if assigned a new ⍝name:abc what does this look like?⍝
(S1: son of t1)abc≡t1.s1 it “remembers” its ⍝reference
1≡⍴⍬ abc it is still just a ref⍝
1⍴⍕abc its display has a length⍝
15
Contents of namespacest1.s1.v11←’var’ If we change the ⍝t1.s1. fx ‘f1’ ‘...’ contents of s1:⎕ ⍝,t1.s1. nl 2 3⎕
f1 v11,abc. nl 2 3⎕ abc will “see” it⍝
f1 v11⎕NC ’abc’ ‘t1’ ‘t1.s1’
9.1 9.1 9.1
Contents of namespacesTo get a distinct copy of t1.s1 we need to
doabc← NS OR 't1.s1'⎕ ⎕,abc. NL 2 3 everything is ⎕ ⍝ copied
f1 v11abc even its name⍝
(S1: son of t1)abc≡t1.s1 but they’re different⍝
0abc. df ‘I am ABC’ ⋄ abc⎕
I am ABC
Contents of namespacesabc
I am ABC
∇f1...∇
v11←’var’
∇tx←{,⍵} ∇
You can addto a namespace:
tx←{, }⍵ ‘abc’ ns ‘tx’⎕
and <tx> appearswithout ‘v11’ or <f1>going away.
Contents of namespacesabc
#.[Namespace]
∇tx←{,⍵} ∇
But if you use ← instead:
tx←{, }⍵ abc← ns 'tx'⎕
<tx> appears in‘abc’ alone andthe display form is reset
Display form of a Form
The default display form of a Form is its path, just like a regular namespace:+'F1' WC 'form'⎕
#.F1
We can change this using DF:⎕F1. df '<F1 is in great form>'⎕F1
<F1 is in great form>
Display form of a form ⍝ we can add items to it:
‘F1.b1’ wc ‘button’⎕‘e2’ F1. wc ‘edit’⎕⎕nc ‘F1’ ‘F1.b1’ ‘F1.e2’
9.2 9.2 9.2 ⍝ we have 3 instances of built-in APL
formsF1.e2 F1 each with its own name⍝
#.F1.e2 <F1 is in great form>
Contents of a formThe form contains 2
objects:⍴ ←⎕ F1. nl 2 3 9⎕
b1e22 2⎕nc ‘F1.b1’
9F1. nc ‘Caption’⎕
0
<F1 is in great form>
e2
Contents of a formA form is pretty much like a namespace.It can contain variables and functions.
F1.v1←’some var’F1. fx ‘f1’ ‘whatever’⎕F1. nl 2 3 these are POSITIVE ⎕ ⍝numbers
f1v1
Those are the items WE have added.
Contents of a formThe form now contains 4 objects:
⍴ ←⎕ F1. nl 2 3 9⎕b1e2f1v14 2⎕nc ‘F1.b1’
9
<F1 is in great form>
e2
∇ f1whatever∇
v1←’some var’
Contents of a formBUT a form also has properties and methods:
F1. wx←1 to be able to see them⎕ ⍝⍴ ←⎕ F1. nl -2 3 include internal items⎕ ⍝
Accelerator ... Caption ... YRange69
F1. nc ⎕ ⊂’Caption’ Caption is external⍝¯2.6The new 65 items are the only visible members
inside the form. They do NOT appear in nl +2 ⎕3
Contents of a formThe form contains 4
external objects:⍴ ←⎕ F1. nl -2 3⎕
4
<F1 is in great form>
∇f1∇
v1
e2
[]WX=0
The form contains 69 total objects:⍴ ←⎕ F1. nl -2 3⎕
Accelerator ... Caption ... YRange
69
[]WX=1
Contents of a form
<F1 is in great form>
∇f1∇
v1
e2
<F1 is in great form>
∇f1∇
v1
e2
AcceleratorCaption
HintWait
HelpButton
Size Step Xrange
Picture Posn PropList Range Redraw
Border BCol
AcceptFiles Active
YRange
VScroll
Picture
PosnMethodList MinButton Moveable
CursorObj Data Detach
Contents of a formThe form has 2 types of functions and
variables:
1. Those internal and specific to its type2. Those external, those we add
The 1st ones can be listed with a NEGATIVE NL argument ONLY⎕
The 2nd ones can be listed with a POSITIVE NL argument. ⎕
⎕NL- includes both types of elements.
Contents of namespaces
All namespaces have a common structure
• They inherit system variables• The system variables exist both internally
AND externally
Contents of a formIf we redefine the form all external objects
disappear:+‘F1’ wc’form’⎕
#.F1⍴F1. nl 2 3⎕
0 0⍴F1. nl-2 3⎕
65
#.F1
Contents of a classOutside world (e.g. WS) ShrIns
Classes are a different kind of namespace but they behave identically.
⍝ NO external object ⍴ShrIns. NL 2 3⎕0 0
Contents of a classOutside world (e.g. WS) ShrIns
Master Share/instance
∇MS1∇
FS1 (no value)
ShrIns. NL -2 3⎕FS1 MS1
The private members cannot be seen or accessed from outside.
∇MS0∇
FS0 (no value)
Contents of a classOutside world (e.g. WS) ShrIns
Master Share/instance
∇MS1∇
FS1 (no value)
You can add items “outside” a class: tx←{, }⍵ ‘ShrIns’ ns ‘tx’⎕ ShrIns.v11←32and they appearwithout anything elsegoing away.
v11←32
∇tx←{,⍵}∇
Contents of a classOutside world (e.g. WS) ShrIns
Master Share/instance
∇MS1∇
FS1 (no value)
ShrIns. NC 2 3⎕txv11 ShrIns. nc-2 3⎕ tx v11 FS1 MS1
v11←32
∇tx←{,⍵}∇
Contents of an instanceOutside world (e.g. WS) SI1
#.[ShrIns]
∇MS1∇
FS1(no value)
SI1← new ShrIns⎕ ⍴SI1. NL 3 2⎕0 0 SI1. NL -3 2⎕ FI1 FS1 MI1 MS1
The shared members are visible through the instance.
∇MI1∇
FI1(no value)
Instancemembers
Classmembers
Contents of an interfaceOutside world (e.g. WS)
)ed maneuvering∘
⍴maneuvering. NC -2 3⎕0 maneuvering.v11←12 maneuvering. NC -2 3⎕v11
Contents of an interfaceOutside world (e.g. WS)
The methods in the interface can only be seen when exposed through the dyadic CLASS ⎕system function:
VW← new Car⎕ (VW class maneuvering). nl -3⎕ ⎕Accellerate SlowDown Steer
Contents of all namespaces
This concept of internal/external names is important.
Assuming we have all types of namespaces as follows:ns← ns 'v1' 'f1'⎕fo← new ⎕ ⊂'form'in1← new cl1⎕all←ns,fo,cl1,in1,int1
then doing⍴¨all. nl⎕ ⊂-2 3 will show⍝
2 65 1 2 0
Contents of all namespacesThis concept of internal/external names is important.
Adding 1 external item to each namespace:
⍴¨all.⎕nl⊂-2 3 ⍝ will show2 65 1 2 0
all.X←⍳5⍴¨all.⎕nl⊂-2 3
3 66 2 3 1
:IncludeThis statement is
used when wanting to include CODE (not data) into a class.
This may be because the code to include is common.
Implementation details summary
• Many new system commands & fns• Interfaces show how it is done• Numbered Properties are implemented in
a special manner in APL• Triggers offer lightweight properties• ⎕NL/ NC have been extended⎕• The notion of external names is important• Inclusion of common code is possible
Complex ClassThis class is used to create
complex numbers. It can perform most simple
math operations on them.
This class has • 2 public instance fields• 1 public shared field• 2 private fields• 5 instance functions• ...
Complex ClassThis class also has • 2 constructors and• 2 private functionsone of which is a
trigger for when one of the fields is modified
Keyed component file
The idea is to have a file whose components are accessed by key instead of a number.
Sort offile.Read ‘key’
Keyed component fileSince it is made from a
component file it might be better to define a component file class.
Like this →This class ties a file,
creating it if necessary, and unties it (and destroys it if it was temporary) upon deleting the instance
.Net and other goodies
In addition to the classes which you can write in APL, version 11.0 allows you to work with:
• Dyalog GUI classes, including OLE Servers and and OLE Controls (ActiveX components)
• Microsoft.Net classes
.Net and other goodies
In version 11.0, these classes can be used in exactly the same way as instances of classes which have been implemented in APL.
You create instances of them with NEW, and ⎕you can manipulate the public properties, methods and events which they expose (but unlike APL classes, you cannot see or modify the implementation).
Forms and more via NEW⎕• It is possible to create forms using new:⎕
f1← NEW 'form‘ ((‘caption’ ‘MyForm’)('size' (12 16))⎕Note that the class name for built-in GUI objects is
provided as a string rather than as a reference to a name in the workspace.
• You can create OLE controls with new too:⎕xl← NEW 'OleClient‘ (⎕ ⊂'ClassName‘ 'Excel.Application')
⎕WX
This system function eXposes Window’s interface:
0= do NOT expose anything+1= expose names and report in NL ⎕
with negative argument, use V10 syntax where properties are treated as functions (e.g. Sheet.(Item 3)...)
+2= use new V11 syntax (e.g. Sheet[3]...)
xl← NEW 'OleClient‘ (⎕ ⊂'ClassName‘ 'Excel.Application')
⎕wx allows to use V10 syntax or the new V11 syntax:xl. wx←1 use V10 form⎕ ⍝xl. ActiveWorkbook.Sheets.(Item 1).Namexl. wx←3 use V11 form⎕ ⍝xl.ActiveWorkbook.Sheets[⊂’Sheet2'].Index
⎕WX
.Net and other goodiesExample:standardize
dialog boxesf1← NEW Dialog ('Hello ⎕
World' (50 250) )⍬
Note how the class is based on ‘Form’ (a string) and not Form (a name)
You can do fm← NEW ‘form’ ...⎕
.Net and other goodies.Net utilitiesThere is a lot of utilities out there that can be used in APL.For example, the Date/Time utilities found in the .Net
DateTime class.They are found in the System namespace.To be able to use the DateTime class you must indicate to APL
that it resides in the System namespace with USING:⎕
⎕USING←’System’now← new DateTime ts⎕ ⎕now let’s see its display form:⍝
2007/10/01 21:47:08⎕nc ‘now’
9
.Net and other goodies.Net for othersTo allow other languages to use your code you
should organize your classes in namespaces:
WS
NS1
NS2
ClassA ClassB ClassC
ClassX ClassY
.Net and other goodies.Net for othersTo allow other languages to use your
code you• declare how it works
using the :Signaturestatement
• export it through theFile/Export/.Net DLLmenu item
:Signature statementThis statement is used for the outside world where type
is important.It is made up of
:Signature [rt←] fnname [at1 [name1] [,ng2 [,...] ] ]rt← is the return type. If present it MUST be followed by ←fnname is the name of the function as seen from outside. It does
not have to match the function name. This MUST be there.at1 is the 1st argument type, if presentname1 is the name of the 1st argument as seen from outside. If
elided the DLL documentation will skip it and only show the type,ng2 is the same at/name group for the 2nd argument. If
present, the comma MUST be there as it delimits groups
,... same for the other arguments
.Net and other goodies.Net for othersHere’s how a C# program would use this
program:
using System;using APLClasses; // the namespace in which our
classpublic class MainClass { // is found public static void Main() { Primitives apl = new Primitives(); // the APL class int[] rslt = apl.GenIndices(10); // THE call for (int i=0;i<rslt.Length;i++) Console.WriteLine(rslt[i]);} }
.Net and other goodies.Net for othersHere’s how another APL program would use
this program:
⎕using←'APLclasses,c:\...\apl.dll'
pr← NEW Primitives⎕ pr.GenIndices 101 2 3 4 5 6 7 8 9 10
.Net and other goodiesWeb services can be written using APLScript, the scripting version of
Dyalog APL, where logic is separated from page layout
• in ASMX files and ASPX filesor• in workspaces “behind”
:Using:Using vs USING⎕
:Using X is akin to USING,←'X'⎕It appends X to USING except when no ⎕
argument is supplied where it means “←0⍴⊂ ”⍬
In a script/class it is necessary to determine the base class/interfaces
Namespaces in script form
)ed ⍟name will edit a new name using the same source format as classes and interfaces.
The source can then be retrieved using SRC and ⎕fixed using FIX.⎕
Namespaces can have a source
Overloading• Primitives cannot be overloaded by APL
classes but some .Net classes are.• Ex: DateTime objects can be compared using
=, >, etc. sorted using ‘grade up’ or added to TimeSpans using +.⎕using←'System'
d1← new DateTime ts assume it is 2007/10/10 ⎕ ⎕ ⍝19:12:01
d2← new DateTime (2007 10 16)⎕ t1← new TimeSpan (7 2 0 0) 7 days, 2 hours⎕ ⍝ +d3←d1-t12007/10/03 17:12:01 d1<d2,d31 0
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.
Conclusion