=== 2008/07/08 ===
其實,Forth 不必使用 prototype 的概念來實現物件。
如果所有的 Forth 物件都來自 Eiffel,則可以使用 factory.
variable x
geometry_factory.new_circle x !
x.radius
x geometry_factory.recycle_circle
x 是一個很簡單的 forth 變數。
而 geometry_factory.new_circle 會建立一個圓,把 object id 放在 forth 的資料堆疊上。
不必使用物件堆疊。執行 x.radius 時,會將 x 當成一物件 id ,在 Eiffel 側找到那個物件,找到那物件的 feature radius 並執行。geometry_factory.recycle_circle 會回收 x 指向的物件。
在 Forth 側,不提供物件導向。只提供 x.y 這樣的語法。和 Lua 比較,Lua 也沒有物件導向。只有 table 而已。
如果要對在堆疊頂的物件進行操作,可以用 top.radius 。
一個問題是:這樣的方法,執行速度會很差,因為不論是 x.radius 或是 top.radius ,都必須在執行階段去查詢和物件結合的 prototype ,才知道 radius 做的是什麼事。
解決的方法是想法子在編譯時就決定 x 的型別。所以可以考慮修正如下:
variable x
geometry_factory.new_circle x !
x circle.radius
x geometry_factory.recycle_circle
這樣,程式師必須清楚知道 x 的型別。這方法類似 gtk 的作法。
2008年7月7日 星期一
Forth and prototype
=== 2008/07/07 ===
思索如何在 forth 中引進物件。引進的方式必須和 eiffel 有很好的配合,又必須適合 dynamic language。一種方法是在 forth 使用 prototype,類似 Javascript 。
我希望觀念儘量和 Eiffel 接近。Forth 可以使用 Eiffel 的物件做為 prototype,再增加自己的資料。
此外,允許使用 x.y 的呼叫方式。此一方式必須符合 Eiffel 的各種規則。如果要對物件堆疊上的物件做處理,必須先給物件一個名字。以下是嘗試:
reference circle
create-circle
circle object!
circle.radius .
reference box
create-box
box object!
box.width .
reference circular-box
prototype circle
prototype box
嗯,似乎不容易以 prototype 方式建造這 circular-box 物件。
因此,解決的方法似乎還必須給予 reference 型別如下:
reference circular-box \ 無型別的 reference
prototype circle \ 具型別 circle
prototype box \ 具型別 box
create-method make-circular-box
origin3d 1.0 make-circle origin3d 1.0 1.0 1.0 make-box ;
create circular-box.make-circular-box
circle-box.radius .
circle-box.width .
如此建造出來的物件沒有 eiffel merge features 的能力。但似乎可用了。
在實現上,不急著做 prototype 。可以先做 x.y 這樣的呼叫。由 Eiffel 建立物件的 prototype。
思索如何在 forth 中引進物件。引進的方式必須和 eiffel 有很好的配合,又必須適合 dynamic language。一種方法是在 forth 使用 prototype,類似 Javascript 。
我希望觀念儘量和 Eiffel 接近。Forth 可以使用 Eiffel 的物件做為 prototype,再增加自己的資料。
此外,允許使用 x.y 的呼叫方式。此一方式必須符合 Eiffel 的各種規則。如果要對物件堆疊上的物件做處理,必須先給物件一個名字。以下是嘗試:
reference circle
create-circle
circle object!
circle.radius .
reference box
create-box
box object!
box.width .
reference circular-box
prototype circle
prototype box
嗯,似乎不容易以 prototype 方式建造這 circular-box 物件。
因此,解決的方法似乎還必須給予 reference 型別如下:
reference circular-box \ 無型別的 reference
prototype circle \ 具型別 circle
prototype box \ 具型別 box
create-method make-circular-box
origin3d 1.0 make-circle origin3d 1.0 1.0 1.0 make-box ;
create circular-box.make-circular-box
circle-box.radius .
circle-box.width .
如此建造出來的物件沒有 eiffel merge features 的能力。但似乎可用了。
在實現上,不急著做 prototype 。可以先做 x.y 這樣的呼叫。由 Eiffel 建立物件的 prototype。
訂閱:
文章 (Atom)