[REBOL] Re: Run Time Object Type Identification
From: joel:neely:fedex at: 2-Oct-2001 20:19
Hi, Phil,
Phil Hayes wrote:
> Is it possible to implement something like RTTI to identify
> object types without having to implement the 'type' var?
>
Not as far as I know. REBOL objects aren't "types" at all, just
namespaces that can have whatever content/behavior you want.
There's no real notion of class/subclass/inheritance/instance/type
that would support the concept of run-time-type-identification.
However ...
> E.g.
>
> obj1: make object! [ type: "obj1" ]
> obj2: make object! [ type: "obj2" ]
>
> o1: make obj1 []
> o2: make obj2 []
>
> switch/default o2/type [
> "obj1" [ print "obj1" ]
> "obj2" [ print "obj2" ]
> ] [
> print "Unknown"
> ]
>
... lots of OO folks aren't really too thrilled with RTTI, because
of the potential for "object abuse", such as writing code that
decides how to treat the object based on its type. The OO purist
would suggest that such behavior be defined within each object
so that it can "do the right thing" for itself. For example:
>> obj1: make object! [saytype: [print "type1"]]
>> obj2: make object! [saytype: [print "type2"]]
>> obj3: make object! [x: 10 y: 12 z: -7]
>> announce: func [ob [object!] /local announcer] [
[ either announcer: in ob 'saytype [
[ do announcer
[ ][
[ print "unknown"
[ ]
[ ]
>> announce obj1
type1
>> announce obj2
type2
>> announce obj3
unknown
or, for another variation,
>> announce-me: func [ob [object!]] [
[ if error? try [
[ do ob/saytype
[ ][
[ print "unknown"
[ ]
[ exit
[ ]
>> announce-me obj1
type1
>> announce-me obj2
type2
>> announce-me obj3
unknown
Of course, SAYTYPE could have been a function, but that's beside
the main point here, which is that either ANNOUNCE or ANNOUNCE-ME
is now independent of how many objects/types there are. It only
cares about whether the specified capability is defined for the
object at hand.
Would this type (pardon the pun ;-) of approach work for you?
-jn-
--
; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677
REBOL [] foreach [order string] sort/skip reduce [ true "!"
false head reverse "rekcah" none "REBOL " prin "Just " "another "
] 2 [prin string] print ""