Art Eschenlauer suggested the following approach for providing a
user defined "instanceof()" method.  I like it a lot (though I'd
still prefer it be built into Unicon...) and it addresses a
number of things I didn't like about my solution.

The attached example is very close to what Art sent me, I've
made a few minor changes to suit my own quirky style.  (Among
this is naming the base class "Class", since I plan on having
most (all?) of the classes I write inherit from this Class
(which I expect to grow as I find other methods I'd like every
class to have - in fact, I'd welcome suggestions on methods
that should be included!).

In Art's solution, the method Type() needs to be overwritten by
every subclass (as shown in the test cases in the example).  This
is easy to do, however, and less work than what I had proposed doing
in the 'initially' clause of every subclass.

Here's the class Class (or is that "Here's the Class class"?):
--------------------------------------------------------------
#
# The Class class provides a foundation for other classes by
#    implementing methods that are generally useful.
#
class Class()

    # This method should be overridden by *every* subclass,
    #    to add to the traversal of the inheritance tree,
    #    as in:
    #
    #    method Type()
    #        suspend CLASSNAME | self$SUPERCLASS.Type()
    #    end
    #
    method Type()
        return "Class"   # Root of all inheritance
    end

    # Succeeds if superClassname is an ancestor of the current
    #   class (provided the current class has overridden the
    #   Type function as shown above).
    #
    method instanceof(superClassname)
        return superClassname == Type()
    end

    # Produces the classname for the current class
    #
    method className()
        return Type()
    end

end
-------------------------------------------------------------

and to show it works, some test cases:

-------------------------------------------------------------
#############################################################
# Test cases:
#############################################################

class A : Class ()
    method Type()
        suspend "A" | self$Class.Type()
    end
end

class B: A ()
    method Type()
        suspend "B" | self$A.Type()
    end
end

class C: Class ()
    method Type()
        suspend "C" | self$Class.Type()
    end
end

class D: A : C ()
    method Type()
        suspend "D" | self$A.Type() | self$C.Type()
    end
end

procedure main()
    a := A()
    b := B()
    c := C()
    d := D()

    checkInherit(a,"Class")
    checkInherit(a,"B")
    checkInherit(b,"A")
    checkInherit(b,"B")
    checkInherit(c,"Class")
    checkInherit(c,"A")
    checkInherit(d,"Class")
    checkInherit(d,"A")
    checkInherit(d,"B")
    checkInherit(d,"C")

end
-------------------------------------------------------------------
with the output:
-------------------------------------------------------------------
A is an instance of Class
A is NOT an instance of B
B is an instance of A
B is an instance of B
C is an instance of Class
C is NOT an instance of A
D is an instance of Class
D is an instance of A
D is NOT an instance of B
D is an instance of C
-------------------------------------------------------------------


-- 
Steve Wampler <[EMAIL PROTECTED]>
National Solar Observatory


-------------------------------------------------------
This SF.net email is sponsored by: Scholarships for Techies!
Can't afford IT training? All 2003 ictp students receive scholarships.
Get hands-on training in Microsoft, Cisco, Sun, Linux/UNIX, and more.
www.ictp.com/training/sourceforge.asp
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to