View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] Bug when changing class

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Tue, 08 Feb 2005 13:25:14 +0100

>> There are two fixes for that, one simple (don't allow reclassing of
>> objects to classes)
>> or a more complex one (do more or less a recreate automatically when
>> this happens;
>> it tries to keep the object information as far as possible).
>
>
> I don't know the internals really of XOTcl but basically making an
> object a class would mean giving it the methods 'instproc' and 'new'
> and various other stuff, thus allowing an instance of it to be
> created. I guess it's some internal thing if it has to be recreated?
> In some sense it would be 'pure' if this was possible.

 From the C level, a class has to keep more information than an object.
The class structure is an
extension of an object, containing hash tables and a couple of pointers:

typedef struct XOTclClass {
  struct XOTclObject object;
  struct XOTclClasses* super;
  struct XOTclClasses* sub;
  short color;
  struct XOTclClasses* order;
  struct XOTclClass* parent;
  Tcl_HashTable instances;
  Tcl_Namespace *nsPtr;
  Tcl_Obj* parameters;
  XOTclClassOpt* opt;
  Tcl_HashTable *nonposArgsTable;
} XOTclClass;

therefore classes need somewhat more memory than objects, these
structures have to
be allocated and maintained, so everything is a little more costly than
it has to
for objects.

Providing means to make "obj class Class" and "cl class Object" working
is certainly
doable, but i am not sure that is worth the effort, since one can
achieve similar effects
already from the tcl level (see below).

> I wonder if this has any relevance to changing the meta-class of a class?

Check out the code below, it might be pretty close to what you might be
thinking about.

-gustaf
===============================================================
package req XOTcl
namespace import ::xotcl::*

    # we will defined our own Class and Object commands
    namespace forget Class Object

    # redefine Class and Object
    ::xotcl::Class create Class -superclass ::xotcl::Class
    Class instproc unknown {m args} {puts "[self] unknown called for $m
$args"}
    Class instproc init {} {
       set sc [my info superclass]
       if {$sc eq "::xotcl::Object"} {set sc [list]}
       my superclass [concat $sc Class]
    }
    ::xotcl::Class Object -superclass Class

    # create some object
    Object c

    # ... which is a class that can create an object ...
    c create d
    # ... and so on ...
    d create e

    # provide a method
    d instproc foo {} {puts [self proc]}
   
    # call the method from it's instance
    e foo

    # create another object which should use the methods provided
    # by some other objects....
    e create f -mixin d

    # call the method
    f foo