quarta-feira, 16 de julho de 2008

The Quest for an ObjectScript Iterator (part 5)

In which we take the last post's iterator and simply wrap it inside a Caché Objects class.

Define a class User.MultiIterator



Class User.MultiIterator Extends %Persistent
{

Property i As %String;
Property j As %String;
Property k As %String;
Property val As %String;

Method startIterator()
{
set ..i=""
set ..j=""
set ..k=""
}

Method next()
{
if (..i="") {
set ..i=$order(^multi(..i))
quit ..next()
}
if (..j="") {
set ..j=$order(^multi(..i,..j))
quit ..next()
}
set ..k=$order(^multi(..i,..j,..k))
if ..k'="" {
set ..val = $get(^multi(..i,..j,..k))
quit 1
}
set ..j=$order(^multi(..i,..j))
if ..j'="" {
quit ..next()
}
set ..i=$order(^multi(..i))
if ..i'="" {
quit ..next()
}
quit 0
}

}



Using it (in another file) is now :



new it
set it= ##class(User.MultiIterator).%New()
do it.startIterator()
while it.next() {
write !,it.i_","_it.j_","_it.k_" : "_it.val
}



The logic is identical. The only difference, now I've made the i,j and k indices and the val properties of the iterator object rather than local variables passed by reference. It looks perhaps a little cleaner from the user's perspective. OTOH you have to create an extra class definition routine.

In the next post I think it's time to start thinking how we can add some filtering to the iterator.

Update : thanks to 80N for the correct. (Read comments)

3 comentários:

80n disse...

I think there's a typo in your startIterator method. The initialisation of the variables i, j and k should actually be initialising the properties ..i, ..j and ..k.

Oh, and User.MultiIterator doesn't need to be persistent, it can just extend %RegisteredObject.

Composing disse...

80n

Doh!

Quite right about the initializer. Dodgy copy'n'pasing from previous version.

Many thanks. I've corrected it.

%Persistent I seem to have got as a default from the Class Wizard. I admit I don't yet understand all the subtleties of this yet.

What does %Registered actually mean? (Apart from non-persistent, maybe).

80n disse...

Yeah, a registered class is one that doesn't have all the baggage necessary for persistence - no %OpenId(), no %Save(), etc.