如何在 squeak 中动态更改方法的名称?
我有一个类,我想在运行时更改特定方法的名称。 我猜“行为”类中有一个方法可以做到这一点。 但我就是找不到它。 有什么帮助吗? [吱吱声]
I have a class and I want to change the name of a specific method in run time.
I guess there's a method in the 'Behavior' class that does it. But I just can't find it. any help? [in squeak]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用户执行此操作的正常方法是修改方法源并“接受它”,然后删除旧版本。 因此,基本的 Squeak 不太可能包含单一方法来执行此操作,尽管我可能是错的。
但是,如果您安装了 OmniBrowser,则有一个名为“rename”的重构方法,您可以检查并查找执行此重构的代码。 它相当复杂,首先因为重构是使用命令模式完成的,这涉及到一些重定向来解决,但其次因为这是一个相当复杂的重构,其中包括修改调用站点。
The normal way a user does this is to modify the method source and 'accept it' then delete the old version. So it's not likely that basic Squeak includes a single method to do this, although I could be wrong.
However if you install, for example, OmniBrowser there is a method refactoring called 'rename' and you could inspect and find code to perform this refactoring. It is fairly complex, firstly because the refactorings are done using the command pattern which involves a little redirection to work out, but secondly because this is a fairly complex refactoring which includes modifying the call sites.
你的建议对我来说是一个巨大的危险信号。
你想用这个来实现什么目的?
您的意思是您想更改在运行时调用的方法的名称吗?
如果是这样,那很容易。
做类似的事情:
What you are suggesting puts HUGE red flags up for me.
What is it you are trying to accomplish with this?
Do you mean you want to change the name of the method you are calling at runtime?
If so, that's easy.
do something like:
你最好使用重构
You best use a refactoring
尽可能避免在实际代码中使用巫毒魔法。
话虽这么说,您可以通过动态操作方法来做一些非常有趣的事情。
例如,Etoys 中的代码块被转换为 Smalltalk 方法。 其他 DSL 实现也可以从类似的元编程技巧中受益。
经过一番尝试后,我想出了以下用于重命名一元方法的代码:
如果您浏览 Squeak 代码的时间比我长一点,您可能会找到一种更简单的方法来执行此操作。
Avoid voodoo magic in real code when possible.
That being said you can do some very interesting things by manipulating methods dynamically.
For instance the code bricks in Etoys are translated into Smalltalk methods. Other DSL implementations can also benefit from similar metaprogramming tricks.
After experimenting a bit I came up with the following code for renaming unary methods:
You could probably find an easier way to do this if you browse through the Squeak code a bit longer than I did.
实际上,您无法更改方法的名称,因为它没有名称。
对象的方法字典将 Symbols 映射到 CompiledMethods。 “更改方法的名称”意味着“将 CompiledMethod 值从这个键移动到那个键”。
You can't change a method's name, really, because it doesn't have one.
An object's method dictionary maps Symbols to CompiledMethods. "Change the name of a method" means "move the CompiledMethod value from this key to that key".