Search code examples
smalltalkpharo

Checking for the presence of a class


I would like to check if a particular class has been loaded.

Smalltalk at: #TabularXSLXExport  ifNone: [ ]

This does not lead to a result in Pharo. How do I do this?


Solution

  • I think the method you're looking for is #at:ifAbsent: (not #at:ifNone:).

    So, inspecting the result of

    Smalltalk at: #String ifAbsent: [ nil ]
    

    will let you inspect the String class, while

    Smalltalk at: #Strign ifAbsent: [ nil ]
    

    will open an inspector on nil (note that "Strign" is a deliberate misspelling of "String" so that the lookup fails).

    Edit: As Max Leske points out in the comments, #hasClassNamed: is a more suitable method if you're just trying to determine whether the class exists, and not interested in the class itself being returned.