Extending Keyed properties of a class.
Posted: Tue Jun 07, 2011 7:09 pm
I had planned to make a small presentation in different place in regards of this topic, but it did not go beyond draft.
Looking into .NET object model you may find syntax for collections, which is not obvious how to implement in Dyalog APL (it's indeed simple):
1.Object.Items[“myEntry”] - To get Instance of a single item
2.Object[“myEntry”] - This is optional and may be met for example in XmlDocument class
3.Object.Items[“myEntry”].SingleItemProperty - To call for single item of collection as object.
4.Object.Items.CollectionMethod - To call Method of collection.
So this is template for two classes to support above syntax in APL.
The main trick is to use two classes and Default keyword twice.
Now all 4 syntax constructs are working:
Looking into .NET object model you may find syntax for collections, which is not obvious how to implement in Dyalog APL (it's indeed simple):
1.Object.Items[“myEntry”] - To get Instance of a single item
2.Object[“myEntry”] - This is optional and may be met for example in XmlDocument class
3.Object.Items[“myEntry”].SingleItemProperty - To call for single item of collection as object.
4.Object.Items.CollectionMethod - To call Method of collection.
So this is template for two classes to support above syntax in APL.
Code: Select all
:Class CollectionProperty
:Field private Instance items←⎕new #.Collection
:Property Default Items
:access Public
∇ Z←get argument
Z←items
∇
:EndProperty
:EndClass
:Class Collection
:Field private Instance keys←''
:Field private Instance values←''
:Property Default Keyed Items
:access Private
∇ Z←get argument
Z←values[keys⍳(1⊃argument.Indexers)]
∇
:EndProperty
∇ Add(key value)
:Access Public
keys,←⊂key
values,←⊂value
∇
∇ Remove key
:Access Public
values keys←(⊂keys∊⊂key)/¨values keys
∇
∇ Clear
:Access Public
values keys←''
∇
:EndClass
The main trick is to use two classes and Default keyword twice.
Now all 4 syntax constructs are working:
Code: Select all
Object←⎕new CollectionProperty
Object.Items.Add'ABC' 1
Object.Items.Add'BCD' 2
Object.Items.Add'CDE' 3
Object.Items.Add'AnotherCollection' (⎕new CollectionProperty)
Object.Items[⊂'ABC']
1
Object[⊂'BCD']
2
Object.Items[⊂'AnotherCollection'].Items
#.CollectionProperty.[Collection]