Extending Keyed properties of a class.

Writing and using Classes in Dyalog APL
Post Reply
alexbalako
Posts: 16
Joined: Mon Nov 30, 2009 8:58 pm

Extending Keyed properties of a class.

Post by alexbalako »

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.

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]
Erik.Friis
Posts: 66
Joined: Mon Apr 04, 2011 3:16 pm

Re: Extending Keyed properties of a class.

Post by Erik.Friis »

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.
alexbalako
Posts: 16
Joined: Mon Nov 30, 2009 8:58 pm

Re: Extending Keyed properties of a class.

Post by alexbalako »

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
Post Reply