A special search algorithm is used to find an instance variable in a tree of class definitions. Like a breadth-first search, the search algorithm is biased towards declarations in the shallow part of the tree. However, like a depth-first search, the search algorithm is independent of the tree's shape beyond the immediate superclass. The algorithm is a modified pre-order search, where the immediate children are checked before recurring on subtrees.
The algorithm first checks each immediate superclass, left-to-right, for the variable declaration. If a declaration is found, the search ends immediately. Otherwise, for each superclass, left-to-right, the entire super-tree is checked. Each super-tree is checked using this algorithm recursively. If a declaration is found in anywhere in a super-tree, then the search ends.
The following Scheme procedure illustrates the search algorithm:
(define find-ivar
(lambda (base-class ivar-name)
(letrec ([find
(lambda (class)
(let ([supers (get-superclass-list class)])
(or (ormap (lambda (super)
(if (has-immediate-ivar? super ivar-name)
(get-immediate-ivar super ivar-name)
#f))
supers)
(ormap find supers))))])
(if (has-immediate-ivar? base-class ivar-name)
(get-immediate-ivar base-class ivar-name)
(find base-class)))))