Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Orientation in Dyalog APL Implementation details V1.01.

Similar presentations


Presentation on theme: "Object Orientation in Dyalog APL Implementation details V1.01."— Presentation transcript:

1 Object Orientation in Dyalog APL Implementation details V1.01

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 Members' system functions/commands Members 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

10 Properties In 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

11 Properties: SIMPLE case When a property is set, its SET function is called. When retrieved, its GET function is called. The argument is a namespace containing the name of the property (Name) and its value (NewValue).

12 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

13 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

14 Default Properties There can be only one. If a default property exist, [indexing] can be done directly on the instance. Doing instance[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

15 Multiple Properties If 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

16 Triggers t← ⎕ NEW trigger1 ('Dan' 'Baronet') Full name is Dan Baronet t.FirstName←'Daniel' Full name is Daniel Baronet Implementation details

17 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

18 pet← ⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet.(Name Type) Rex canis familiaris Implementation details: :Base

19 ⎕ 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

20 Override and overridable These concepts allow a base class code to be overridden. The following slides will explain this in more details. Implementation details

21 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

22 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 ‘I am A’ M1 M2 ‘I am B’ Class A Class B

23 Based classes ⍝ M1 calls its own M2: ( ⎕ NEW B).M1 I am A

24 Based classes ⍝ M1 calls its own M2: ( ⎕ NEW B).M1 I am A M1 M2 ‘I am A’ There is no M1 in B so A’s (on which B is based) M1 is used M2 ‘I am B’ Class A Class B: A

25 Overridden classes ⍝ M1 calls its own M2 because ⍝ it has not been overridden: ( ⎕ NEW B).M1 I am A

26 Overridden classes ⍝ M1 calls its own M2: ( ⎕ NEW B).M1 I am A M1 M2 ‘I am A’ There is no M1 in B so A’s (on which B is based) M1 is used M2 ‘I am B’ Class A Class B: A

27 Overridden classes ⍝ M1 calls B’s M2 because ⍝ it has been overridden: ( ⎕ NEW B).M1 I am B

28 M2 ‘I am B’ Overridden classes ⍝ M1 calls B’s M2: ( ⎕ NEW B).M1 I am B M1 M2 ‘I am A’ Class A There is no M1 in B so A’s (on which B is based) M1 is used Class B: A

29 Implementation details Destructors They 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.

30 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’ 9 To find their sub-class they need to be enclosed: ⎕ NC ⊂ ‘myClass’ 9.4

31 ⎕ 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

32 ⎕ 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

33 ⎕ NC/ ⎕ NL extensions ⎕ NL and ⎕ NC also apply to variables and functions. The complete list is: 23/49 n.1 VariableTraditional fn/opNS created by ⎕ NS n.2 FieldD-fns or –opInstance (could be form) n.3 PropertyDerived/Primitive - n.4 --Class n.5 --Interface n.6 External/sharedExternal External Class n.7 --External Interface Implementation details: ⎕ NC/ ⎕ NL

34 To seeuse vars or fields:)vars fns or methods:)fns/methods classes:)classes interfaces:)interfaces events:)events properties:)props Implementation details: System commands

35 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

36 Types of Namespaces and their ⎕ NC The first two already existed in V10 Regular NS9.1 Forms9.2 Instances9.2 (NOT 9.3!) Classes9.4 Interfaces9.5 Implementation details

37 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)

38 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

39 Contents of namespaces t1.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

40 Contents of namespaces To get a distinct copy of t1.s1 we need to do abc← ⎕ NS ⎕ OR 't1.s1',abc. ⎕ NL 2 3 ⍝ everything is copied f1 v11 abc ⍝ even its name (S1: son of t1) abc≡t1.s1 ⍝ but they’re different 0 abc. ⎕ df ‘I am ABC’ ⋄ abc I am ABC

41 Contents of namespaces Outside world (e.g. WS) abc I am ABC ∇ f1... ∇ v11←’var’ abc. ⎕ NL 2 3 f1 v11

42 Contents of namespaces abc I am ABC ∇ f1... ∇ v11←’var’ ∇ tx←{, ⍵ } ∇ You can add to a namespace: tx←{, ⍵ } ‘abc’ ⎕ ns ‘tx’ and appears without ‘v11’ or going away.

43 Contents of namespaces abc #.[Namespace] ∇ tx←{, ⍵ } ∇ But if you use ← instead: tx←{, ⍵ } abc← ⎕ ns 'tx' appears in ‘abc’ alone and the display form is reset

44 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

45 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 forms F1.e2 F1 ⍝ each with its own name #.F1.e2

46 Contents of a form The form contains 2 objects: ⍴⎕ ←F1. ⎕ nl 2 3 9 b1 e2 2 ⎕ nc ‘F1.b1’ 9 F1. ⎕ nc ‘Caption’ 0 e2

47 Contents of a form A 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 f1 v1 Those are the items WE have added.

48 Contents of a form The form now contains 4 objects: ⍴⎕ ←F1. ⎕ nl 2 3 9 b1 e2 f1 v1 4 2 ⎕ nc ‘F1.b1’ 9 e2 ∇ f1 whatever ∇ v1←’some var’

49 Contents of a form BUT 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... YRange 69 F1. ⎕ nc ⊂ ’Caption’ ⍝ Caption is external ¯2.6 The new 65 items are the only visible members inside the form. They do NOT appear in ⎕ nl +2 3

50 Contents of a form The form contains 4 external objects: ⍴⎕ ←F1. ⎕ nl -2 3 4 ∇ f1 ∇ v1 e2 []WX=0

51 The form contains 69 total objects: ⍴⎕ ←F1. ⎕ nl -2 3 Accelerator... Caption... YRange 69 []WX=1 Contents of a form ∇ f1 ∇ v1 e2 ∇ f1 ∇ v1 e2 Accelerator Caption Hint Wait HelpButton Size Step Xrange Picture Posn PropList Range Redraw Border BCol AcceptFiles Active YRange VScroll Picture Posn MethodList MinButton Moveable CursorObj Data Detach

52 Contents of a form The form has 2 types of functions and variables: 1.Those internal and specific to its type 2.Those external, those we add The 1 st ones can be listed with a NEGATIVE ⎕ NL argument ONLY The 2 nd ones can be listed with a POSITIVE ⎕ NL argument. ⎕ NL- includes both types of elements.

53 Contents of namespaces All namespaces have a common structure They inherit system variables The system variables exist both internally AND externally

54 Contents of a form If we redefine the form all external objects disappear: +‘F1’ ⎕ wc’form’ #.F1 ⍴ F1. ⎕ nl 2 3 0 ⍴ F1. ⎕ nl-2 3 65 #.F1

55 Contents of a class Outside world (e.g. WS) ShrIns Classes are a different kind of namespace but they behave identically. ⍝ NO external object ⍴ ShrIns. ⎕ NL 2 3 0

56 Contents of a class Outside 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)

57 Contents of a class Outside 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←32 and they appear without anything else going away. v11←32 ∇ tx←{, ⍵ } ∇

58 Contents of a class Outside world (e.g. WS) ShrIns Master Share/instance ∇ MS1 ∇ FS1 (no value) ShrIns. ⎕ NC 2 3 tx v11 ShrIns. ⎕ nc-2 3 tx v11 FS1 MS1 v11←32 ∇ tx←{, ⍵ } ∇

59 Contents of an instance Outside world (e.g. WS) SI1 #.[ShrIns] ∇ MS1 ∇ FS1 (no value) SI1← ⎕ new ShrIns ⍴ SI1. ⎕ NL 3 2 0 SI1. ⎕ NL -3 2 FI1 FS1 MI1 MS1 The shared members are visible through the instance. ∇ MI1 ∇ FI1 (no value) Instance members Class members

60 Contents of an interface Outside world (e.g. WS) )ed ∘ maneuvering ⍴ maneuvering. ⎕ NC -2 3 0 maneuvering.v11←12 maneuvering. ⎕ NC -2 3 v11

61 Contents of an interface Outside 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

62 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

63 Contents of all namespaces This concept of internal/external names is important. Adding 1 external item to each namespace: ⍴ ¨all. ⎕ nl ⊂ -2 3 ⍝ will show 2 65 1 2 0 all.X← ⍳ 5 ⍴ ¨all. ⎕ nl ⊂ -2 3 3 66 2 3 1

64 :Include This statement is used when wanting to include CODE (not data) into a class. This may be because the code to include is common.

65 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

66 End of implementation details QUESTIONS ?

67

68 Complex Class This 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...

69 Complex Class This class also has 5 shared functions These perform the basic operations: + - × ÷ *

70 Complex Class This class also has 2 constructors and 2 private functions one of which is a trigger for when one of the fields is modified

71 Keyed component file The idea is to have a file whose components are accessed by key instead of a number. Sort of file.Read ‘key’

72 Keyed component file Since 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

73

74 .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

75 .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).

76 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')

77 ⎕ 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]...)

78 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).Name xl. ⎕ wx←3 ⍝ use V11 form xl.ActiveWorkbook.Sheets[ ⊂ ’Sheet2'].Index ⎕ WX

79 .Net and other goodies Example: standardize dialog boxes f1← ⎕ 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’...

80 .Net and other goodies.Net utilities There 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

81 .Net and other goodies.Net for others To allow other languages to use your code you should organize your classes in namespaces: WS NS1 NS2 ClassAClassBClassC ClassXClassY

82 .Net and other goodies.Net for others To allow other languages to use your code you declare how it works using the :Signature statement export it through the File/Export/.Net DLL menu item

83 :Signature statement This 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. at1is the 1 st argument type, if present name1 is the name of the 1 st 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 2 nd argument. If present, the comma MUST be there as it delimits groups,... same for the other arguments

84 .Net and other goodies.Net for others Here’s how a C# program would use this program: using System; using APLClasses; // the namespace in which our class public 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]); }

85 .Net and other goodies.Net for others Here’s how another APL program would use this program: ⎕ using←'APLclasses,c:\...\apl.dll' pr← ⎕ NEW Primitives pr.GenIndices 10 1 2 3 4 5 6 7 8 9 10

86 .Net and other goodies Web services can be written using APLScript, the scripting version of Dyalog APL, where logic is separated from page layout in ASMX files and ASPX files or in workspaces “behind”

87 .Net and other goodies Example of Web services written in APLScript    x            

88 .Net and other goodies Example of Web services written in APLScript

89 .Net and other goodies                         With ASPX files:

90 :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

91 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

92 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-t1 2007/10/03 17:12:01 d1<d2,d3 1 0

93 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

94 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. Conclusion

95 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 Careful!

96 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. Careful!

97 This doesn't mean you have to rewrite everything! But you are now competitive with the other guys. If someone tells you "you can't do OO or.Net" you can now say "We can!"


Download ppt "Object Orientation in Dyalog APL Implementation details V1.01."

Similar presentations


Ads by Google