(newbee) How to evaluate if class already exists?

Boris Gaertner Boris.Gaertner at gmx.net
Wed Nov 9 21:58:58 UTC 2005


From: "Marcus Pedersén" <marcus.pedersen at comhem.se> wrote:

> Hi!
> I want to evaluate if a class already exists but I can not figure out how
to do it.
> I get a string from a textfield in a GUI and wants to send a reply back
> (CustomMeny) telling the user if a class exists with that name.
> I have been looking around in Object and Class classes for a method
> but I can not find anything that works.
> What Im looking for:
> | myString |
> myString := 'text'.
> myString "isAClass" ( or something like that, that returns true
                                     > if there already is a class called
'text').

You can try this:

| myString |
myString := 'Text'.
(Smalltalk includesKey: myString asSymbol)
      and: [(Smalltalk at: myString asSymbol) isBehavior] .


Here you use
  1. the fact that all classes are registered in the global variable
      Smalltalk, which is a  SystemDictionary. The keys are Symbols.
      Note that Smalltalk contains also a few items that are not classes.
      (e.g. Display, Processor, Transcript, World, ActiveHand,
TextConstants, Smalltalk, ... )
  2. Classes answer  true to the message  isBehavior, all other
      objects answer  false.

Hope that helps
Boris




More information about the Squeak-dev mailing list