Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Incorrect return value from self class

Closed / Fixed2704302fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 17 years ago
Descriptionplain2009-03-22 21:55:39
With active state tcl 8.6b1.1 on windows vista the following code:

package require TclOO
namespace path ::oo
class create A {
method change_class {new} {
oo::objdefine [self] class $new
list [self class] [info object class [self]]
}
}
class create B {}
set o [A new]
puts [$o change_class B]

outputs:

::A ::B

but should output

::B ::B
dkfplain2009-03-23 15:06:08
It's not clear at all to me what the correct response should be in that situation.
abstractsigplain2009-03-23 16:31:49
My logic is that the 'self class' return value should be consistent with the 'info object class' return value; a different return creates an ambiguity as to what the object's class is?

The 'oo::objdefine [self] class $new' definitely changes the class of self.  In this example the behaviour of self after 'oo::objdefine [self] class $new' is ::B not ::A.


package require TclOO
namespace path ::oo
class create A {
method change_class {new} {
oo::objdefine [self] class $new
my change_class foo
list [self class] [info object class [self]]
}
}
class create B {
method change_class {new} {
puts $new
}
}
set o [A new]
puts [$o change_class B]
dkfplain2009-03-24 17:26:48
Reading through the documentation again, I suppose that [self class] ought to fail if the method is not defined on a class. Will change to do that. (Will cause the problem code to throw an error, but that's probably best anyway.)
dkfplain2009-03-24 17:47:09
Also, there's arguably a case for having something in [self] to do [info object class [self]], but [self class] currently works with the method invocation and not the object.
abstractsigplain2009-03-25 02:05:35
Thanks for the comments.  I see now that my initial classification of this as a bug is a bit suspect.  

But given 'self class' works on the method invocation, so it is not clear to me what it should do as it depends on your timeline.  If you want to know the name of the class corresponding to the  'current' method for a recrusive call (i.e. looking forward in time) then you need [info object class [self]],  but if you want to trace back, then you need [self class].

Perhaps just a change to the documentation for 'self class' would be enough.  For example if it pointed out that it is possible that the results of self class and info object class [self] can be different.
dkfplain2009-07-24 15:23:36
allow_comments - 1

Put some more info into self.n
Log in (anonymous is fine) to add a comment.