Extending Keyed properties of a class.
3 posts
• Page 1 of 1
Extending Keyed properties of a class.
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]
- alexbalako
- Posts: 16
- Joined: Mon Nov 30, 2009 8:58 pm
Re: Extending Keyed properties of a class.
- Code: Select all
:Field private Instance items←⎕new #.Collection
I think you want to do this assignment in a constructor, otherwise each instance of CollectionProperty will share the same instance of Collection.
Last edited by Erik.Friis on Tue Jun 07, 2011 8:34 pm, edited 1 time in total.
- Erik.Friis
- Posts: 66
- Joined: Mon Apr 04, 2011 3:16 pm
Re: Extending Keyed properties of a class.
Erik, Thank you for pointing the issue.
- Code: Select all
:Class CollectionProperty
:Field private Instance items
∇ Constructor
:Implements Constructor
:Access Public
items←⎕NEW #.Collection
∇
:Property Default Items
:access Public
∇ Z←get argument
Z←items
∇
:EndProperty
:EndClass
- alexbalako
- Posts: 16
- Joined: Mon Nov 30, 2009 8:58 pm
3 posts
• Page 1 of 1
Return to Object Oriented Programming
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group