[Xotcl] Re: [Xotcl] class methods
Artur Trzewik
mail@xdobry.de
Wed, 25 Apr 2001 20:51:56 +0200
Halo
The problem is more complicated than I have expected.
It is true, proc are not good for this purposes.
Classproc are very good idea but it would complicate the model.
It is really a program style not to use class methods.
If you come from C++ or Java (brr...) you will seldom use it.
Smalltalk-programmers will miss it (there are many smalltalk based languages)
A main usage is individual instance creation
(such as Factory Patterns in C++ or type based polymorphismus)
for example
MyClass createFromXML "...xml string"
MyClass createFromFooObject myObject
C++ programmers will write
class MyClass {
public:
MyClass(XMLNode node);
MyClass(FooClass myObject);
};
It is difficult to simulate it in xotcl by using C++ way
MyClass instproc init {aThing} {
if {[Object isobject $aThing] && [$aThing istype FooClass]} {
[self] initFromFooObject $aThing
} else {
.......
}
}
Perhaps Xotcl need inheritable class methods because it is not typed.
It can be very usefull to delegate some tasks to another class method
by implementing factories as class methods.
At now I implement databased persistence for Xotcl objects.
I use class methods for create instances, query database or code
mapping rules.
Class DBPersistence
Class MyClass -super DBPesistence
so I will do (100 is the database Id)
MyClass createFormId 100
or
MyClass getAllFor {name="destroy" and type not null}
I have noticed that Xotcl Philosophy is to use Metaclasses
Class DBPersistent -superclass Class
DBPersistent MyClass
until now I have not programed any Metaclasses (maybe it is time to do it)
> InheritClassProc instproc unknown {m args} {
> foreach c [[self] info heritage] {
> if {[info command ${c}::$m] != ""} {return [eval $c $m $args]}
> }
> next
> }
> Class instmixin InheritClassProc
The solution do not do what I expect
See this example. It demostrate how inheritable class method can be used
Class TkWidget
TkWidget proc testMe {} {
set toplevel [toplevel [Object autoname .toplevel]]
set inst [[self] create [Object autoname twidget] ${toplevel}.test]
pack $toplevel.test
return $inst
}
Class TkText -superclass TkWidget
TkText instproc init {win} {
text $win
}
Class TkEntry -superclass TkWidget
TkEntry instproc init {win} {
puts "window $win"
entry $win
}
TkEntry testMe
I will try to use metaclasses for my problems.
It can be good to take a look on other new objectbased languages
ruby, self, python.
How do these languages handle class methods?
=========================================
Artur Trzewik
http://www.xdobry.de
=========================================