Sunday, January 21, 2024

Tcl 'oo::define self' explained



oo::class create c {

method a {} { puts "c a" }

self method b {} { puts "c b" }}




c create o




# this raises unknown method "a", because 'a' is a member of 'c' as a class, not as an object

# c a




# this works, because 'b' is a member of 'c' as an object, due to self, see man oo_define (^ *self)

c b




# this works, because 'a' is a member of the class 'c' that created o

o a




# this raises unknown method "b", because b is a member of c as an object, not as a class

# o b