是否可以在 Smalltalk 中扩展单个对象 [英] Is it possible to extend an individual object in Smalltalk

查看:67
本文介绍了是否可以在 Smalltalk 中扩展单个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Smalltalk 反射,我想知道是否可以像在 Ruby 中那样扩展单个对象.我的意思是一个只有特定对象响应的选择器.

I'm doing research in Smalltalk reflection, and I was wondering if it was possible to extend an individual object like that would be possible for instance in Ruby. With this I mean a selector that only particular objects respond to.

这里有一些 Ruby 代码说明了我的意思.澄清一下:在 Ruby 中,这会为此对象打开一个虚拟类,并使用新定义对其进行扩展.这里的重要部分是类定义没有任何变化!

Here is some Ruby code that states what I mean. For clarification: in Ruby this open a virtual class for this object, and extends it with a new definition. The vital part here is that nothing changes to the class definition!

o = Object.new
o.instance_eval {def foo;puts "foo";end}
o.foo #=> "foo"

#however this will fail:
m = Object.new
m.foo #=> NoMethod error

更具体地说,我的问题是这是否可以在标准 Squeak/Pharo 或其他 smalltalk 实现中实现,而无需添加大量结构或代码来实现这一点.因此,使用 Smalltalk 中存在的具有规则反射功能的其他词.

More specifically my question is whether this is possible in standard Squeak/Pharo or other smalltalk implementations, without adding substantial structures or code to allow this. So with other words with regular reflective features that exist in Smalltalk.

例如,可以添加方法、删除方法、将新代码编译到类中、更改实例变量等等,但我还没有找到扩展单个对象的方法.

As an example, it is possible to add methods, remove methods, compile new code into a class, change instance variables and just about anything, but I haven't found a way to extend a single object.

Test addInstVarNamed: #var.
Test compile: 'var ^var'.
t:= Test new.
Test instVarNames.
t instVarNamed: #var put: 666. 
t var. #=> 666

如果答案是否定的,请解释原因.我不是要解决这个问题,而是要理解为什么它不是闲聊.

推荐答案

在 Smalltalk 中,没有内置的以这种方式执行特定于实例的行为的方法.Smalltalk 坚持每个对象都属于一个类,其行为和状态形状取决于该类的原则.这就是为什么您可以轻松更改类(添加 inst 变量、编译新方法等),但这意味着更改其所有实例的行为.然而,有不同的方法(根据 Smalltalk 的风格)来实现特定于实例的行为,例如轻量级类,其思想是为特定实例创建一个特殊的(轻量级)类并用自定义的类替换原始类一.因此,每个特殊"实例都有一个特殊的类.AFAIK 在 Digitalk St 中,调度机制更加灵活,可以轻松实现基于实例的行为(请参阅第 4 个链接).我会在这里留下一些您可能会觉得有用的链接:

In Smalltalk there is no built in way of doing instance-specific behavior in that way. Smalltalk adheres to the principle that each object belongs to a class and its behavior and state shape depends on the class. That is why you can easily change the class (add inst vars, compile new methods, etc), but that means changing the behavior to all of its instances. There are, however, different approaches (according to the Smalltalk flavor) for achieving instance-specific behavior, such as Lightweight Classes, where the idea is to create a special (lightweight) class for a particular instance and replace the original class with the custom one. As a result you have a special class for each "special" instance. AFAIK in Digitalk St the dispatching mechanism is a bit more flexible and allows to easily implement instance-based behavior (see 4th link). I'll leave here some links that you may find useful:

HTH

Hernan 发布的链接(Hinkle, Jones & Johnson 的调试对象")是我所指的链接,但找不到.em>

这篇关于是否可以在 Smalltalk 中扩展单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆