Version 3 of Semantics of the oo::class command

Updated 2009-11-09 11:08:07 by dkf

The "oo::class create <name>" command will fail if the class <name> already exists. This is in contrast to the behaviour of the "proc" command, which overwrites an existing proc. The behavior of "oo::class" makes it awkward to use "package forget; package require" to refresh the contents of a package that defines a class. A package definition like:

namespace boo {
  oo::class create Boo {
    method boo {} { puts "ouch" }
  }
}

causes the following problem:

> package require boo

1.0

> package forget boo

> package require boo

can't create object "Boo": command already exists with that name

This can be avoided by splitting the definition of the class as in:

namespace boo {
  catch {oo::class create Boo}
  oo::define Boo {
    method boo {} { puts "ouch" }
  }
}

but it seems awkward to have to do this for every class that might be used in a package. What is the reasoning behind making it an error to re-"create" a class?

DKF: That's the way all objects are created (though with the “new” method it's not an issue anyway, since that never uses the same name as anything that exists). This matters for a class particularly because deleting a class also deletes the subclasses and instances of that class; I presume this is not something that you would like to have happen accidentally!

The real issue is that you're assuming that package forget unloads the package. It does not. It just forgets that the package is loaded; the package's contents are actually still present.