Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 357
z-- Avatar asked Oct 26 '25 08:10

z--


2 Answers

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.

like image 56
Amos M. Carpenter Avatar answered Oct 29 '25 01:10

Amos M. Carpenter


You might want to do an extension method for this, or use a pragma to collect all 'exports', or register this functionality. The world menu uses a pragma <worldMenu>.Catalog browser uses it like this:

CatalogBrowser class>>menuCommandOn: aBuilder 
"Add a custom menu item to the world menu"  
<worldMenu> 

(aBuilder item: #'Catalog Browser')
        order: 0.19; 
      icon: Smalltalk ui icons catalogIcon;  
        parent: #'Tools';
        action: [ self open ].

Take a look at PragmaMenuBuilder for how that works

In Pharo4 that is the MetacelloConfigurationBrowser:

MetacelloConfigurationBrowser class>>menuCommandOn: aBuilder  
<worldMenu>

(aBuilder item: 'Configuration Browser')
    parent: #Tools;
    order: 0.20;
    action: [self new openWithSpec]; 
    icon: Smalltalk ui icons configIcon
like image 37
Stephan Eggermont Avatar answered Oct 29 '25 03:10

Stephan Eggermont