扩展 Pharo Smalltalk 类行为的最佳方式?
我想使用一种方法来扩展 String 类,以从字符串中创建 url slug。我在这里找到了一个链接,显示如何将扩展移动到自己的包中:
Smalltalk Daily 07/13/10:扩展行为 II。
但是,我在 Pharo Smalltalk 中找不到任何“移动到包”选项。是否可以只用新方法扩展核心类,或者有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Pharo 或 Squeak 中,将 MyPackage 的扩展方法放在名为
*mypackage
的方法类别中(或者,如果您想更具描述性,*mypackage-slug
)。这些类别中的方法自动属于 MyPackage 包(至少从 Monticello 的角度来看)
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)
“用新方法扩展核心类可以吗,还是有更好的方法?”
这个决定需要权衡。事实上,Pharo 直到最近才使用 String>>asUrl,它作为清理的一部分被删除系统。一方面,一些人认为在不具有相似协议(语义上相似)的对象之间使用转换方法是不好的风格(参见 Kent Beck 的最佳实践)。此外,这会导致核心类(如 String 和 Object)臃肿。然而,在您自己的应用程序中,可能有一个很好的理由来平衡这些因素,并且由于您将其与您的应用程序一起打包,而不是与系统一起打包,所以请放心。
"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.
在pharo 7中,*是被禁止的。
一条消息告诉您必须勾选方法编辑窗格中的扩展复选框。
如果这样做,您可以选择您的套餐。
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.