継承とは(修正)

継承

When an object receives a message it looks for a matching slot, if not found, the lookup continues depth first recursively in its protos. Lookup loops are detected (at runtime) and avoided. If the matching slot contains an activatable object, such as a Block or CFunction, it is activated, if it contains any other type of value it returns the value. Io has no globals and the root object in the Io namespace is called the Lobby.
Since there are no classes, there's no difference between a subclass and an instance. Here's an example of creating a the equivalent of a subclass:

Io> Dog := Object clone
==> Object_0x4a7c0 

The above code sets the Lobby slot "Dog" to a clone of the Object object. Notice it only contains a protos list contains a reference to Object. Dog is now essentially a subclass of Object. Instance variables and methods are inherited from the proto. If a slot is set, it creates a new slot in our object instead of changing the proto:

Io> Dog color := "red"
Io> Dog
==> Object_0x4a7c0:
  color := "red"

Ioプログラミングガイド - オブジェクト - 継承

継承

あるオブジェクトが該当するスロットを探すようなメッセージを受けた場合、見つからなければ、その1階層深いprotoリストへprotoリストを深さ優先で再帰的に検査を続けます。検査のループが見つかった場合には、検査は中断されます。マッチするスロットに実行可能なオブジェクトがあれば、例えばBlockやCFunctionなどの場合、それが実行され、値があればその値を返します。Ioにはグローバルオブジェクトはなく、LobbyというIo名前空間のルートオブジェクトがあります。

クラスがないため、サブクラスとインスタンスの違いはありません。以下は、サブクラスに相当するものを作成する例です:

Io> Dog := Object clone
==> Object_0x4a7c0 

上記のコードでは、Lobbyの"Dog"スロットに"Object"オブジェクトのクローンをセットしています。そのクローンはObjectへの参照を含むprotosリストのみを持っています。これで、本質的にDogはObjectのサブクラスとなります。インスタンスの変数やメソッドはそのprotoから継承されます。スロットがセットされた場合、protoは変更されず、作成したオブジェクトに新しいスロットを作成します:

Io> Dog color := "red"
Io> Dog
==> Object_0x4a7c0:
  color := "red"

あるオブジェクトのクローンが、そのオブジェクトのサブクラスのインスタンスと同等の意味を持つ。あとは、そのクローンに、独自にスロットを追加していけばおk。

Lobbyオブジェクトが実行環境みたいな感じで、以下の2文は同じ意味になる。

Io> Dog color := "white"
Io> Lobby Dog color := "white"

Dogのcolorスロットは、つまりLobbyのDogスロットのcolorスロット。