Best way to extend Pharo Smalltalk class behavior?
Asked Answered
G

3

6

I want to extend the String class with a method to create a url slug out of a string. I found a link here that shows how you can move extensions to their own package:

Smalltalk Daily 07/13/10: Extending Behavior II.

However, I can't find any "move to package" option in Pharo Smalltalk. Is it ok to just extend the core class with the new method, or is there a better way?

Greatnephew answered 2/2, 2011 at 1:18 Comment(0)
F
8

In Pharo or Squeak put the extension methods for MyPackage in a method category called *mypackage (or if you want to be more descriptive *mypackage-slug).

The methods in these categories belong automatically to the MyPackage package (at least from the Monticello point of view)

Fibrillation answered 2/2, 2011 at 1:38 Comment(0)
M
8

"Is it ok to just extend the core class with the new method, or is there a better way?"

There are tradeoffs to this decision. In fact, Pharo had String>>asUrl until very recently, when it was removed as part of cleaning the system. On one hand, it is considered bad style by some (see Kent Beck's Best Practices) to have conversion methods between objects that do not have similar protocols (are semantically similar). Additionally, this leads to bloated core classes (like String and Object). However, in your own application, there may be a good reason that balances these factors, and since you are packaging it with your app, and not with the system, rock out.

Meagan answered 3/2, 2011 at 2:34 Comment(0)
R
0

In pharo 7, * is forbidden.

A message tells you have to tick the extension checkbox in method edition pane.

If you do so, you can choose your package.

Rundell answered 17/1, 2020 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.