| 1 |
@node Object system, Library modules - Overview, Core library, Top |
| 2 |
@chapter Object system |
| 3 |
@c NODE オブジェクトシステム |
| 4 |
|
| 5 |
@c EN |
| 6 |
Gauche's object system design is largely inspired by |
| 7 |
STklos, whose design has come from TinyCLOS. |
| 8 |
It supports multiple inheritance, multimethods, |
| 9 |
and metaobject protocol. |
| 10 |
@c JP |
| 11 |
Gaucheのオブジェクトシステムのデザインは、TinyCLOSから |
| 12 |
派生したSTklosから大きな影響を受けています。 |
| 13 |
多重継承、マルチメソッド、メタオブジェクトプトロコルを |
| 14 |
サポートしています。 |
| 15 |
@c COMMON |
| 16 |
|
| 17 |
@c EN |
| 18 |
The type system is integrated to the object system, |
| 19 |
that is, a string is an instance of the class @code{<string>}, |
| 20 |
and so on. |
| 21 |
@c JP |
| 22 |
型システムは、オブジェクトシステムに統合されています。 |
| 23 |
つまり、文字列は@code{<string>}クラスのインスタンスである、などです。 |
| 24 |
@c COMMON |
| 25 |
|
| 26 |
@c ---------------------------------------------------------------------- |
| 27 |
@menu |
| 28 |
* Introduction to the object system:: |
| 29 |
* Class:: |
| 30 |
* Instance:: |
| 31 |
* Generic function and method:: |
| 32 |
* Metaobject protocol:: |
| 33 |
@end menu |
| 34 |
|
| 35 |
@node Introduction to the object system, Class, Object system, Object system |
| 36 |
@section Introduction to the object system |
| 37 |
@c NODE オブジェクトシステムの紹介 |
| 38 |
|
| 39 |
@c EN |
| 40 |
This section briefly explains the basic structure of |
| 41 |
Gauche's object system. It is strongly influenced |
| 42 |
by CLOS (Common-Lisp Object System). |
| 43 |
If you have experience in CLOS or related systems |
| 44 |
such as TinyCLOS, STklos or Guile's object system, |
| 45 |
you may skip to the next section. |
| 46 |
|
| 47 |
Three concepts play the central role in CLOS-like object systems: |
| 48 |
A @emph{class}, a @emph{generic function}, and a @emph{method}. |
| 49 |
|
| 50 |
A @emph{class} specifies a structure of object. |
| 51 |
It also defines a datatype (strictly speaking, |
| 52 |
it's not the same thing as a datatype, |
| 53 |
but let's skip the complicated part for now). |
| 54 |
|
| 55 |
For example, a point in 2D space can be represented by |
| 56 |
x and y coordinates. |
| 57 |
A point class can be defined using @code{define-class} macro. |
| 58 |
In the shortest form, it can be defined like this: |
| 59 |
|
| 60 |
@c JP |
| 61 |
この節では Gauche のオブジェクトシステムの基本的構造について手短かに |
| 62 |
説明します。Gauche のオブジェクトシステムは CLOS (Common-Lisp Object System) |
| 63 |
に強く影響されています。CLOS やそれにかかわりのある TinyCLOS、STklos、 |
| 64 |
あるいは Guile のオブジェクトシステムの経験がある方は、この節を飛ばして、 |
| 65 |
次の節にいってもよいでしょう。 |
| 66 |
|
| 67 |
CLOS 風のオブジェクトシステムでは 3 つの概念が中心的な役割をはたします。 |
| 68 |
@emph{クラス}、@emph{ジェネリック関数}、それに@emph{メソッド}です。 |
| 69 |
|
| 70 |
@emph{クラス}はオブジェクトの構造を指定します。また、データ型も定義します |
| 71 |
(厳密にいうと、データ型とはちがうものですが、難しいところは今は |
| 72 |
おいておきましょう)。 |
| 73 |
|
| 74 |
たとえば、2 次元空間の点は x 座標と y 座標で表現できます。点のクラスは |
| 75 |
@code{define-class} マクロを使って定義できます。もっとも手短かには、 |
| 76 |
次のように定義できます。 |
| 77 |
|
| 78 |
@c COMMON |
| 79 |
@example |
| 80 |
(define-class <2d-point> () (x y)) |
| 81 |
@end example |
| 82 |
|
| 83 |
@c EN |
| 84 |
(You can find the code of definitions in the examples of this section |
| 85 |
in @code{examples/oointro.scm} of Gauche's source distribution.) |
| 86 |
|
| 87 |
The symbol @code{<2d-point>} is the name of the class, and also |
| 88 |
the global variable @code{<2d-point>} is bound to a class object. |
| 89 |
Surrounding a class name by @code{<} and @code{>} is just a |
| 90 |
convention; you can pass any symbol to @code{define-class}. |
| 91 |
|
| 92 |
The second argument of @code{define-class} is a list of |
| 93 |
direct superclasses, which specifies inheritance of the class. |
| 94 |
We'll come back to it later. |
| 95 |
|
| 96 |
The third argument of @code{define-class} is a list of |
| 97 |
@emph{slots}. A slot is a storage space, usually in each object, |
| 98 |
where you can store a value. It is something similar to |
| 99 |
what is called a field or an instance variable in other |
| 100 |
object-oriented languages; but slots can be configured more |
| 101 |
than just a per-object storage space. |
| 102 |
|
| 103 |
Now we defined a 2D point class, so we can create an instance |
| 104 |
of a point. You can pass a class to a generic function @code{make} |
| 105 |
to create an instance. (Don't worry about what generic function |
| 106 |
is---think it as a special type of function, just for now). |
| 107 |
|
| 108 |
@c JP |
| 109 |
(この節の例の定義コードは Gauche ソース配布物のなかの |
| 110 |
@code{examples/oointro.scm} にあります。) |
| 111 |
|
| 112 |
シンボル @code{<2d-point>} はクラスの名前です。グローバル変数 |
| 113 |
@code{<2d-point>}はクラスオブジェクトに束縛されています。クラス名を |
| 114 |
@code{<} と @code{>} で囲むのは慣習にすぎません。@code{define-class} |
| 115 |
には任意のシンボルを渡すことができます。 |
| 116 |
|
| 117 |
@code{define-class}の第二引数は、直接のスーパークラスのリストです。 |
| 118 |
これでこのクラスの継承を指定します。これについては後で説明します。 |
| 119 |
|
| 120 |
@code{define-class}の第三引数は@emph{スロット}のリストです。 |
| 121 |
スロットはなにかを格納しておく場所で、通常は各オブジェクトに対して、値を格納できます。 |
| 122 |
これは、他のオジェクト指向言語では、フィールドとかインスタンス変数などと |
| 123 |
呼ばれているものに似たものです。しかし、スロットは単なる各オブジェクト毎の |
| 124 |
格納スペース以上の使い方ができます。 |
| 125 |
|
| 126 |
さて、2 次元の点のクラスを定義しましたので、点のインスタンスを生成できます。 |
| 127 |
クラスをジェネリック関数 @code{make} に渡してインスタンスを生成します。 |
| 128 |
(ジェネリック関数がどんなものであるかは気にしないで下さい。今はある特殊な |
| 129 |
タイプの関数とだけ考えておいてください。) |
| 130 |
|
| 131 |
@c COMMON |
| 132 |
@example |
| 133 |
(define a-point (make <2d-point>)) |
| 134 |
|
| 135 |
a-point @result{} #<<2d-point> 0x8117570> |
| 136 |
@end example |
| 137 |
|
| 138 |
@c EN |
| 139 |
If you are using @code{gosh} interactively, you can use |
| 140 |
a generic function @code{describe} to inspect the internal |
| 141 |
of an instance. |
| 142 |
A short alias, @code{d}, is defined to @code{describe} for |
| 143 |
the convenience. (See @ref{Interactive session} |
| 144 |
for the details). |
| 145 |
|
| 146 |
@c JP |
| 147 |
対話モードで @code{gosh} を使っているなら、ジェネリック関数 @code{describe} を |
| 148 |
使って、インスタンスの内部を点検できます。 |
| 149 |
便利なので、省略形として@code{d}という名前も定義されています。 |
| 150 |
(詳細については、@ref{Interactive session}参照) |
| 151 |
|
| 152 |
@c COMMON |
| 153 |
@example |
| 154 |
gosh> (d a-point) |
| 155 |
#<<2d-point> 0x8117570> is an instance of class <2d-point> |
| 156 |
slots: |
| 157 |
x : #<unbound> |
| 158 |
y : #<unbound> |
| 159 |
@end example |
| 160 |
|
| 161 |
@c EN |
| 162 |
In order to access or modify the value of the slot, you can use |
| 163 |
@code{slot-ref} and @code{slot-set!}, respectively. |
| 164 |
These names are taken from STklos. |
| 165 |
|
| 166 |
@example |
| 167 |
(slot-ref a-point 'x) ;; access to the slot x of a-point |
| 168 |
@result{} @r{error, since slot 'x doesn't have a value yet} |
| 169 |
|
| 170 |
(slot-set! a-point 'x 10.0) ;; set 10.0 to the slot x of a-point |
| 171 |
|
| 172 |
(slot-ref a-point 'x) |
| 173 |
@result{} 10.0 |
| 174 |
@end example |
| 175 |
|
| 176 |
@c JP |
| 177 |
スロットの値にアクセスあるいは値を変更するためには、それぞれ、 |
| 178 |
@code{slot-ref} および @code{slot-set!} が使えます。これらの名前は |
| 179 |
STklos からとりました。 |
| 180 |
|
| 181 |
@example |
| 182 |
(slot-ref a-point 'x) ;; a-point のスロット x にアクセス |
| 183 |
@result{} @r{error, since slot 'x doesn't have a value yet} |
| 184 |
|
| 185 |
(slot-set! a-point 'x 10.0) ;; a-point のスロット x を 10.0 に設定 |
| 186 |
|
| 187 |
(slot-ref a-point 'x) |
| 188 |
@result{} 10.0 |
| 189 |
@end example |
| 190 |
|
| 191 |
@c EN |
| 192 |
Gauche also provides a shorter name, @code{ref}, which can also |
| 193 |
be used in SRFI-17's generalized @code{set!} syntax: |
| 194 |
@c JP |
| 195 |
Gauche では、より短かい名前 @code{ref} も使えます。これを使えば |
| 196 |
SRFI-17 の一般化された @code{set!} の構文が使えます。 |
| 197 |
@c COMMON |
| 198 |
@example |
| 199 |
(ref a-point 'x) @result{} 10.0 |
| 200 |
|
| 201 |
(set! (ref a-point 'y) 20.0) |
| 202 |
|
| 203 |
(ref a-point 'y) @result{} 20.0 |
| 204 |
@end example |
| 205 |
|
| 206 |
@c EN |
| 207 |
Now you can see slot values are set. |
| 208 |
@c JP |
| 209 |
スロットの値が設定されていることを見てみましょう。 |
| 210 |
@c COMMON |
| 211 |
@example |
| 212 |
gosh> (d a-point) |
| 213 |
#<<2d-point> 0x8117570> is an instance of class <2d-point> |
| 214 |
slots: |
| 215 |
x : 10.0 |
| 216 |
y : 20.0 |
| 217 |
@end example |
| 218 |
|
| 219 |
@c EN |
| 220 |
In practice, it is usually convenient if you can specify the default |
| 221 |
value for a slot, or give values for slots when you create an instance. |
| 222 |
Such information can be specified by @emph{slot options}. |
| 223 |
Let's modify the definition of @code{<2d-point>} like this: |
| 224 |
|
| 225 |
@c JP |
| 226 |
実際には、スロットにデフォルト値を設定できたり、インスタンス生成時に |
| 227 |
スロットに値を設定できると便利なことが多いです。このような情報は、 |
| 228 |
@emph{スロットオプション}で指定できます。@code{<2d-point>}の定義を |
| 229 |
次のように変更してみましょう。 |
| 230 |
|
| 231 |
@c COMMON |
| 232 |
@example |
| 233 |
(define-class <2d-point> () |
| 234 |
((x :init-value 0.0 :init-keyword :x :accessor x-of) |
| 235 |
(y :init-value 0.0 :init-keyword :y :accessor y-of))) |
| 236 |
@end example |
| 237 |
|
| 238 |
@c EN |
| 239 |
Note that each slot specification is now a list, instead of just |
| 240 |
a symbol as in the previous example. |
| 241 |
The list's car now specifies the slot name, and its cdr |
| 242 |
gives various information. The value after @code{:init-value} |
| 243 |
defines the default value of the slot. The keyword after @code{:init-keyword} |
| 244 |
defines the keyword argument which can be passed to @code{make} to |
| 245 |
initialize the slot at creation time. |
| 246 |
The name after keyword @code{:accessor} is bound to a generic |
| 247 |
function that can be used to access/modify the slot, instead of |
| 248 |
using @code{slot-ref}/@code{slot-set!}. |
| 249 |
|
| 250 |
Let's see some interactive session. You create an instance |
| 251 |
of the new @code{<2d-point>} class, and you can see the slots are |
| 252 |
initialized by the default values. |
| 253 |
@c JP |
| 254 |
各スロットの指定は、前の例では一つのシンボルでしたが、こんどはリスト |
| 255 |
であることに注意してください。 |
| 256 |
こんどは、リストの car 部分がスロット名の指定で、cdr 部分にいろいろな情報が |
| 257 |
はいります。@code{:init-value} のうしろの値はそのスロットのデフォルト値を |
| 258 |
定義しています。@code{:init-keyword}のうしろのキーワードは、生成時にスロットを |
| 259 |
初期化するために @code{make} に渡すことのできるキーワード引数を定義しています。 |
| 260 |
キーワード @code{:accessor} のうしろの名前は、スロットへのアクセス/変更の |
| 261 |
ために使えるジェネリック関数に束縛されます。これは |
| 262 |
@code{slot-ref}/@code{slot-set!} のかわりに使います。 |
| 263 |
|
| 264 |
ちょっとした対話セッションをみてみましょう。新しい @code{<2d-point>} クラスの |
| 265 |
インスタンスをつくります。スロットがデフォルト値で初期化されているのがわかり |
| 266 |
ますね。 |
| 267 |
@c COMMON |
| 268 |
@example |
| 269 |
gosh> (define a-point (make <2d-point>)) |
| 270 |
a-point |
| 271 |
gosh> (d a-point) |
| 272 |
#<<2d-point> 0x8148680> is an instance of class <2d-point> |
| 273 |
slots: |
| 274 |
x : 0.0 |
| 275 |
y : 0.0 |
| 276 |
@end example |
| 277 |
|
| 278 |
@c EN |
| 279 |
You create another instance, this time giving initialization values |
| 280 |
by keyword arguments. |
| 281 |
@c JP |
| 282 |
こんどは、キーワード引数で初期値を与えて、別のインスタンスを作ります。 |
| 283 |
@c COMMON |
| 284 |
@example |
| 285 |
gosh> (define b-point (make <2d-point> :x 50.0 :y -10.0)) |
| 286 |
b-point |
| 287 |
gosh> (d b-point) |
| 288 |
#<<2d-point> 0x8155b80> is an instance of class <2d-point> |
| 289 |
slots: |
| 290 |
x : 50.0 |
| 291 |
y : -10.0 |
| 292 |
@end example |
| 293 |
|
| 294 |
@c EN |
| 295 |
Accessors are less verbose than @code{slot-ref}/@code{slot-set!}, thus |
| 296 |
convenient. |
| 297 |
@c JP |
| 298 |
アクセサは @code{slot-ref}/@code{slot-set!} より短かくなって便利に |
| 299 |
なりましたね。 |
| 300 |
@c COMMON |
| 301 |
@example |
| 302 |
gosh> (x-of a-point) |
| 303 |
0.0 |
| 304 |
gosh> (x-of b-point) |
| 305 |
50.0 |
| 306 |
gosh> (set! (y-of a-point) 3.33) |
| 307 |
#<undef> |
| 308 |
gosh> (y-of a-point) |
| 309 |
3.33 |
| 310 |
@end example |
| 311 |
|
| 312 |
@c EN |
| 313 |
The full list of available slot options is described in |
| 314 |
@ref{Defining class}. At a first glance, |
| 315 |
the declarations of such slot options may look verbose. |
| 316 |
The system might have provide a static way to define |
| 317 |
init-keywords or accessor names automatically; however, |
| 318 |
CLOS-like systems prefer flexibility. |
| 319 |
Using a mechanism called metaobject protocol, you can customize |
| 320 |
how these slot options are interpreted, and you can add |
| 321 |
your own slot options as well. |
| 322 |
See @ref{Metaobject protocol}, for details. |
| 323 |
|
| 324 |
We can also have @code{<2d-vector>} class in similar fashion. |
| 325 |
|
| 326 |
@c JP |
| 327 |
利用可能な全スロットオプションのリストは@ref{Defining class}にあります。 |
| 328 |
ちょっと見ると、これらのスロットオプションの宣言は冗長に見えるかもしれません。 |
| 329 |
システム側で初期化キーワードやアクセス名を自動的に定義する静的な仕組を |
| 330 |
用意することもできたかもしれません。 |
| 331 |
しかしながら、CLOS 風のシステムでは柔軟性の方をより重視します。 |
| 332 |
メタオブジェクトプロトコルと呼ばれる機構をもちいて、これらの |
| 333 |
スロットオプションがどのように解釈されるかをカスタマイズすることができます。 |
| 334 |
また、自分用のスロットオプションを追加することもできます。 |
| 335 |
詳しくは @ref{Metaobject protocol} を見てください。 |
| 336 |
|
| 337 |
また、似たような流儀で @code{<2d-vector>} クラスを定義できます。 |
| 338 |
|
| 339 |
@c COMMON |
| 340 |
@example |
| 341 |
(define-class <2d-vector> () |
| 342 |
((x :init-value 0.0 :init-keyword :x :accessor x-of) |
| 343 |
(y :init-value 0.0 :init-keyword :y :accessor y-of))) |
| 344 |
@end example |
| 345 |
|
| 346 |
@c EN |
| 347 |
Yes, we can use the same accessor name like @code{x-of}, and |
| 348 |
it is effectively overloaded. |
| 349 |
|
| 350 |
If you are familiar with mainstream object-oriented languages, |
| 351 |
you may wonder where methods are. Here they are. |
| 352 |
The following form defines a method @code{move-by!} of |
| 353 |
three arguments, @var{pt}, @var{dx}, @var{dy}, where @var{pt} is |
| 354 |
an instance of @code{<2d-point>}. |
| 355 |
|
| 356 |
@c JP |
| 357 |
そうです。同じアクセサ名を @code{x-of} のように使うことができます。 |
| 358 |
これは事実上、多重定義されます。 |
| 359 |
|
| 360 |
主流のオブジェクト指向言語に慣れた方は、そろそろ、メソッドはどこにあるのだと |
| 361 |
思い始めているかもしれません。いよいよメソッドの登場です。以下のフォームは、3 つの引数 |
| 362 |
@var{pt}、@var{dy}、@var{dy} をとり、 |
| 363 |
しかも@var{pt} は @code{<2d-point>} のインスタンスであるような |
| 364 |
メソッド @code{move-by!} を定義するものです。 |
| 365 |
|
| 366 |
@c COMMON |
| 367 |
@example |
| 368 |
(define-method move-by! ((pt <2d-point>) dx dy) |
| 369 |
(inc! (x-of pt) dx) |
| 370 |
(inc! (y-of pt) dy)) |
| 371 |
@end example |
| 372 |
|
| 373 |
@c EN |
| 374 |
The second argument of @code{define-method} macro specifies a |
| 375 |
@emph{method specializer list}. It indicates the first argument must be |
| 376 |
an instance of @code{<2d-point>}, and the second and third |
| 377 |
can be any type. The syntax to call a method is just like |
| 378 |
the one to call an ordinary function. |
| 379 |
|
| 380 |
@c JP |
| 381 |
@code{define-method} マクロの第二引数は@emph{メソッド特定化子リスト}を |
| 382 |
指定しています。これは、第一引数が @code{<2d-point>} のインスタンスで |
| 383 |
なければならないことを示し、第二、第三引数は任意の型でよいことを |
| 384 |
示しています。メソッド呼び出しの構文は通常関数の呼び出しと同じです。 |
| 385 |
|
| 386 |
@c COMMON |
| 387 |
@example |
| 388 |
gosh> (move-by! b-point 1.4 2.5) |
| 389 |
#<undef> |
| 390 |
gosh> (d b-point) |
| 391 |
#<<2d-point> 0x8155b80> is an instance of class <2d-point> |
| 392 |
slots: |
| 393 |
x : 51.4 |
| 394 |
y : -7.5 |
| 395 |
@end example |
| 396 |
|
| 397 |
@c EN |
| 398 |
You can overload the method by different specializers; here |
| 399 |
you can move a point using a vector. |
| 400 |
@c JP |
| 401 |
別の特定化子によりこのメソッドを多重定義することが可能です。以下のように |
| 402 |
ベクタを使って点を動かすこともできます。 |
| 403 |
@c COMMON |
| 404 |
@example |
| 405 |
(define-method move-by! ((pt <2d-point>) (delta <2d-vector>)) |
| 406 |
(move-by! pt (x-of delta) (y-of delta))) |
| 407 |
@end example |
| 408 |
|
| 409 |
@c EN |
| 410 |
Specialization isn't limited to a user-defined classes. |
| 411 |
You can also specialize a method using Gauche's built-in type. |
| 412 |
@c JP |
| 413 |
特定化はユーザ定義のクラスにだけ限定されているわけではありません。 |
| 414 |
Gauche の組み込み型を使ってメソッドを特定化することもできます。 |
| 415 |
|
| 416 |
@c COMMON |
| 417 |
@example |
| 418 |
(define-method move-by! ((pt <2d-point>) (c <complex>)) |
| 419 |
(move-by! pt (real-part c) (imag-part c))) |
| 420 |
@end example |
| 421 |
|
| 422 |
@c EN |
| 423 |
And here's the example session: |
| 424 |
@c JP |
| 425 |
以下はセッション例です。 |
| 426 |
@c COMMON |
| 427 |
@example |
| 428 |
gosh> (define d-vector (make <2d-vector> :x -9.0 :y 7.25)) |
| 429 |
d-vector |
| 430 |
gosh> (move-by! b-point d-vector) |
| 431 |
#<undef> |
| 432 |
gosh> (d b-point) |
| 433 |
#<<2d-point> 0x8155b80> is an instance of class <2d-point> |
| 434 |
slots: |
| 435 |
x : 42.4 |
| 436 |
y : -0.25 |
| 437 |
gosh> (move-by! b-point 3+2i) |
| 438 |
#<undef> |
| 439 |
gosh> (d b-point) |
| 440 |
#<<2d-point> 0x8155b80> is an instance of class <2d-point> |
| 441 |
slots: |
| 442 |
x : 45.4 |
| 443 |
y : -2.25 |
| 444 |
@end example |
| 445 |
|
| 446 |
@c EN |
| 447 |
You see that a method is dispatched not only by its primary |
| 448 |
receiver (@code{<2d-point>}), but also other arguments. |
| 449 |
In fact, the first argument is no more special than the rest. |
| 450 |
In CLOS-like system a method does not belong to |
| 451 |
a particular class. |
| 452 |
|
| 453 |
So what is actually a method? Inspecting @code{move-by!} |
| 454 |
reveals that it is an instance of @code{<generic>}, a generic function. |
| 455 |
(Note that @code{describe} truncates the printed value in @code{methods} |
| 456 |
slot for the sake of readability). |
| 457 |
@c JP |
| 458 |
メソッドがその主レシーバ @code{<2d-point>} によってのみディスパッチされる |
| 459 |
のではなく、その他の引数によってもディスパッチされているのがわかると思います。 |
| 460 |
実は、第一引数は残りの引数に比べて何ら特別ではありません。 |
| 461 |
CLOS 風のシステムではメソッドは特定のクラスに属するものではないのです。 |
| 462 |
|
| 463 |
ではメソッドとはいったいなんでしょう。@code{move-by!} を点検してみると |
| 464 |
それが @code{<generic>} のインスタンスであり、ジェネリック関数であると |
| 465 |
わかります。(@code{describe} は可読性のために @code{methods} スロットの |
| 466 |
値の印字を途中で切っていることに注意してください。) |
| 467 |
|
| 468 |
@c COMMON |
| 469 |
@example |
| 470 |
gosh> move-by! |
| 471 |
#<generic move-by! (3)> |
| 472 |
gosh> (d move-by!) |
| 473 |
#<generic move-by! (3)> is an instance of class <generic> |
| 474 |
slots: |
| 475 |
name : move-by! |
| 476 |
methods : (#<method (move-by! <2d-point> <complex>)> #<method (move- |
| 477 |
gosh> (ref move-by! 'methods) |
| 478 |
(#<method (move-by! <2d-point> <complex>)> |
| 479 |
#<method (move-by! <2d-point> <2d-vector>)> |
| 480 |
#<method (move-by! <2d-point> <top> <top>)>) |
| 481 |
@end example |
| 482 |
|
| 483 |
@c EN |
| 484 |
I said a generic function is a special type of function. |
| 485 |
It is recognized by Gauche as an applicable object, but |
| 486 |
when applied, it selects appropriate method(s) according to |
| 487 |
its arguments and calls the selected method(s). |
| 488 |
|
| 489 |
What the @code{define-method} macro actually does is (1) to create |
| 490 |
a generic function of the given name if it does not exist yet, |
| 491 |
(2) to create a method object with the given specializers |
| 492 |
and the body, and (3) to add the method object to the generic function. |
| 493 |
|
| 494 |
The accessors are also generic functions, created implicitly by the |
| 495 |
@code{define-class} macro. |
| 496 |
@c JP |
| 497 |
ジェネリック関数は特別なタイプの関数だと言いましたが、Gauche ではこれは |
| 498 |
適用可能オブジェクトとして認識されます。それは、適用されるとその引数に |
| 499 |
対応して適切なメソッドを選択して、その選択されたメソッドを呼び出すという動作を |
| 500 |
行います。 |
| 501 |
|
| 502 |
実際 @code{define-method} マクロがやっていることは、(1)もし与えられた |
| 503 |
名前のジェネリック関数がまだ存在していなければ、これを生成し、(2)与えられた |
| 504 |
特定化子と本体とでメソッドオブジェクトを生成し、(3)ジェネリック関数に |
| 505 |
そのメソッドオブジェクトを追加する、ということです。 |
| 506 |
|
| 507 |
アクセサも同様にジェネリック関数で、@code{define-class} マクロが暗黙のうちに |
| 508 |
生成します。 |
| 509 |
|
| 510 |
@c COMMON |
| 511 |
@example |
| 512 |
gosh> (d x-of) |
| 513 |
#<generic x-of (2)> is an instance of class <generic> |
| 514 |
slots: |
| 515 |
name : x-of |
| 516 |
methods : (#<method (x-of <2d-vector>)> #<method (x-of <2d-point>)>) |
| 517 |
@end example |
| 518 |
|
| 519 |
@c EN |
| 520 |
In the mainstream dynamic object-oriented languages, a class |
| 521 |
has many roles; it defines a structure and a type, creates a |
| 522 |
namespace for its slots and methods, and is responsible for |
| 523 |
method dispatch. In Gauche, namespace is managed by modules, |
| 524 |
and method dispatch is handled by generic functions. |
| 525 |
|
| 526 |
The default printed representation of object is not very user-friendly. |
| 527 |
Gauche's @code{write} and @code{display} function call a generic |
| 528 |
function @code{write-object} when they encounter an instance |
| 529 |
they don't know how to print. You can define its method |
| 530 |
specialized to your class to customize how the instance is |
| 531 |
printed. |
| 532 |
@c JP |
| 533 |
主流の動的オジェクト指向言語では、クラスには多くの役割があります。 |
| 534 |
クラスは構造と型を定義し、スロットとメソッドの名前空間を生成し、 |
| 535 |
メソッドのディスパッチに責任をもちます。Gaucheでは、名前空間は |
| 536 |
モジュールによって管理し、メソッドのディスパッチはジェネリック関数で |
| 537 |
あつかいます。 |
| 538 |
|
| 539 |
オブジェクトの印字表現は、デフォルトでは、あまりユーザにやさしくありません。 |
| 540 |
Gauche の @code{write} および @code{display} 関数は、どのように印字して |
| 541 |
よいかわからないインスタンスにであうと、ジェネリック関数 @code{write-object} |
| 542 |
を呼びます。定義したクラスのインスタンスをどのように印字するかを |
| 543 |
カスタマイズするために、そのクラスに特定したメソッドを定義することができます。 |
| 544 |
|
| 545 |
@c COMMON |
| 546 |
@example |
| 547 |
(define-method write-object ((pt <2d-point>) port) |
| 548 |
(format port "[[~a, ~a]]" (x-of pt) (y-of pt))) |
| 549 |
|
| 550 |
(define-method write-object ((vec <2d-vector>) port) |
| 551 |
(format port "<<~a, ~a>>" (x-of vec) (y-of vec))) |
| 552 |
@end example |
| 553 |
|
| 554 |
@c EN |
| 555 |
And what you'll get is: |
| 556 |
@c JP |
| 557 |
で、どうなったかというと、 |
| 558 |
@c COMMON |
| 559 |
@example |
| 560 |
gosh> a-point |
| 561 |
[[0.0, 3.33]] |
| 562 |
gosh> d-vector |
| 563 |
<<-9.0, 7.25>> |
| 564 |
@end example |
| 565 |
|
| 566 |
@c EN |
| 567 |
If you customize the printed representation to conform SRFI-10 |
| 568 |
format, and define a corresponding read-time constructor, |
| 569 |
you can make your instances to be written-out and read-back |
| 570 |
just like built-in objects. See @ref{Read-time constructor} for |
| 571 |
the details. |
| 572 |
|
| 573 |
Several built-in functions have similar way to extend their |
| 574 |
functionality for user-defined objects. For example, if |
| 575 |
you specialize a generic function @code{object-equal?}, |
| 576 |
you can compare the instances by @code{equal?}: |
| 577 |
@c JP |
| 578 |
SRFI-10 のフォーマットを満すように印字表現をカスタマイズし、読み込み時 |
| 579 |
構築子を定義すれば、自分の定義したクラスのインスタンスを、組み込み |
| 580 |
オブジェクトと同様に書き出し、読み戻しできます。詳しくは |
| 581 |
@ref{Read-time constructor} を見てください。 |
| 582 |
|
| 583 |
いくつかの組み込み関数はユーザ定義オブジェクトに対して同様の方法で |
| 584 |
機能拡張できます。たとえば、@code{object-equal?} を特定化すれば、 |
| 585 |
@code{equal?} を使ってインスタンスの比較ができます。 |
| 586 |
|
| 587 |
@c COMMON |
| 588 |
@example |
| 589 |
(define-method object-equal? ((a <2d-point>) (b <2d-point>)) |
| 590 |
(and (equal? (x-of a) (x-of b)) |
| 591 |
(equal? (y-of a) (y-of b)))) |
| 592 |
|
| 593 |
(equal? (make <2d-point> :x 1 :y 2) (make <2d-point> :x 1 :y 2)) |
| 594 |
@result{} #t |
| 595 |
|
| 596 |
(equal? (make <2d-point> :x 1 :y 2) (make <2d-point> :x 2 :y 1)) |
| 597 |
@result{} #f |
| 598 |
|
| 599 |
(equal? (make <2d-point> :x 1 :y 2) 'a) |
| 600 |
@result{} #f |
| 601 |
|
| 602 |
(equal? (list (make <2d-point> :x 1 :y 2) |
| 603 |
(make <2d-point> :x 3 :y 4)) |
| 604 |
(list (make <2d-point> :x 1 :y 2) |
| 605 |
(make <2d-point> :x 3 :y 4))) |
| 606 |
@result{} #t |
| 607 |
@end example |
| 608 |
|
| 609 |
@c EN |
| 610 |
Let's proceed to more interesting examples. |
| 611 |
Think of a class @code{<shape>}, |
| 612 |
which is an entity that can be drawn. |
| 613 |
As a base class, it keeps |
| 614 |
common attributes such as a color and line thickness in its slots. |
| 615 |
@c JP |
| 616 |
もっとおもしろい例を見てみましょう。描画可能な @code{<shape>} というクラス |
| 617 |
を考えます。 |
| 618 |
基本クラスとして、色や線の太さといった共通属性をスロットに持たせます。 |
| 619 |
|
| 620 |
@c COMMON |
| 621 |
@example |
| 622 |
(define-class <shape> () |
| 623 |
((color :init-value '(0 0 0) :init-keyword :color) |
| 624 |
(thickness :init-value 2 init-keyword :thickness))) |
| 625 |
@end example |
| 626 |
|
| 627 |
@c EN |
| 628 |
When an instance is created, @code{make} calls a generic function |
| 629 |
@code{initialize}, which takes care of initializing slots |
| 630 |
such as processing init-keywords and init-values. |
| 631 |
You can customize the initialization behavior by specializing |
| 632 |
the @code{initialize} method. The @code{initialize} method |
| 633 |
is called with two arguments, one is a newly created instance, |
| 634 |
and another is a list of arguments passed to @code{make}. |
| 635 |
|
| 636 |
We define a @code{initialize} method for @code{<shape>} class, |
| 637 |
so that the created shape will be automatically recorded in a global |
| 638 |
list. Note that we don't want to replace system's |
| 639 |
@code{initialize} behavior completely, |
| 640 |
since we still need the init-keywords to be handled. |
| 641 |
|
| 642 |
@example |
| 643 |
(define *shapes* '()) ;; global shape list |
| 644 |
|
| 645 |
(define-method initialize ((self <shape>) initargs) |
| 646 |
(next-method) ;; let the system to handle slot initialization |
| 647 |
(push! *shapes* self)) ;; record myself to the global list |
| 648 |
@end example |
| 649 |
@c JP |
| 650 |
インスタンスを生成する際、@code{make} はジェネリック関数 @code{initialize} |
| 651 |
を呼びます。この関数は init-keyword や init-value を処理するようなスロットの |
| 652 |
面倒を見ます。この @code{initialize} メソッドを特定化することによって、初期化 |
| 653 |
の振舞いをカスタマイズすることができます。@code{initialize} メソッドは二つの |
| 654 |
引数とともに呼ばれます。ひとつは新しく生成されたインスタンス、もうひとつは |
| 655 |
@code{make} にわたされた引数のリストです。 |
| 656 |
|
| 657 |
@code{initialize} メソッドを @code{<shape>} クラス用に定義し、生成された |
| 658 |
shape が自動的にグローバルなリストに登録されるようにしましょう。 |
| 659 |
システムのもつ @code{initialize} の振舞いを完全に置き換えたくはないという |
| 660 |
ことに注意してください。init-keyword は相変わらず扱う必要があるからです。 |
| 661 |
|
| 662 |
@example |
| 663 |
(define *shapes* '()) ;; グローバルな shape のリスト |
| 664 |
|
| 665 |
(define-method initialize ((self <shape>) initargs) |
| 666 |
(next-method) ;; このシステムがスロットの初期化処理をするようにする |
| 667 |
(push! *shapes* self)) ;; 自分自身をグローバルなリストに登録する |
| 668 |
@end example |
| 669 |
|
| 670 |
@c EN |
| 671 |
The trick is a special method, @code{next-method}. It can only be |
| 672 |
used inside a method body, and calls @emph{less specific method} |
| 673 |
of the same generic function---typically, it means you call the |
| 674 |
same method of superclass. |
| 675 |
Most object-oriented languages have the concept of calling |
| 676 |
superclass's method. Because of multiple-argument |
| 677 |
dispatching and multiple inheritance, @code{next-method} is |
| 678 |
a little bit more complicated, but the basic idea is the same. |
| 679 |
|
| 680 |
So, what's the superclass of @code{<shape>}? In fact, all |
| 681 |
Scheme-defined class inherits a class called @code{<object>}. |
| 682 |
And it is @code{<object>}'s initialize method which takes care |
| 683 |
of slot initialization. After calling @code{next-method} |
| 684 |
within your @code{initialize} method, you can assume all |
| 685 |
the slots are properly initialized. So it is generally the |
| 686 |
first thing in your @code{initialize} method to call @code{next-method}. |
| 687 |
|
| 688 |
Let's inspect the above code. When you call |
| 689 |
@code{(make <shape> args @dots{})}, the system allocates |
| 690 |
memory for an instance of @code{<shape>}, and calls |
| 691 |
@code{initialize} generic function with the instance and |
| 692 |
@code{args @dots{}}. It is dispatched to the @code{initialize} |
| 693 |
method you just defined. In it, you call @code{next-method}, |
| 694 |
which in turn calls @code{<object>} class's @code{initialize} |
| 695 |
method. It initializes the instance with init-values and init-keywords. |
| 696 |
After it returns, you register the new @code{<shape>} instance |
| 697 |
to the global shape list @code{*shapes*}. |
| 698 |
|
| 699 |
The @code{<shape>} class represents just an abstract concept of |
| 700 |
shape. Now we define some concrete drawable shapes, by |
| 701 |
@emph{subclassing} the @code{<shape>} class. |
| 702 |
@c JP |
| 703 |
仕掛けは、特別なメソッド @code{next-method} にあります。これは |
| 704 |
メソッド本体の中でしか使えません。同じジェネリック関数の |
| 705 |
@emph{特定化の度合がより低いメソッド}を呼びます。これは、典型的には |
| 706 |
スーパークラスの同じメソッドを呼ぶということを意味します。 |
| 707 |
ほとんどのオジェクト指向言語には、スーパークラスのメソッドを呼ぶという |
| 708 |
概念があります。多重引数ディスパッチと多重継承のために @code{next-method} |
| 709 |
は少し複雑にはなっていますが、基本的なアイディアは同じです。 |
| 710 |
|
| 711 |
さて、では @code{<shape>} のスーパークラスはなんでしょう。実は、 |
| 712 |
すべての Scheme で定義されたクラスは @code{<object>} というクラスを |
| 713 |
継承します。スロットのめんどうを見ているのは、@code{<object>} の |
| 714 |
初期化メソッドなのです。自分で定義した @code{initialize} メソッド中で |
| 715 |
@code{next-method} を呼びだしたあとでは、すべてのスロットが |
| 716 |
正しくイニシャライズされたとみなせます。というわけで、自分で定義した |
| 717 |
@code{initialize} のなかで通常、最初にすべきことは @code{next-method} |
| 718 |
を呼ぶことです。 |
| 719 |
|
| 720 |
上のコードを点検してみましょう。@code{(make <shape> args @dots{})} を呼ぶと、 |
| 721 |
システムは @code{<shape>} のインスタンスのためのメモリを確保し、 |
| 722 |
@code{initialize} ジェネリック関数を、そのインスタンスと @code{args @dots{}} |
| 723 |
で呼びます。これは、いま自分で定義した @code{initialize} にディスパッチ |
| 724 |
されます。その中で @code{next-method} よび、それがこんどは @code{<object>} |
| 725 |
クラスの @code{initialize} メソッドをよびます。これでこのインスタンスは |
| 726 |
init-value と init-keyword で初期化されます。@code{next-method} からもどった |
| 727 |
あと、新しい @code{<shape>} インスタンスをグローバルの shape リストに |
| 728 |
@code{*shapes*} に登録します。 |
| 729 |
|
| 730 |
この @code{<shape>} クラスは shape の抽象的概念を表現しているにすぎません。 |
| 731 |
では、いくつかの具体的な描画可能な shape を @code{<shape>} の |
| 732 |
@emph{サブクラス化}によって定義しましょう。 |
| 733 |
|
| 734 |
@c COMMON |
| 735 |
@example |
| 736 |
(define-class <point-shape> (<shape>) |
| 737 |
((point :init-form (make <2d-point>) :init-keyword :point))) |
| 738 |
|
| 739 |
(define-class <polyline-shape> (<shape>) |
| 740 |
((points :init-value '() :init-keyword :points) |
| 741 |
(closed :init-value #f :init-keyword :closed))) |
| 742 |
@end example |
| 743 |
|
| 744 |
@c EN |
| 745 |
Note the second argument passed to @code{define-class}. |
| 746 |
It indicates that @code{<point-shape>} and @code{<polyline-shape>} |
| 747 |
inherit slots of @code{<shape>} class, and also instances of |
| 748 |
those subclasses can be accepted wherever an instance of |
| 749 |
@code{<shape>} class is accepted. |
| 750 |
|
| 751 |
The @code{<point-shape>} adds one slot, @code{point}, which |
| 752 |
contains an instance of @code{<2d-point>} defined in the beginning |
| 753 |
of this section. The @code{<polyline-shape>} class stores |
| 754 |
a list of points, and a flag, which specifies whether the end |
| 755 |
point of the polyline is connected to its starting point or not. |
| 756 |
|
| 757 |
Inheritance is a powerful mechanism that should be used with care, |
| 758 |
or it easily result a code which is untractable |
| 759 |
("Object-oriented programming offers a sustainable way to |
| 760 |
write spaghetti code.", as Paul Graham says in his article |
| 761 |
"The Hundred-Year Language"). |
| 762 |
The rule of thumb is to make a subclass when you need a subtype. |
| 763 |
The inheritance of slots is just something that comes with, |
| 764 |
but it shouldn't be the main reason to do subclassing. |
| 765 |
You can always "include" the substructure, as is done in |
| 766 |
@code{<point-shape>} class. |
| 767 |
|
| 768 |
There appeared a new slot option in @code{<point-shape>} class. |
| 769 |
The @code{:init-form} slot option specifies the default value of |
| 770 |
the slot when init-keyword is not given to @code{make} method. |
| 771 |
However, unlike @code{:init-value}, with which the value is |
| 772 |
evaluated at the time the class is defined, |
| 773 |
the value with @code{:init-form} is evaluated when the system |
| 774 |
actually needs the value. So, in the @code{<point-shape>} instance, |
| 775 |
the default @code{<2d-point>} instance is only created if the |
| 776 |
@code{<point-shape>} instance is created without having @code{:point} |
| 777 |
init-keyword argument. |
| 778 |
|
| 779 |
A shape may be drawn in different formats for different devices. |
| 780 |
For now, we just consider a PostScript output. To make the @code{draw} |
| 781 |
method polymorphic, we define a postscript output device class, |
| 782 |
@code{<ps-device>}. |
| 783 |
|
| 784 |
@c JP |
| 785 |
@code{define-class} の第二引数に注目してください。これは |
| 786 |
@code{<point-shape>} および @code{<polyline-shape>} が @code{<shape>} |
| 787 |
クラスのスロットを継承していることを示しています。そして @code{<shape>} |
| 788 |
クラスが受け入れるものはすべて、それらのサブクラスでも受け入れらること |
| 789 |
も示しています。 |
| 790 |
|
| 791 |
@code{<point-shape>} には @code{point} というスロットがひとつ追加 |
| 792 |
されています。このスロットはこの節の最初で定義した、@code{<2d-point>} の |
| 793 |
インスタンスを持ちます。@code{<polyline-shape>} クラスは点のリスト、 |
| 794 |
フラグを格納します。フラグは多角形のラインが終点と始点つないでいるか |
| 795 |
どうかを指定します。 |
| 796 |
|
| 797 |
継承はとりあつかいに注意が必要な強力な機構です。うっかりするとすぐに |
| 798 |
追跡不能なコードができあがります。(Paul Graham は『百年の言語』という |
| 799 |
記事のなかで、「オブジェクト指向プログラミングはスパゲッティコードを |
| 800 |
書くための持続的な方法を提供してくれる。」と言っているように。) |
| 801 |
経験則からいうとサブタイプが必要なときにサブクラスを作るのがよいようです。 |
| 802 |
スロットの継承は付随するなにかではありますが、サブクラス化のための |
| 803 |
主要な理由にしてはいけません。@code{<point-shape>} クラスでやったように |
| 804 |
サブストラクチャを「インクルード」することは常にできるのです。 |
| 805 |
|
| 806 |
@code{<point-shape>} クラスに新しいスロットオプションがあらわれました。 |
| 807 |
@code{:init-form} というスロットオプションは、init-keyword が |
| 808 |
@code{make} に与えられなかったときのそのスロットのデフォルト値を指定します。 |
| 809 |
しかし、クラス定義時に評価される @code{:init-value} の値とはちがって、 |
| 810 |
この @code{:init-form} をともなう値はシステムが実際のその値を必要とした |
| 811 |
ときに評価されます。したがって、@code{<point-shape>} のインスタンス |
| 812 |
では、@code{<point-shape>} インスタンスが @code{:point} キーワード引数を |
| 813 |
わたされずに生成されたときにのみ、デフォルトの @code{<2d-point>} |
| 814 |
インスタンスが生成されます。 |
| 815 |
|
| 816 |
shape は別のデバイスに別の方法で描画され得ます。いまのところは、 |
| 817 |
PostScript 出力だけを考慮しましょう。@code{draw} メソッドに多相性を持たせる |
| 818 |
ために、PostScript 出力デバイス @code{<ps-device>} を定義します。 |
| 819 |
|
| 820 |
@c COMMON |
| 821 |
@example |
| 822 |
(define-class <ps-device> () ()) |
| 823 |
@end example |
| 824 |
|
| 825 |
@c EN |
| 826 |
Then we can write a @code{draw} method, specialized for |
| 827 |
both @code{<shape>} and @code{<ps-device>}. |
| 828 |
@c JP |
| 829 |
こうすると、@code{<shape>} および @code{<ps-device>} の両方に特定化した |
| 830 |
@code{draw} メソッドを書くことができます。 |
| 831 |
|
| 832 |
@c COMMON |
| 833 |
@example |
| 834 |
(define-method draw ((self <shape>) (device <ps-device>)) |
| 835 |
(format #t "gsave\n") |
| 836 |
(draw-path self device) |
| 837 |
(apply format #t "~a ~a ~a setrgbcolor\n" (ref self 'color)) |
| 838 |
(format #t "~a setlinewidth\n" (ref self 'thickness)) |
| 839 |
(format #t "stroke\n") |
| 840 |
(format #t "grestore\n")) |
| 841 |
@end example |
| 842 |
|
| 843 |
@c EN |
| 844 |
In this code, the @var{device} argument isn't |
| 845 |
used within the method body. It is just used for method dispatching. |
| 846 |
If we eventually have different output devices, we can add |
| 847 |
a @code{draw} method that is specialized for such devices. |
| 848 |
|
| 849 |
The above @code{draw} method does the common work, but actual |
| 850 |
drawing must be done in specialized way for each subclasses. |
| 851 |
@c JP |
| 852 |
このコードでは @var{device} 引数はメソッド本体内では使われていません。 |
| 853 |
メソッドディスパッチのためのみに使われます。いずれ別の出力デバイス |
| 854 |
を必要になったら、そのデバイスに特定化した @code{draw} メソッドを |
| 855 |
追加することができます。 |
| 856 |
|
| 857 |
上の @code{draw} メソッドは共通の仕事をしますが、実際の描画は |
| 858 |
それぞれのサブクラス用に特定化された方法で行わなければなりません。 |
| 859 |
|
| 860 |
@c COMMON |
| 861 |
@example |
| 862 |
(define-method draw-path ((self <point-shape>) (device <ps-device>)) |
| 863 |
(apply format #t "newpath ~a ~a 1 0 360 arc closepath\n" |
| 864 |
(point->list (ref self 'point)))) |
| 865 |
|
| 866 |
(define-method draw-path ((self <polyline-shape>) (device <ps-device>)) |
| 867 |
(let ((pts (ref self 'points))) |
| 868 |
(when (>= (length pts) 2) |
| 869 |
(format #t "newpath\n") |
| 870 |
(apply format #t "~a ~a moveto\n" (point->list (car pts))) |
| 871 |
(for-each (lambda (pt) |
| 872 |
(apply format #t "~a ~a lineto\n" (point->list pt))) |
| 873 |
(cdr pts)) |
| 874 |
(when (ref self 'closed) |
| 875 |
(apply format #t "~a ~a lineto\n" (point->list (car pts)))) |
| 876 |
(format #t "closepath\n")))) |
| 877 |
|
| 878 |
;; utility method |
| 879 |
(define-method point->list ((pt <2d-point>)) |
| 880 |
(list (x-of pt) (y-of pt))) |
| 881 |
@end example |
| 882 |
|
| 883 |
@c EN |
| 884 |
Finally, we do a little hack. Let @code{draw} method work on |
| 885 |
the list of shapes, so that we can draw multiple shapes within a page |
| 886 |
in batch. |
| 887 |
@c JP |
| 888 |
最後にもう少しだけ、ハックしましょう。@code{draw} メソッドを |
| 889 |
shape のリストに対しても動作するようにします。こうすれば、 |
| 890 |
同一ページ内で複数の shape をひつまとめで描画できます。 |
| 891 |
|
| 892 |
@c COMMON |
| 893 |
@example |
| 894 |
(define-method draw ((shapes <list>) (device <ps-device>)) |
| 895 |
(format #t "%%\n") |
| 896 |
(for-each (cut draw <> device) shapes) |
| 897 |
(format #t "showpage\n")) |
| 898 |
@end example |
| 899 |
|
| 900 |
@c EN |
| 901 |
Then we can write some simple figures @dots{}. |
| 902 |
@c JP |
| 903 |
これで簡単な図を書けるようになります。 |
| 904 |
|
| 905 |
@c COMMON |
| 906 |
@example |
| 907 |
(use math.const) ;; for constant pi |
| 908 |
|
| 909 |
(define (shape-sample) |
| 910 |
|
| 911 |
;; creates 5 corner points of pentagon |
| 912 |
(define (make-corners scale) |
| 913 |
(map (lambda (i) |
| 914 |
(let ((pt (make <2d-point>))) |
| 915 |
(move-by! pt (make-polar scale (* i 2/5 pi))) |
| 916 |
(move-by! pt 200 200) |
| 917 |
pt)) |
| 918 |
(iota 5))) |
| 919 |
|
| 920 |
(set! *shapes* '()) ;; clear the shape list |
| 921 |
(let* ((corners (make-corners 100))) |
| 922 |
;; a pentagon in green |
| 923 |
(make <polyline-shape> |
| 924 |
:color '(0 1 0) :closed #t |
| 925 |
:points corners) |
| 926 |
;; a star-shape in red |
| 927 |
(make <polyline-shape> |
| 928 |
:color '(1 0 0) :closed #t |
| 929 |
:points (list (list-ref corners 0) |
| 930 |
(list-ref corners 2) |
| 931 |
(list-ref corners 4) |
| 932 |
(list-ref corners 1) |
| 933 |
(list-ref corners 3))) |
| 934 |
;; put dots in each corner of the star |
| 935 |
(for-each (cut make <point-shape> :point <>) |
| 936 |
(make-corners 90)) |
| 937 |
;; draw the shapes |
| 938 |
(draw *shapes* (make <ps-device>))) |
| 939 |
) |
| 940 |
@end example |
| 941 |
|
| 942 |
@c EN |
| 943 |
The function @code{shape-sample} writes out a PostScript code of |
| 944 |
simple drawing to the current output port. You can write it out |
| 945 |
to file by the following expression, and then view the result |
| 946 |
by PostScript viewer such as GhostScript. |
| 947 |
@c JP |
| 948 |
関数 @code{shape-sample} は現在の出力ポートに簡単な PostScript の描画コード |
| 949 |
を書き出します。これを以下のような式でファイルに出力し、GhostScriptなどの |
| 950 |
PostScriptビューワで結果を見てください。 |
| 951 |
|
| 952 |
@c COMMON |
| 953 |
@example |
| 954 |
(with-output-to-file "oointro.ps" shape-sample) |
| 955 |
@end example |
| 956 |
|
| 957 |
@c ---------------------------------------------------------------------- |
| 958 |
@node Class, Instance, Introduction to the object system, Object system |
| 959 |
@section Class |
| 960 |
@c NODE クラス |
| 961 |
|
| 962 |
@c EN |
| 963 |
In this section, a class in Gauche is explained in detail. |
| 964 |
@c JP |
| 965 |
この節では、Gauche におけるクラスについて詳しく説明します。 |
| 966 |
@c COMMON |
| 967 |
|
| 968 |
@menu |
| 969 |
* Defining class:: |
| 970 |
* Inheritance:: |
| 971 |
* Class object:: |
| 972 |
* Slot definition object:: |
| 973 |
* Class redefinition:: |
| 974 |
* Class definition examples:: |
| 975 |
@end menu |
| 976 |
|
| 977 |
@node Defining class, Inheritance, Class, Class |
| 978 |
@subsection Defining class |
| 979 |
@c NODE クラスの定義 |
| 980 |
|
| 981 |
@c EN |
| 982 |
To define a class, use a macro @code{define-class}. |
| 983 |
@c JP |
| 984 |
クラスを定義するには、@code{define-class} マクロを使います。 |
| 985 |
@c COMMON |
| 986 |
|
| 987 |
@defmac define-class name supers (slot-spec @dots{}) option @dots{} |
| 988 |
@c EN |
| 989 |
Creates a class object according to the arguments, and globally |
| 990 |
bind it to a variable @var{name}. This macro should be used at toplevel. |
| 991 |
|
| 992 |
@var{Supers} is a list of direct superclasses from which this class |
| 993 |
inherits. You can use multiple inheritance. |
| 994 |
All Scheme-defined classes implicitly inherits @code{<object>}. |
| 995 |
It is implicitly added to the right of @var{supers} list, so you |
| 996 |
don't need to specify it. |
| 997 |
See @ref{Inheritance}, for the details about inheritance. |
| 998 |
|
| 999 |
@var{Slot-spec} is a specification of a "slot", sometimes known as |
| 1000 |
a "field" or an "instance variable" (but you can specify "class variable" |
| 1001 |
in @var{slot-spec} as well). |
| 1002 |
The simplest form of @var{slot-spec} is just a symbol, which names |
| 1003 |
the slot. Or you can give a list, whose first element is a symbol |
| 1004 |
and whose rest is an interleaved list of keywords and values. |
| 1005 |
The list form not only defines a name of the slot but specifies |
| 1006 |
behavior of the slot. It is explained below. |
| 1007 |
|
| 1008 |
Finally, @var{option} @dots{} is an interleaved list of keywords |
| 1009 |
and values, specifies how class object should be created. |
| 1010 |
This macro recognizes one keyword, @code{:metaclass}, whose |
| 1011 |
corresponding value is used for metaclass (class that instantiates |
| 1012 |
another class). Other options are passed to the @code{make} |
| 1013 |
method to create the class object. @xref{Class instantiation}, |
| 1014 |
for the usage of metaclass. |
| 1015 |
@c JP |
| 1016 |
引数によって指定されたクラスオブジェクトを作成し、それを @var{name} に |
| 1017 |
グローバルに束縛します。このマクロはトップレベルでのみ使うことができます。 |
| 1018 |
|
| 1019 |
@var{Supers} はそのクラスが継承する直接のスーパークラスのリストです。 |
| 1020 |
多重継承も使えます。継承の詳細については@ref{Inheritance} を参照して下さい。 |
| 1021 |
|
| 1022 |
@var{Slot-spec} は「スロット」の仕様で、他の言語ではよく |
| 1023 |
「フィールド」や「インスタンス変数」と呼ばれるものです |
| 1024 |
(@var{slot-spec} を使って「クラス変数」を指定することもできます)。 |
| 1025 |
@var{slot-spec} の最も単純なフォームはシンボルそのもので、その名前が |
| 1026 |
スロットであるものです。あるいは、最初の要素がシンボルで残りの要素が |
| 1027 |
キーワードと値が交互に来るリストを渡すこともできます。 |
| 1028 |
|
| 1029 |
このリストフォームは、スロットの名前を定義するだけでなく、そのスロットの |
| 1030 |
振る舞いも定義します。スロットの定義については以下で説明します。 |
| 1031 |
|
| 1032 |
最後に、@var{option} @dots{} は、クラスオブジェクトがどのように |
| 1033 |
作られるかを指定する、キーワードと値が交互に来るリストです。 |
| 1034 |
|
| 1035 |
このマクロでは1つのキーワード引数、@code{:metaclass} により、 |
| 1036 |
メタクラス(他のクラスをインスタンス化するクラス)を指定できます。 |
| 1037 |
他のオプションはクラスオブジェクトを作成するために、@code{make} |
| 1038 |
メソッドに渡されます。メタクラスの使用方法については、 |
| 1039 |
@ref{Class instantiation}を参照。 |
| 1040 |
@c COMMON |
| 1041 |
@end defmac |
| 1042 |
|
| 1043 |
@c EN |
| 1044 |
If a slot specification is a list, it should be in the following form: |
| 1045 |
@c JP |
| 1046 |
スロットの指定はリストで、以下のようなフォームであるべきです。 |
| 1047 |
@c COMMON |
| 1048 |
|
| 1049 |
@example |
| 1050 |
(@var{slot-name} :option1 value1 :option2 value2 @dots{}) |
| 1051 |
@end example |
| 1052 |
|
| 1053 |
@c EN |
| 1054 |
Each keyword (@code{option1} etc.) gives a @emph{slot option}. |
| 1055 |
By default, the following slot options are recognized. |
| 1056 |
You can add more slot options by defining metaclass. |
| 1057 |
|
| 1058 |
@table @code |
| 1059 |
|
| 1060 |
@item :allocation |
| 1061 |
Specifies an allocation type of this slot, which specifies how the |
| 1062 |
value for this slot is stored. The following keyword values are |
| 1063 |
recognized by the standard class. A programmer can define his own |
| 1064 |
metaclass to extend the class to recognize other allocation types. |
| 1065 |
|
| 1066 |
@table @code |
| 1067 |
@item :instance |
| 1068 |
A slot is allocated for each instance, so that every instance can have |
| 1069 |
distinct value. This realizes so-called "instance variable" behavior. |
| 1070 |
If @code{:allocation} slot option is omitted, this is the default. |
| 1071 |
@item :class |
| 1072 |
A slot is allocated in this class object, so that every instance will |
| 1073 |
share the same value for this slot. This realizes so-called |
| 1074 |
"class variable" behavior. The slot value is also shared by all |
| 1075 |
subclasses (unless a subclass definition shadows the slot). |
| 1076 |
@item :each-subclass |
| 1077 |
Similar to @code{class} allocation, but a slot is allocated for each |
| 1078 |
class; that is, it is shared by every instance of the class, but |
| 1079 |
not shared by the instances of its subclasses. |
| 1080 |
@item :virtual |
| 1081 |
No storage is allocated for this type of slot. Accessing the slot |
| 1082 |
calls procedures given in @code{:slot-ref} and @code{:slot-set!} options |
| 1083 |
described below. In other words, you can make a procedural slot. |
| 1084 |
If a slot's allocation is specified as virtual, at least |
| 1085 |
@code{:slot-ref} option has to be specified as well, or @code{define-class} |
| 1086 |
raises an error. |
| 1087 |
@item :builtin |
| 1088 |
This type of allocation only appears in built-in classes, and |
| 1089 |
you can't specify it in Scheme-defined class. |
| 1090 |
@end table |
| 1091 |
|
| 1092 |
@item :init-keyword |
| 1093 |
A keyword value given to this slot option can be used to pass an |
| 1094 |
initial value to @code{make} method when an instance is created. |
| 1095 |
|
| 1096 |
@item :init-value |
| 1097 |
Gives an initial value of the slot, if the slot is not initialized |
| 1098 |
by the keyword argument at the creation time. The value is |
| 1099 |
evaluated when @code{define-class} is evaluated. |
| 1100 |
|
| 1101 |
@item :init-form |
| 1102 |
Like @code{init-value}, but the value given is wrapped in a thunk, |
| 1103 |
and evaluated each time when the value is required. |
| 1104 |
If both @code{init-value} and @code{init-form} are given, @code{init-form} |
| 1105 |
is ignored. Actually, @code{:init-form @var{expr}} is converted |
| 1106 |
to @code{:init-thunk (lambda () @var{expr})} by @code{define-class} macro. |
| 1107 |
|
| 1108 |
@item :initform |
| 1109 |
A synonym of @code{init-form}. This is kept for compatibility to |
| 1110 |
STk, and shouldn't be used in the new code. |
| 1111 |
|
| 1112 |
@item :init-thunk |
| 1113 |
Gives a thunk, which will be evaluated to obtain an initial value |
| 1114 |
of the slot, if the slot is not initialized by the keyword argument |
| 1115 |
at the creation time. To give a value to @code{:init-form} |
| 1116 |
is equivalent to give @code{(lambda () value)} to @code{:init-thunk}. |
| 1117 |
|
| 1118 |
@item :immutable |
| 1119 |
If not false, this slot is immutable. To be precise, you can set the |
| 1120 |
slot value only once, which usually happens at instance initializaiton. |
| 1121 |
The slot setter raises an error unless the slot is previously unbound. |
| 1122 |
If the slot is left unbound by @code{initialize} method, |
| 1123 |
it can be set later. It is regarded as a delayed initialization. |
| 1124 |
|
| 1125 |
@item :getter |
| 1126 |
Takes a symbol, and a getter method is created and bound to the |
| 1127 |
generic function of that name. The getter method takes an instance |
| 1128 |
of the class and returns the value of the slot. |
| 1129 |
|
| 1130 |
@item :setter |
| 1131 |
Takes a symbol, and a setter method is created and bound to |
| 1132 |
the generic function of that name. The setter method takes an instance |
| 1133 |
of the class and a value, and sets the value to the slot of the instance. |
| 1134 |
|
| 1135 |
@item :accessor |
| 1136 |
Takes a symbol, and create two methods; a getter method and a setter method. |
| 1137 |
A getter method is bound to the generic function of the given name, |
| 1138 |
and a setter method is added as the @emph{setter} of that generic function |
| 1139 |
(see @ref{Assignments} for generic setters). |
| 1140 |
|
| 1141 |
@item :slot-ref |
| 1142 |
Specifies a value that evaluates to a procedure which takes one argument, |
| 1143 |
an instance. This slot option must be specified if the allocation of |
| 1144 |
the slot is @code{virtual}. Whenever a program tries to get the value |
| 1145 |
of the slot, either using @code{slot-ref} or the getter method, |
| 1146 |
the specified procedure is called, and its result is returned as |
| 1147 |
the value of the slot. The procedure can return an undef value |
| 1148 |
(the return value of @code{undefined}) to indicate the slot doesn't |
| 1149 |
have a value. |
| 1150 |
If the slot allocation is not @code{virtual} this slot option is ignored. |
| 1151 |
|
| 1152 |
@item :slot-set! |
| 1153 |
Specifies a value that evaluates to a procedure which takes two arguments, |
| 1154 |
an instance and a value. Whenever a program tries |
| 1155 |
to set the value of the slot, either using @code{slot-set!} or the |
| 1156 |
setter method, the specified procedure is called with the value to be set. |
| 1157 |
If the slot allocation is not @code{virtual} this slot option is ignored. |
| 1158 |
If this option isn't given to a virtual slot, the slot becomes read-only. |
| 1159 |
|
| 1160 |
@item :slot-bound? |
| 1161 |
Specifies a value that evaluates to a procedure which takes one argument, |
| 1162 |
an instance. This slot option is only meaningful when the slot allocation |
| 1163 |
is @code{virtual}. Whenever a program tries to determine whether |
| 1164 |
the slot has a value, this procedure is called. It should return |
| 1165 |
a true value if the slot has a value, or @code{#f} otherwise. |
| 1166 |
If this slot option is omitted for a virtual slot, the system |
| 1167 |
calls the procedure given to @code{slot-ref} instead, and see whether |
| 1168 |
its return value is @code{#<undef>} or not. |
| 1169 |
@end table |
| 1170 |
@c JP |
| 1171 |
各キーワード(@code{option1} など) は @emph{slot option} を与えます。 |
| 1172 |
デフォルトでは、以下のスロットオプションが認識されます。 |
| 1173 |
メタクラスを定義することで、デフォルト以外のスロットオプションを |
| 1174 |
追加できます。 |
| 1175 |
|
| 1176 |
@table @code |
| 1177 |
|
| 1178 |
@item :allocation |
| 1179 |
このスロットのアロケーションタイプを指定します。これは、このスロットが |
| 1180 |
どのように値を格納するかを指定します。以下のようなキーワード値が |
| 1181 |
標準クラスによって認識されます。プログラマは、自分用のメタクラスを定義し、 |
| 1182 |
これら以外のアロケーションタイプを認識するように、このクラスを拡張すること |
| 1183 |
ができます。 |
| 1184 |
|
| 1185 |
@table @code |
| 1186 |
@item :instance |
| 1187 |
スロットは各インスタンス毎にアロケートされます。したがって、おのおのの |
| 1188 |
インスタンスは別々の値をもてます。これは、いわゆる「インスタンス |
| 1189 |
変数」の振舞いを実現します。@code{:allocation} スロットオプションが |
| 1190 |
省略された場合、これがデフォルトとなります。 |
| 1191 |
@item :class |
| 1192 |
スロットはクラスオブジェクト自身にアロケートされます。したがって、おのおのの |
| 1193 |
インスタンスはこのスロットの同じ値を共有します。これは、いわゆる |
| 1194 |
「クラス変数」の振舞いを実現します。このスロットの値は、すべてのサブクラス |
| 1195 |
でも共有されます。(ただし、サブクラスの定義がこのスロットをシャドウする |
| 1196 |
場合には、そのかぎりではありません。) |
| 1197 |
@item :each-subclass |
| 1198 |
@code{class} アロケーションと似ていますが、スロットはクラス毎にアロケート |
| 1199 |
されます。すなわち、このスロットは、このクラスのすべてのインスタンスで |
| 1200 |
共有されますが、サブクラスのインスタンスには共有されません。 |
| 1201 |
@item :virtual |
| 1202 |
このタイプのスロット用には格納領域はアロケートされません。このスロット |
| 1203 |
にアクセスすると以下で説明する @code{:slot-ref} および @code{:slot-set!} |
| 1204 |
オプションで与えられた手続きが呼ばれます。いいかえれば、手続きスロットを |
| 1205 |
作成できるということです。スロットアロケーションが virtual と指定されて |
| 1206 |
いる場合、少なくとも @code{:slot-ref} オプションが同時に |
| 1207 |
指定されていなければなりません。さもなければ、@code{define-class} は |
| 1208 |
エラーを発生させます。 |
| 1209 |
@item :builtin |
| 1210 |
このアロケーションタイプは組み込みクラスの中だけに現れます。 |
| 1211 |
Scheme 定義のクラスでこのタイプを指定することはできません。 |
| 1212 |
@end table |
| 1213 |
|
| 1214 |
@item :init-keyword |
| 1215 |
このスロットオプションに与えられたキーワード値は、インスタンスが生成 |
| 1216 |
される際に @code{make} メソッドに初期値をわたすために使えます。 |
| 1217 |
|
| 1218 |
@item :init-value |
| 1219 |
生成時にキーワード引数で初期化されてないスロットの場合、これによって |
| 1220 |
スロットの初期値を与えます。その値は @code{define-class} が評価される |
| 1221 |
ときに、評価されます。 |
| 1222 |
|
| 1223 |
@item :init-form |
| 1224 |
@code{init-value} と似ていますが、与えられた値は thunk で包まれていて、 |
| 1225 |
その値が必要とされた時に毎回評価されます。@code{init-value} と |
| 1226 |
@code{init-form} との両方が与えられた時には @code{init-form} が無視されます。 |
| 1227 |
実際には、@code{:init-form @var{expr}} は @code{define-class} マクロの |
| 1228 |
@code{:init-thunk (lambda () @var{expr})} に変換されます。 |
| 1229 |
|
| 1230 |
@item :initform |
| 1231 |
@code{init-form} と同義です。STk との互換性のためにあります。新しくコードを |
| 1232 |
書く場合には使うべきではありません。 |
| 1233 |
|
| 1234 |
@item :init-thunk |
| 1235 |
thunk を与えます。もし、当該スロットが生成時にキーワード引数によって |
| 1236 |
初期化されていなければ、その thunk を評価して当該スロットの初期値とします。 |
| 1237 |
@code{:init-form} に @code{value} を与えることと、@code{:init-thunk} |
| 1238 |
に @code{(lambda () value)} を与えることは同じことです。 |
| 1239 |
|
| 1240 |
@item :immutable |
| 1241 |
偽でない場合、このスロットは変更不可となります。 |
| 1242 |
正確には、このスロットには一回だけ値をセットできます。通常それは初期化時に行われます。 |
| 1243 |
スロットのセッターは、スロットが未束縛であった場合のみ成功し、そうでなければ |
| 1244 |
エラーを投げます。 |
| 1245 |
もし@code{initialize}メソッド内でスロットの初期化が行われなかった場合は、 |
| 1246 |
後で一回だけスロットに値をセットできます。これは遅延された初期化とみなせます。 |
| 1247 |
|
| 1248 |
@item :getter |
| 1249 |
シンボルをとり、getter メソッドを生成し、同じ名前のジェネリック関数に |
| 1250 |
束縛します。getter メソッドは当該クラスのインスタンスを引数とし、当該 |
| 1251 |
スロットの値を返します。 |
| 1252 |
|
| 1253 |
@item :setter |
| 1254 |
シンボルをとり、setter メソッドを生成し、同じ名前のジェネリック関数に |
| 1255 |
束縛します。setter メソッドは当該クラスのインスタンスと値をひとつ引数 |
| 1256 |
として、そのインスタンスの当該スロットの値をその値にセットします。 |
| 1257 |
|
| 1258 |
@item :accessor |
| 1259 |
シンボルをとり、2 つのメソッド(getter メソッドと setter メソッド)を |
| 1260 |
生成します。getter メソッドは与えられた名前のジェネリック関数に |
| 1261 |
束縛され、setter メソッドは、与えられた名前のジェネリック |
| 1262 |
関数のsetterとして追加されます(setter については @ref{Assignments} |
| 1263 |
を参照して下さい)。 |
| 1264 |
|
| 1265 |
@item :slot-ref |
| 1266 |
評価すると引数(インスタンス)を一つとる手続きとなる値を指定します。 |
| 1267 |
このスロットオプションは当該スロットのアロケーションが @code{virtual} |
| 1268 |
である場合、必ず指定されていなければなりません。プログラムが |
| 1269 |
@code{slot-ref}やgetter メソッドを使って |
| 1270 |
当該スロットの値を得ようとすると、 |
| 1271 |
このオプションに指定された手続きが呼ばれ、その結果が |
| 1272 |
当該スロットの値として返されます。手続きは undef 値を返し |
| 1273 |
(@code{undefined}の戻り値)、スロットが値をもっていないことを示す |
| 1274 |
ことができます。もしスロットのアロケーションが @code{virtual} で |
| 1275 |
なければ、このスロットオプションは無視されます。 |
| 1276 |
|
| 1277 |
@item :slot-set! |
| 1278 |
評価すると二つの引数(インスタンスと値)をとる手続きとなる値を指定します。 |
| 1279 |
プログラムが@code{slot-set!} あるいは setter メソッドを使って |
| 1280 |
当該スロットに値をセットしようとするときに、 |
| 1281 |
指定した手続きがインスタンスとセットすべき値を引数として |
| 1282 |
呼ばれます。スロットアロケーションが |
| 1283 |
@code{virtual}でなければ、このスロットオプションは無視されます。 |
| 1284 |
このスロットオプションの無いvirtualスロットはリードオンリースロットとなります。 |
| 1285 |
|
| 1286 |
@item :slot-bound? |
| 1287 |
評価すると引数(インスタンス)を一つとる手続きとなる値を指定します。 |
| 1288 |
このスロットオプションは当該スロットのアロケーションが @code{virtual} |
| 1289 |
である場合しか意味を持ちません。 |
| 1290 |
プログラムが当該スロットが値を持っているかどうかを決定しようとしたときに、 |
| 1291 |
この手続きを呼ばれます。手続きは、スロットが値をもつなら、真の値を、 |
| 1292 |
そうでなければ@code{#f} を返します。仮想スロットに対して、このスロット |
| 1293 |
オプションが省略されると、システムは代りに @code{slot-ref} に与えられ |
| 1294 |
た手続きを呼び、それが、@code{#<undef>} を返すかどうか見ます。 |
| 1295 |
@end table |
| 1296 |
@c COMMON |
| 1297 |
|
| 1298 |
@node Inheritance, Class object, Defining class, Class |
| 1299 |
@subsection Inheritance |
| 1300 |
@c NODE 継承 |
| 1301 |
|
| 1302 |
@c EN |
| 1303 |
Inheritance has two roles. First, you can @emph{extend} the existing |
| 1304 |
class by adding more slots. Second, you can @emph{specialize} the |
| 1305 |
methods related to the existing class so that those methods will |
| 1306 |
do a little more specific task than the original methods. |
| 1307 |
@c JP |
| 1308 |
継承にはふたつの役割があります。第一に、スロットを追加することで |
| 1309 |
既存のクラスを@emph{拡張}できます。第二には、既存のクラス |
| 1310 |
関連するメソッドを@emph{特定化}して、元々のメソッドよりもすこし |
| 1311 |
特定化した仕事をやらせるようにできます。 |
| 1312 |
@c COMMON |
| 1313 |
|
| 1314 |
@c EN |
| 1315 |
Let's define some terms. When a class @code{<T>} inherits a |
| 1316 |
class @code{<S>}, we call @code{<T>} a @emph{subclass} of @code{<S>}, |
| 1317 |
and @code{<S>} a @emph{superclass} of @code{<T>}. This relation |
| 1318 |
is transitive: @code{<T>}'s subclasses are also @code{<S>}'s subclasses, |
| 1319 |
and @code{<S>}'s superclasses are also @code{<T>}'s superclasses. |
| 1320 |
Specifically, if @code{<T>} directly inherits @code{<S>}, that is, |
| 1321 |
@code{<S>} appeared in the superclass list when @code{<T>} is defined, |
| 1322 |
then @code{<S>} is a @emph{direct superclass} of @code{<T>}, |
| 1323 |
and @code{<T>} is a @emph{direct subclass} of @code{<S>}. |
| 1324 |
@c JP |
| 1325 |
いくつかの用語を定義しておきましょう。クラス @code{<T>} がクラス |
| 1326 |
@code{<S>}を継承しているとき、@code{<T>} を @code{<S>} の |
| 1327 |
@emph{サブクラス}といい、@code{<S>} を @code{<T>} の |
| 1328 |
@emph{スーパークラス}といいます。この関係は推移的です。すなわち、 |
| 1329 |
@code{<T>} のサブクラスは、やはり @code{<S>} のサブクラスであり、 |
| 1330 |
@code{<S>} のスーパークラスは、やはり @code{<T>} のスーパークラスです。 |
| 1331 |
特に、@code{<T>} が @code{<S>} を直接継承している場合、すなわち、 |
| 1332 |
@code{<S>} が @code{<T>} を定義する際のスーパークラスリストに |
| 1333 |
現われている場合には @code{<S>} を @code{<T>} の |
| 1334 |
@emph{直接スーパークラス}といい、@code{<T>} を @code{<S>} の |
| 1335 |
@emph{直接サブクラス}といいます。 |
| 1336 |
@c COMMON |
| 1337 |
|
| 1338 |
@c EN |
| 1339 |
When a class is defined, it and its superclasses are ordered from |
| 1340 |
subclasses to superclasses, and a list of classes is created in such |
| 1341 |
order. It is called @emph{class precedence list}, or CPL. |
| 1342 |
Every class has its own CPL. |
| 1343 |
A CPL of a class always begins with the class itself, and ends with |
| 1344 |
@code{<top>}. |
| 1345 |
|
| 1346 |
You can query a class's CPL by a procedure @code{class-precedence-list}: |
| 1347 |
@c JP |
| 1348 |
クラスを定義したとき、そのクラスとそのスーパークラスは、サブクラスから |
| 1349 |
スーパークラスという順序になり、クラスのリストがその順で生成されます。 |
| 1350 |
このリストのことを@emph{クラス順位リスト}、あるいは CPL といいます。 |
| 1351 |
すべてのクラスはそれぞれ自身の CPL を持っています。 |
| 1352 |
クラスの CPL は常に自分自身からはじまり、@code{<top>} で終ります。 |
| 1353 |
|
| 1354 |
手続き @code{class-precedence-list} を用いてクラスの CPL を問い合わせ |
| 1355 |
ることができます。 |
| 1356 |
@c COMMON |
| 1357 |
|
| 1358 |
@example |
| 1359 |
gosh> (class-precedence-list <boolean>) |
| 1360 |
(#<class <boolean>> #<class <top>>) |
| 1361 |
gosh> (class-precedence-list <string>) |
| 1362 |
(#<class <string>> #<class <sequence>> #<class <collection>> #<class <top>>) |
| 1363 |
@end example |
| 1364 |
|
| 1365 |
@c EN |
| 1366 |
As you see, all classes inherits a class named @code{<top>}. |
| 1367 |
Some built-in classes have several abstract classes in its CPL |
| 1368 |
between itself and @code{<top>}; the above example shows @code{<string>} |
| 1369 |
class inherits @code{<sequence>} and @code{<collection>}. |
| 1370 |
That means a string can behave both as a sequence and a collection. |
| 1371 |
@c JP |
| 1372 |
見るとわかるように、すべてのクラスは @code{<top>} という名前のクラスを |
| 1373 |
継承しています。組み込みクラスには、いくつかの抽象クラスを CPL 中で |
| 1374 |
自分自身と @code{<top>} の間に連ねているものもあります。上の例では |
| 1375 |
@code{<string>} クラスは @code{<sequence>} と @code{<collection>} を |
| 1376 |
継承しています。これは、文字列がシーケンスとしても、コレクションとして |
| 1377 |
も振舞うことができるということです。 |
| 1378 |
@c COMMON |
| 1379 |
|
| 1380 |
@example |
| 1381 |
gosh> (is-a? "abc" <string>) |
| 1382 |
#t |
| 1383 |
gosh> (is-a? "abc" <sequence>) |
| 1384 |
#t |
| 1385 |
gosh> (is-a? "abc" <collection>) |
| 1386 |
#t |
| 1387 |
@end example |
| 1388 |
|
| 1389 |
@c EN |
| 1390 |
How about inheritance of Scheme-defined classes? |
| 1391 |
If there's only single inheritance, its CPL is straightforward: |
| 1392 |
you can just follow the class's super, its super's super, its |
| 1393 |
super's super's super, @dots{}, until you reach @code{<top>}. |
| 1394 |
See the example: |
| 1395 |
@c JP |
| 1396 |
Schemeで定義したクラスの継承についてはどうでしょう。 |
| 1397 |
単一継承なら、CPL は直截的です。そのクラスのスーパークラス、 |
| 1398 |
スーパークラスのスーパークラス、 |
| 1399 |
スーパークラスのスーパークラスのスーパークラス、@dots{} と |
| 1400 |
@code{<top>} に到達するまで、たどっていけます。例を見てください。 |
| 1401 |
@c COMMON |
| 1402 |
|
| 1403 |
@example |
| 1404 |
gosh> (define-class <a> () ()) |
| 1405 |
<a> |
| 1406 |
gosh> (define-class <b> (<a>) ()) |
| 1407 |
<b> |
| 1408 |
gosh> (class-precedence-list <b>) |
| 1409 |
(#<class <b>> #<class <a>> #<class <object>> #<class <top>>) |
| 1410 |
@end example |
| 1411 |
|
| 1412 |
@c EN |
| 1413 |
Scheme-defined class always inherits @code{<object>}. |
| 1414 |
It is automatically inserted by the system. |
| 1415 |
|
| 1416 |
When multiple inheritance is involved, a story becomes a bit |
| 1417 |
complicated. We have to merge multiple CPLs of the superclasses |
| 1418 |
into one CPL. It is called @emph{linearization}, and there are |
| 1419 |
several known linearization strategies. By default, Gauche uses |
| 1420 |
an algorithm called @emph{C3 linearization}, |
| 1421 |
which is consistent with the local precedence order, |
| 1422 |
monotonicity, and the extended precedence graph. |
| 1423 |
We don't go into the details here; as a general rule, |
| 1424 |
the order of superclasses in a class's CPL is always |
| 1425 |
consistent to the order of direct superclasses of |
| 1426 |
the class, the order of CPL of each superclasses, |
| 1427 |
and the order of direct superclasses of each superclass, |
| 1428 |
and so on. For the precise description, see |
| 1429 |
Kim Barrett, Bob Cassels, Paul Haahr, |
| 1430 |
David A. Moon, Keith Playford, P. Tucker Withington, |
| 1431 |
A Monotonic Superclass Linearization for Dylan, |
| 1432 |
in @i{Proceedings of OOPSLA 96}, October 1996. |
| 1433 |
|
| 1434 |
If a class inherits superclasses in a way that its CPL can't |
| 1435 |
be constructed with satisfying consistencies, an error is reported. |
| 1436 |
|
| 1437 |
Here's a simple example of multiple inheritance. |
| 1438 |
@c JP |
| 1439 |
Scheme定義のクラスは常に @code{<object>} を継承します。 |
| 1440 |
システムが自動的に挿入します。 |
| 1441 |
|
| 1442 |
多重継承が使われる場合には話はすこし複雑になります。複数のスーパークラスの |
| 1443 |
複数の CPL をひとつの CPL にマージしなければなりません。このことを |
| 1444 |
@emph{線形化}といい、いくつかの線形化戦略が知られています。Gauche では |
| 1445 |
デフォルトで @emph{C3 線形化}と呼ばれているアルゴリズムを使います。 |
| 1446 |
このアルゴリズムは局所的な順位、単調性、拡張順位グラフと整合性のとれた |
| 1447 |
ものです。ここでは詳細に立ち入りませんが、一般的なルールとして、 |
| 1448 |
CPL 中のスーパークラスの順序は、つねにそのクラスの直接スーパークラスの |
| 1449 |
順序、それぞれのスーパークラスの CPL の順序、および、各スーパークラス |
| 1450 |
の直接スーパークラスの順序、などと整合性をもちます。正確な説明については |
| 1451 |
次の文献を参照してください: |
| 1452 |
Kim Barrett, Bob Cassels, Paul Haahr, |
| 1453 |
David A. Moon, Keith Playford, P. Tucker Withington, |
| 1454 |
A Monotonic Superclass Linearization for Dylan, |
| 1455 |
in @i{Proceedings of OOPSLA 96}, October 1996. |
| 1456 |
|
| 1457 |
もしクラスが、整合性を満した CPL を構築できないようなやり方で |
| 1458 |
複数のスーパークラスを継承すると、エラーになります。以下は多重継承の |
| 1459 |
単純な例です。 |
| 1460 |
@c COMMON |
| 1461 |
|
| 1462 |
@example |
| 1463 |
(define-class <grid-layout> () ()) |
| 1464 |
|
| 1465 |
(define-class <horizontal-grid> (<grid-layout>) ()) |
| 1466 |
|
| 1467 |
(define-class <vertical-grid> (<grid-layout>) ()) |
| 1468 |
|
| 1469 |
(define-class <hv-grid> (<horizontal-grid> <vertical-grid>) ()) |
| 1470 |
|
| 1471 |
(map class-name (class-precedence-list <hv-grid>)) |
| 1472 |
@result{} (<hv-grid> <horizontal-grid> <vertical-grid> |
| 1473 |
<grid-layout> <object> <top>) |
| 1474 |
@end example |
| 1475 |
|
| 1476 |
@c EN |
| 1477 |
Note that the order of direct superclasses of @code{<hv-grid>} |
| 1478 |
(@code{<horizontal-grid>} and @code{<vertical-grid>}) is kept. |
| 1479 |
|
| 1480 |
The following is a little twisted example: |
| 1481 |
@c JP |
| 1482 |
@code{<hv-grid>} の直接スーパークラス(@code{<horizontal-grid>}と |
| 1483 |
@code{<vertical-grid>})の順序が保存されていることに注意してください。 |
| 1484 |
|
| 1485 |
以下は、すこしひねくれた例です。 |
| 1486 |
@c COMMON |
| 1487 |
|
| 1488 |
@example |
| 1489 |
(define-class <pane> () ()) |
| 1490 |
|
| 1491 |
(define-class <scrolling-mixin> () ()) |
| 1492 |
|
| 1493 |
(define-class <scrollable-pane> (<pane> <scrolling-mixin>) ()) |
| 1494 |
|
| 1495 |
(define-class <editing-mixin> () ()) |
| 1496 |
|
| 1497 |
(define-class <editable-pane> (<pane> <editing-mixin>) ()) |
| 1498 |
|
| 1499 |
(define-class <editable-scrollable-pane> |
| 1500 |
(<scrollable-pane> <editable-pane>) ()) |
| 1501 |
|
| 1502 |
(map class-name (class-precedence-list <editable-scrollable-pane>)) |
| 1503 |
@result{} (<editable-scrollable-pane> <scrollable-pane> |
| 1504 |
<editable-pane> <pane> <scrolling-mixin> <editing-mixin> |
| 1505 |
<object> <top>) |
| 1506 |
@end example |
| 1507 |
|
| 1508 |
@c EN |
| 1509 |
Once the class precedence order is determined, |
| 1510 |
the slots of defined class is calculated as follows: |
| 1511 |
the slot definitions are collected in the direction from superclasss |
| 1512 |
to subclass in CPL. If a subclass has a slot definition of the same |
| 1513 |
name of the one in superclass, then the slot definition of the subclass |
| 1514 |
is taken and superclass's is discarded. Suppose a class @code{<S>} |
| 1515 |
defines slots @code{a}, @code{b}, and @code{c}, a class @code{<T>} |
| 1516 |
defines slots @code{c}, @code{d}, and @code{e}, and a class @code{<U>} |
| 1517 |
defines slots @code{b} and @code{e}. When @code{<U>}'s CPL is |
| 1518 |
@code{(<U> <T> <S> <object> <top>)}, then @code{<U>}'s slots is |
| 1519 |
calculated as the chart below; that is, @code{<U>} gets five slots, |
| 1520 |
of which @code{b} and @code{e}'s definitions come from @code{<U>}'s |
| 1521 |
definitions, @code{c} and @code{d}'s come from @code{<T>}, and |
| 1522 |
@code{a}'s comes from @code{<S>}. |
| 1523 |
@c JP |
| 1524 |
いったんクラス順位が決まると、定義されたクラスのスロットが以下の手順で |
| 1525 |
計算されます。スロットの定義が CPL 中のスーパークラスからサブクラスへの |
| 1526 |
順で集められます。サブクラスにスーパークラスと同じ名前のスロット定義が |
| 1527 |
あった場合には、サブクラスのそのスロット定義が採用され、スーパークラス |
| 1528 |
の方の定義は捨てられます。あるクラス @code{<S>} がスロット @code{a}、 |
| 1529 |
@code{b}、および @code{c} を定義しており、あるクラス @code{<T>} が |
| 1530 |
スロット @code{c}、@code{d}、および @code{e} を定義し、さらに、 |
| 1531 |
あるクラス @code{<U>} がスロット @code{b} および @code{e} を定義していると |
| 1532 |
しよう。@code{<U>} の CPL が @code{(<U> <T> <S> <object> <top>)} と |
| 1533 |
なっている場合、@code{<U>} のスロットが下の図のように計算されます。 |
| 1534 |
すなわち、@code{<U>} は 5 つのスロットをもち、 |
| 1535 |
@code{b} および @code{e} の定義は @code{<U>} のものを、@code{c} |
| 1536 |
および @code{d} の定義は @code{<T>} 由来のものを、そして、@code{a} |
| 1537 |
の定義は @code{<S>} 由来のものとなります。 |
| 1538 |
@c COMMON |
| 1539 |
|
| 1540 |
@c EN |
| 1541 |
@example |
| 1542 |
CPL | slot definitions |
| 1543 |
| () indicates shadowed slot |
| 1544 |
-----------+------------------- |
| 1545 |
<top> | |
| 1546 |
<object> | |
| 1547 |
<S> | a (b) (c) |
| 1548 |
<T> | c d (e) |
| 1549 |
<U> | b e |
| 1550 |
-----------+-------------------- |
| 1551 |
<U>'s slots| a b c d e |
| 1552 |
@end example |
| 1553 |
@c JP |
| 1554 |
@example |
| 1555 |
CPL | スロットの定義 |
| 1556 |
| () はシャドウされたスロットを表す |
| 1557 |
--------------+------------------- |
| 1558 |
<top> | |
| 1559 |
<object> | |
| 1560 |
<S> | a (b) (c) |
| 1561 |
<T> | c d (e) |
| 1562 |
<U> | b e |
| 1563 |
--------------+-------------------- |
| 1564 |
<U>のスロット | a b c d e |
| 1565 |
@end example |
| 1566 |
@c COMMON |
| 1567 |
|
| 1568 |
@c EN |
| 1569 |
You can get a list of slot definitions of a class object using |
| 1570 |
@code{class-slots} function. |
| 1571 |
|
| 1572 |
Note that the behavior described above is mere a default behavior. |
| 1573 |
You can customize how the CPL is computed, or how slot definitions |
| 1574 |
are inherited, by defining metaclass. For example, |
| 1575 |
you can write a metaclass that allows you to merge slot options |
| 1576 |
of the same slot names, instead of the one shadowing the other. |
| 1577 |
Or you can write a metaclass that forbids a subclass shadows |
| 1578 |
the superclass's slot. |
| 1579 |
@c JP |
| 1580 |
@code{class-slots} 関数を使ってクラスオブジェクトのスロット定義の |
| 1581 |
リストを得ることができます。 |
| 1582 |
|
| 1583 |
上述の振舞いはデフォルトの振舞いにすぎないことに注意してください。 |
| 1584 |
CPL の計算方法あるいはスロット定義の継承方法は、メタクラスを |
| 1585 |
定義することでカスタマイズ可能です。たとえば、同じスロット名の |
| 1586 |
スロットオプションはどれかが他のものをシャドウしますが、これを |
| 1587 |
マージすることができるようにメタクラスを書くことができます。 |
| 1588 |
あるいは、サブクラスがスーパークラスのスロットをシャドウするのを |
| 1589 |
禁止するようにメタクラスを書くことができます。 |
| 1590 |
@c COMMON |
| 1591 |
|
| 1592 |
@node Class object, Slot definition object, Inheritance, Class |
| 1593 |
@subsection Class object |
| 1594 |
@c NODE クラスオブジェクト |
| 1595 |
|
| 1596 |
@c EN |
| 1597 |
What is a class? In Gauche, a class is just an object that implements |
| 1598 |
a specific feature: to instantiate an object. |
| 1599 |
Because of that, you can introspect the class by just |
| 1600 |
looking into the slot values. There are some procedures provided |
| 1601 |
for the convenience of such introspection. |
| 1602 |
Note that if those procedures return a list, it belongs to |
| 1603 |
the class and you shouldn't modify it. |
| 1604 |
@c JP |
| 1605 |
クラスとは何か。Gauche ではクラスはオブジェクトをインスタンス化して |
| 1606 |
ある特定の機能を実装するようなオブジェクトにすぎません。 |
| 1607 |
そうなので、スロットの値を見るだけで、クラス内部を覗けます。 |
| 1608 |
このように内部を覗くのに便利な手続きがいくつか用意されています。 |
| 1609 |
これらの手続きがリストを返す場合、それはクラスに所属するもので、 |
| 1610 |
変更してはいけないということに注意してください。 |
| 1611 |
@c COMMON |
| 1612 |
|
| 1613 |
@defun class-name class |
| 1614 |
@c EN |
| 1615 |
Returns the name of @var{class}. |
| 1616 |
@c JP |
| 1617 |
@var{class} の名前を返します。 |
| 1618 |
@c COMMON |
| 1619 |
|
| 1620 |
@example |
| 1621 |
(class-name <string>) @result{} <string> |
| 1622 |
@end example |
| 1623 |
@end defun |
| 1624 |
|
| 1625 |
@defun class-precedence-list class |
| 1626 |
@c EN |
| 1627 |
Returns the class precedence list of @var{class}. |
| 1628 |
@c JP |
| 1629 |
@var{class} のクラス順位リストを返します。 |
| 1630 |
@c COMMON |
| 1631 |
|
| 1632 |
@example |
| 1633 |
(class-precedence-list <string>) |
| 1634 |
@result{} (#<class <string>> |
| 1635 |
#<class <sequence>> |
| 1636 |
#<class <collection>> |
| 1637 |
#<class <top>>) |
| 1638 |
@end example |
| 1639 |
@end defun |
| 1640 |
|
| 1641 |
@defun class-direct-supers class |
| 1642 |
@c EN |
| 1643 |
Returns a list of direct superclasses of @var{class}. |
| 1644 |
A direct superclass is a class from which |
| 1645 |
@var{class} inherits directly. |
| 1646 |
@c JP |
| 1647 |
@var{class} の直接スーパークラスのリストを返します。 |
| 1648 |
直接スーパークラスは @var{class} が直接継承しているクラスです。 |
| 1649 |
@c COMMON |
| 1650 |
|
| 1651 |
@example |
| 1652 |
(class-direct-supers <string>) |
| 1653 |
@result{} (#<class <sequence>>) |
| 1654 |
@end example |
| 1655 |
@end defun |
| 1656 |
|
| 1657 |
@defun class-direct-subclasses class |
| 1658 |
@c EN |
| 1659 |
Returns a list of direct subclasses of @var{class}. |
| 1660 |
A direct subclass is a class that directly inherits |
| 1661 |
@var{class}. If @code{<T>} is a direct subclass of @code{<S>}, |
| 1662 |
then @code{<S>} is a direct superclass of @code{<T>}. |
| 1663 |
@c JP |
| 1664 |
@var{class} の直接サブクラスのリストを返します。 |
| 1665 |
直接サブクラスは @var{class} を直接継承しているクラスです。 |
| 1666 |
@code{<T>} が @code{<S>} の直接サブクラスであれば、 |
| 1667 |
@code{<S>} は @code{<T>} の直接スーパークラスです。 |
| 1668 |
@c COMMON |
| 1669 |
@end defun |
| 1670 |
|
| 1671 |
@defun class-slots class |
| 1672 |
@c EN |
| 1673 |
Returns a list of @emph{slot definitions} of @var{class}. |
| 1674 |
A slot definition is a list whose car is the name of the slot |
| 1675 |
and whose cdr is a keyword-value list that specifies slot options. |
| 1676 |
You can further inspect a slot definition to know what characteristics |
| 1677 |
the slot has. See @ref{Slot definition object} for the details. |
| 1678 |
|
| 1679 |
The standard way to get a list of slot names of a given class |
| 1680 |
is @code{(map slot-definition-name (class-slots class))}. |
| 1681 |
@c JP |
| 1682 |
@var{class} の @emph{スロット定義} のリストを返します。スロット定義は |
| 1683 |
リストで、その car 部はスロット名、cdr 部はスロットオプションを指定する |
| 1684 |
キーワード値のリストです。スロット定義内部を覗いてスロットのもつ性格を |
| 1685 |
知ることができます。詳しくは @ref{Slot definition object} を |
| 1686 |
参照してください。 |
| 1687 |
|
| 1688 |
与えられたクラスのスロット名のリストを得るための標準的な方法は、 |
| 1689 |
@code{(map slot-definition-name (class-slots class))} です。 |
| 1690 |
@c COMMON |
| 1691 |
@end defun |
| 1692 |
|
| 1693 |
@defun class-slot-definition class slot-name |
| 1694 |
@c EN |
| 1695 |
Returns a slot definition of a slot specified by @var{slot-name} in |
| 1696 |
a class @var{class}. If @var{class} doesn't have a named slot, |
| 1697 |
@code{#f} is returned. |
| 1698 |
@c JP |
| 1699 |
クラス @var{class} 中の @var{slot-name} で指定されたスロットの |
| 1700 |
スロット定義を返します。@var{class} が指定した名前のスロットを |
| 1701 |
持たなければ @code{#f} が返ります。 |
| 1702 |
@c COMMON |
| 1703 |
@end defun |
| 1704 |
|
| 1705 |
@defun class-direct-slots class |
| 1706 |
@c EN |
| 1707 |
Returns a list of slot definitions that are directly defined |
| 1708 |
in this class (i.e. not inherited from superclasses). |
| 1709 |
This information is used to calculate slot inheritance |
| 1710 |
during class initialization. |
| 1711 |
@c JP |
| 1712 |
当該クラスで直接定義されている(つまりスーパークラスから継承された |
| 1713 |
ものではない)スロット定義のリストを返します。この情報は、クラスの |
| 1714 |
初期化の際にスロットの継承を処理するために利用されます。 |
| 1715 |
@c COMMON |
| 1716 |
@end defun |
| 1717 |
|
| 1718 |
@defun class-direct-methods class |
| 1719 |
@c EN |
| 1720 |
Returns a list of methods that has @var{class} in its specializer. |
| 1721 |
@c JP |
| 1722 |
@var{class}を特定化子中にもつメソッドのリストを返します。 |
| 1723 |
@c COMMON |
| 1724 |
@end defun |
| 1725 |
|
| 1726 |
@defun class-slot-accessor class slot-name |
| 1727 |
@c EN |
| 1728 |
Returns a slot accessor object of the slot specified by @var{slot-name} |
| 1729 |
in @var{class}. |
| 1730 |
A slot accessor object is an internal object that encapsulates |
| 1731 |
the information how to access, modify, and initialize the given slot. |
| 1732 |
|
| 1733 |
You don't usually need to deal with slot accessor objects unless |
| 1734 |
you are defining some special slots using metaobject protocol. |
| 1735 |
@c JP |
| 1736 |
@var{class} で @var{slot-name} で指定したスロットの |
| 1737 |
スロットアクセサオブジェクトを返します。 |
| 1738 |
スロットアクセサオブジェクトは内部オブジェクトで与えられたスロットへの |
| 1739 |
アクセス方法、変更方法、初期化の方法という情報をカプセル化しています。 |
| 1740 |
|
| 1741 |
メタオブジェクトプロトコルを使って特別なスロットを定義するのでなければ、 |
| 1742 |
通常スロットアクセサオブジェクトを扱う必要はありません。 |
| 1743 |
@c COMMON |
| 1744 |
@end defun |
| 1745 |
|
| 1746 |
@node Slot definition object, Class redefinition, Class object, Class |
| 1747 |
@subsection Slot definition object |
| 1748 |
@c NODE スロット定義オブジェクト |
| 1749 |
|
| 1750 |
@c EN |
| 1751 |
A slot definition object, returned by @code{class-slots}, |
| 1752 |
@code{class-direct-slots} and @code{class-slot-definition}, |
| 1753 |
keeps information about a slot. |
| 1754 |
Currently Gauche uses a list to represent the slot definition, |
| 1755 |
as STklos and TinyCLOS do. However, it is not guaranteed that |
| 1756 |
Gauche keeps such a structure in future; you should use the |
| 1757 |
following dedicated accessor methods to obtain information |
| 1758 |
of a slot definition object. |
| 1759 |
@c JP |
| 1760 |
@code{class-slots} が返すスロットの定義オブジェクト |
| 1761 |
@code{class-direct-slots} および @code{class-slot-definition} |
| 1762 |
はスロットに関する情報を保持しています。 |
| 1763 |
現時点では Gauche はスロット定義を表現するのにリストを使っています。これは |
| 1764 |
STklos や TinyCLOS と同じです。しかし、Gauche が将来にわたって、この構造を |
| 1765 |
保持するかどうかは保証のかぎりではありません。スロット定義オブジェクトの |
| 1766 |
情報を得るには以下のそれ専用のアクセサメソッドを使うべきです。 |
| 1767 |
@c COMMON |
| 1768 |
|
| 1769 |
@defun slot-definition-name slot-def |
| 1770 |
@c EN |
| 1771 |
Returns the name of a slot given by a slot definition object @var{slot-def}. |
| 1772 |
@c JP |
| 1773 |
スロット定義オブジェクト @var{slot-def} で与えられたスロットの名前を |
| 1774 |
返します。 |
| 1775 |
@c COMMON |
| 1776 |
@end defun |
| 1777 |
|
| 1778 |
@defun slot-definition-options slot-def |
| 1779 |
@c EN |
| 1780 |
Returns a keyword-value list of slot options of @var{slot-def}. |
| 1781 |
@c JP |
| 1782 |
@var{slot-def} のスロットオプションのキーワード値リストを返します。 |
| 1783 |
@c COMMON |
| 1784 |
@end defun |
| 1785 |
|
| 1786 |
@defun slot-definition-allocation slot-def |
| 1787 |
@c EN |
| 1788 |
Returns the value of @code{:allocation} option of @var{slot-def}. |
| 1789 |
@c JP |
| 1790 |
@var{slot-def} の @code{:allocation} オプションの値を返します。 |
| 1791 |
@c COMMON |
| 1792 |
@end defun |
| 1793 |
|
| 1794 |
@defun slot-definition-getter slot-def |
| 1795 |
@defunx slot-definition-setter slot-def |
| 1796 |
@defunx slot-definition-accessor slot-def |
| 1797 |
@c EN |
| 1798 |
Returns the value of @code{:getter}, @code{:setter} and @code{:accessor} |
| 1799 |
slot options of @var{slot-def}, respectively. |
| 1800 |
@c JP |
| 1801 |
それぞれ、@var{slot-def} の @code{:getter}、@code{:setter} および |
| 1802 |
@code{:accessor} スロットオプションの値を返します。 |
| 1803 |
@c COMMON |
| 1804 |
@end defun |
| 1805 |
|
| 1806 |
@defun slot-definition-option slot-def option :optional default |
| 1807 |
@c EN |
| 1808 |
Returns the value of slot option @var{option} of @var{slot-def}. |
| 1809 |
If there's no such an option, @var{default} is returned |
| 1810 |
if given, or an error is signaled otherwise. |
| 1811 |
@c JP |
| 1812 |
@var{slot-def} のスロットオプション @var{option} の値を返します。 |
| 1813 |
そのようなオプションがない場合には、@var{default} が与えられていれば |
| 1814 |
それを返し、さもなければ、エラーシグナルがあがります。 |
| 1815 |
@c COMMON |
| 1816 |
@end defun |
| 1817 |
|
| 1818 |
@node Class redefinition, Class definition examples, Slot definition object, Class |
| 1819 |
@subsection Class redefinition |
| 1820 |
@c NODE クラスの再定義 |
| 1821 |
|
| 1822 |
@c EN |
| 1823 |
If the specified class name is bound to a class when @code{define-class} |
| 1824 |
is used, it is regarded as @emph{redefinition} of the original class. |
| 1825 |
|
| 1826 |
Redefinition of a class means the following operations: |
| 1827 |
@c JP |
| 1828 |
@code{define-class} を使うとき、指定したクラス名がすでにあるクラスに束縛 |
| 1829 |
されている場合、これは元々のクラスの@emph{再定義}と看倣されます。 |
| 1830 |
|
| 1831 |
クラスの再定義は以下の操作を意味します。 |
| 1832 |
@c COMMON |
| 1833 |
@c EN |
| 1834 |
@itemize @bullet |
| 1835 |
@item |
| 1836 |
A new class object is created based on the new definition, |
| 1837 |
and bound to the variable given to @code{define-class}. |
| 1838 |
|
| 1839 |
@item |
| 1840 |
Methods defined on the original class (i.e. methods that have |
| 1841 |
the original class in their specializers) are changed so that |
| 1842 |
they are defined on the new class. |
| 1843 |
|
| 1844 |
@item |
| 1845 |
The direct-subclasses link of the direct superclasses of the |
| 1846 |
original class is modified so that they will point to the new class. |
| 1847 |
|
| 1848 |
@item |
| 1849 |
All the subclasses of the original class are redefined recursively |
| 1850 |
so that they reflect the changes of the class. |
| 1851 |
Each class remembers its initialization arguments, and |
| 1852 |
each redefined subclass gets the same initialization arguments |
| 1853 |
as the original subclass. |
| 1854 |
|
| 1855 |
@item |
| 1856 |
The original class is marked @emph{redefined}. |
| 1857 |
@end itemize |
| 1858 |
@c JP |
| 1859 |
@itemize @bullet |
| 1860 |
@item |
| 1861 |
新しいクラスオブジェクトは新しい定義にもとづいて生成され、 |
| 1862 |
@code{define-class} に与えられた変数に束縛されます。 |
| 1863 |
|
| 1864 |
@item |
| 1865 |
元々のクラスに対して定義されたメソッド(すなわち、特定化子の中に |
| 1866 |
元々のクラスを持つメソッド)は新しいクラス上で定義されたように |
| 1867 |
変更されます。 |
| 1868 |
|
| 1869 |
@item |
| 1870 |
元々のクラスの直接スーパークラスの直接サブクラスリンクは、 |
| 1871 |
新しいクラスを指すように変更されます。 |
| 1872 |
|
| 1873 |
@item |
| 1874 |
元々のクラスのすべてのサブクラスはそのクラスの変更を反映するように |
| 1875 |
再帰的に再定義されます。各クラスはその初期化引数を覚えており、 |
| 1876 |
再定義されたサブクラスは元々のサブクラスと同じ初期化引数を受けとります。 |
| 1877 |
|
| 1878 |
@item |
| 1879 |
元々のクラスには@emph{再定義}マークが付きます。 |
| 1880 |
@end itemize |
| 1881 |
@c COMMON |
| 1882 |
|
| 1883 |
@c EN |
| 1884 |
Note that the original class and the new class are different objects. |
| 1885 |
The original class object remembers which variable in which module |
| 1886 |
it is originally bound, and replaces the binding to a new class. |
| 1887 |
If you keep the direct reference to the original class somewhere else, |
| 1888 |
it still refers to the original class; you might want to take extra |
| 1889 |
care. You can customize class redefinition behavior by |
| 1890 |
defining the @code{class-redefinition} method; see |
| 1891 |
@ref{Metaobject protocol} for the details. |
| 1892 |
@c JP |
| 1893 |
元々のクラスと新しいクラスは別のオブジェクトであることに注意してください。 |
| 1894 |
元々のクラスオブジェクトは元々どのモジュールでどの変数に束縛されていたかを |
| 1895 |
覚えており、この束縛を新しいクラスに置き換えます。どこかで、元々のクラスへ |
| 1896 |
の直接参照を持っていれば、その参照は元々クラスへの参照のままです。この点 |
| 1897 |
については特に注意してください。@code{class-redefinition} メソッドを |
| 1898 |
定義することによりクラス再定義の振舞いをカスタマイズできます。 |
| 1899 |
詳しくは @ref{Metaobject protocol}を参照してください。 |
| 1900 |
@c COMMON |
| 1901 |
|
| 1902 |
@c EN |
| 1903 |
If there are instances of the original class, such instances |
| 1904 |
are automatically @emph{updated} when it is about to be accessed or modified |
| 1905 |
via @code{class-of}, @code{is-a?}, @code{slot-ref}, |
| 1906 |
@code{slot-set!}, @code{ref}, a getter method, or a setter method. |
| 1907 |
@c JP |
| 1908 |
元々のクラスのインスタンスが存在している場合、それらのインスタンスは |
| 1909 |
以下のようなメソッドでアクセスあるいは変更しようとしたときに自動的に |
| 1910 |
更新されます。@code{class-of}、@code{is-a?}、@code{slot-ref}、 |
| 1911 |
@code{slot-set!}、@code{ref}、getterメソッド、setterメソッド。 |
| 1912 |
@c COMMON |
| 1913 |
|
| 1914 |
@c EN |
| 1915 |
Updating an instance means that the class of the instance |
| 1916 |
is changed (from the old class to the new class). |
| 1917 |
By default, the values of the slots that are common in the original |
| 1918 |
class and the new class are carried over, and the slots added |
| 1919 |
by the new class are initialized according to the slot specification |
| 1920 |
of the new class, and the values of the slots that are |
| 1921 |
removed from the original class are discarded. |
| 1922 |
You can customize this behavior by writing the @code{change-class} method. |
| 1923 |
See @ref{Changing classes}, for the details. |
| 1924 |
@c JP |
| 1925 |
インスタンスの更新とは、インスタンスのクラスを(旧いクラスから新しいクラスへ) |
| 1926 |
変更するということです。デフォルトでは、元々のクラスと新しいクラスで |
| 1927 |
共通のスロットの値はそのまま引き継がれます。新しいクラスで追加された |
| 1928 |
スロットは新しいクラスでのそのスロットの仕様にしたがって初期化されます。 |
| 1929 |
元々のクラスから削除されたスロットの値は破棄されます。この振舞いは、 |
| 1930 |
@code{change-class} メソッドを書くことでカスタマイズできます。詳しくは |
| 1931 |
@ref{Changing classes} を参照してください。 |
| 1932 |
@c COMMON |
| 1933 |
|
| 1934 |
@c EN |
| 1935 |
@subsubheading Notes on thread safety |
| 1936 |
|
| 1937 |
Class redefinition process is non-local operation with full of side-effects. |
| 1938 |
It is difficult to guarantee that two threads safely run |
| 1939 |
class redefinition protocol simultaneously. So Gauche uses |
| 1940 |
a process-wide lock to limit only one thread to enter the |
| 1941 |
class redefinition protocol at a time. |
| 1942 |
@c JP |
| 1943 |
@subsubheading スレッド安全性について |
| 1944 |
|
| 1945 |
クラス再定義処理はローカルな処理ではなく、多くの副作用を行います。 |
| 1946 |
複数のスレッドが同時にクラス再定義プロトコルを走らせた場合の |
| 1947 |
安全性を保証するのは困難です。そこでGauche では、一度にひとつのスレッドしか |
| 1948 |
クラス再定義プロトコルに入らないように、プロセスレベルのロックを使用します。 |
| 1949 |
@c COMMON |
| 1950 |
|
| 1951 |
@c EN |
| 1952 |
If a thread tries to redefine a class while another thread is in |
| 1953 |
the redefinition protocol, the thread is blocked, even if |
| 1954 |
it is redefining a class different from the one that are |
| 1955 |
being redefined; because redefinition affects all the |
| 1956 |
subclasses, and all the methods and generic functions that |
| 1957 |
are related to the class and subclasses, it is not trivial |
| 1958 |
to determine two classes are completely independent or not. |
| 1959 |
@c JP |
| 1960 |
スレッドが、別のスレッドが再定義プロトコルにいる最中にクラスを |
| 1961 |
再定義しようとした場合、たとえ、別々のクラスを再定義しようとしている |
| 1962 |
場合でも、そのスレッドはブロックされます。このようにするのは、 |
| 1963 |
再定義がそのすべてのサブクラス、そのクラスとそのサブクラスに |
| 1964 |
関わるすべてのメソッドおよびジェネリック関数に影響するからで、 |
| 1965 |
ふたつのクラスが完全に独立であるかどうかを決定するのは、自明では |
| 1966 |
ないからです。 |
| 1967 |
@c COMMON |
| 1968 |
|
| 1969 |
@c EN |
| 1970 |
If a thread tries to access an instance whose class |
| 1971 |
is being redefined by another thread, also the thread |
| 1972 |
is blocked until the redefinition is finished. |
| 1973 |
@c JP |
| 1974 |
スレッドが他のスレッドが再定義しようとしているクラスのインスタンスに |
| 1975 |
アクセスしようとした場合にも、このスレッドは再定義が完了するまで、 |
| 1976 |
ブロックされます。 |
| 1977 |
@c COMMON |
| 1978 |
|
| 1979 |
@c EN |
| 1980 |
Note that the instance update protocol isn't serialized. |
| 1981 |
If two threads try to access an instance whose |
| 1982 |
class has been redefined, both trigger the instance |
| 1983 |
update protocol, which would cause an undesired race condition. |
| 1984 |
It is the application's responsibility to ensure |
| 1985 |
such a case won't happen. It is natural since the |
| 1986 |
instance access isn't serialized by the system anyway. |
| 1987 |
However, an extra care is required to have mutex within |
| 1988 |
an instance; just accessing the mutex in it may trigger |
| 1989 |
the instance update protocol. |
| 1990 |
@c JP |
| 1991 |
インスタンス更新プロトコルは直列化されません。ふたつのスレッドが |
| 1992 |
再定義されたクラスのインスタンスにアクセスしようとすると、両方の |
| 1993 |
スレッドが更新プロトコルを起動します。これは好ましくない競合状態を |
| 1994 |
生じる可能性があります。このような場合がおきないようにするのは |
| 1995 |
アプリケーションの責任です。インスタンスへのアクセスはどのみち |
| 1996 |
システムが直列化するわけではないので、これは自然なことです。 |
| 1997 |
インスタンス内に mutex を持たせる場合は特に注意が必要です。 |
| 1998 |
インスタンス中のmutexにアクセスするだけでインスタンス更新プロトコルを |
| 1999 |
起動することになる可能性があるからです。 |
| 2000 |
@c COMMON |
| 2001 |
|
| 2002 |
@c EN |
| 2003 |
@subsubheading Notes on compatibility |
| 2004 |
|
| 2005 |
Class redefinition protocols subtlety differ among CLOS-like |
| 2006 |
Scheme systems. Gauche's is very similar to STklos's, except |
| 2007 |
that STklos 0.56 doesn't replace bindings of redefined subclasses, |
| 2008 |
and also it doesn't remember initialization arguments so the |
| 2009 |
redefined subclass may lose some of the information that the original |
| 2010 |
subclass has. Guile's object system swaps identities of the original |
| 2011 |
class and the redefined class at the end of class redefinition |
| 2012 |
protocol, so the reference to the original class object |
| 2013 |
will turn to the redefined class. |
| 2014 |
As far as the author knows, class redefinition is not thread-safe |
| 2015 |
in both STklos 0.56 and Guile 1.6.4. |
| 2016 |
@c JP |
| 2017 |
@subsubheading 互換性に関して |
| 2018 |
|
| 2019 |
クラス再定義プロトコルは CLOS風の Scheme システムとは微妙に違います。 |
| 2020 |
Gauche のものは STklos のものによく似ていますが、STklos 0.56 は再定義 |
| 2021 |
サブクラスの束縛を置き換えず、初期化引数を覚えたりはしないので、 |
| 2022 |
再定義されたサブクラスは、元々のサブクラスが持っていた情報のなにがしかを |
| 2023 |
失ってしまう可能性があるという点にちがいがあります。Guile のオブジェクト |
| 2024 |
システムはクラス再定義プロトコルの最後で、元々のクラスのアイデンティティと |
| 2025 |
再定義されたクラスをアイデンティティを入れ替えてしまします。それゆえ、 |
| 2026 |
元々のクラスへの参照は、再定義されたクラスへの参照となります。筆者が |
| 2027 |
知るかぎり、クラス再定義は、STklos 0.56 においても、Guile 1.6.4 においても |
| 2028 |
スレッド安全ではありません。 |
| 2029 |
@c COMMON |
| 2030 |
|
| 2031 |
@node Class definition examples, , Class redefinition, Class |
| 2032 |
@subsection Class definition examples |
| 2033 |
@c NODE クラスの定義例 |
| 2034 |
|
| 2035 |
@c EN |
| 2036 |
Let's see some examples. Suppose you are defining a graphical |
| 2037 |
toolkit. A @code{<window>} is a rectangle region on |
| 2038 |
the screen, so it has width and height. It can be organized |
| 2039 |
hierarchically, i.e. a window can be placed within another window; |
| 2040 |
so it has a pointer to the parent window. And we specify the |
| 2041 |
window's position, x, y, by the coordinate relative to its |
| 2042 |
parent window. Finally, we create a "root" window that |
| 2043 |
covers entire screen. It also serves the default parent window. |
| 2044 |
So far, what we get is something like this: |
| 2045 |
@c JP |
| 2046 |
いくつかの例をみましょう。グラフィカルツールキットを定義しているところ |
| 2047 |
だということにしましょう。@code{<window>} はスクリーン上の矩形領域で、 |
| 2048 |
幅と高さを持ちます。これを階層構造に構成することが可能です。すなわち、 |
| 2049 |
ある window は別の window 中に置くことができ、親 window へのポインタを |
| 2050 |
もっているものとします。window の位置は、親ウィンドウの位置からの |
| 2051 |
相対座標、x と y で指定します。スクリーン全体を覆う「ルート」window |
| 2052 |
を作り、これがデフォルトの親 window にもなります。ここまでで、次のように |
| 2053 |
なります。 |
| 2054 |
@c COMMON |
| 2055 |
|
| 2056 |
@example |
| 2057 |
;; The first version |
| 2058 |
(define-class <window> () |
| 2059 |
(;; Pointer to the parent window. |
| 2060 |
(parent :init-keyword :parent :init-form *root-window*) |
| 2061 |
;; Sizes of the window |
| 2062 |
(width :init-keyword :width :init-value 1) |
| 2063 |
(height :init-keyword :height :init-value 1) |
| 2064 |
;; Position of the window relative to the parent. |
| 2065 |
(x :init-keyword :x :init-value 0) |
| 2066 |
(y :init-keyword :y :init-value 0) |
| 2067 |
)) |
| 2068 |
|
| 2069 |
(define *screen-width* 1280) |
| 2070 |
(define *screen-height* 1024) |
| 2071 |
|
| 2072 |
(define *root-window* |
| 2073 |
(make <window> :parent #f :width *screen-width* :height *screen-height*)) |
| 2074 |
@end example |
| 2075 |
|
| 2076 |
@c EN |
| 2077 |
Note the usage of @code{:init-value} and @code{:init-form}. |
| 2078 |
When the @code{<window>} class is defined, we haven't bound |
| 2079 |
@code{*root-window*} yet, so we can't use @code{:init-value} here. |
| 2080 |
@c JP |
| 2081 |
@code{:init-value} および @code{:init-form} の使い方に注意してください。 |
| 2082 |
@code{<window>} クラスが定義されたとき、@code{*root-window*} はまだ束縛 |
| 2083 |
されていませんので、ここでは @code{:init-value} は使えません。 |
| 2084 |
@c COMMON |
| 2085 |
|
| 2086 |
@example |
| 2087 |
gosh> *root-window* |
| 2088 |
#<<window> 0x80db1d0> |
| 2089 |
gosh> (define window-a (make <window> :width 100 :height 100)) |
| 2090 |
window-a |
| 2091 |
gosh> (d window-a) |
| 2092 |
#<<window> 0x80db1b0> is an instance of class <window> |
| 2093 |
slots: |
| 2094 |
parent : #<<window> 0x80db1d0> |
| 2095 |
width : 100 |
| 2096 |
height : 100 |
| 2097 |
x : 0 |
| 2098 |
y : 0 |
| 2099 |
gosh> (define window-b |
| 2100 |
(make <window> :parent window-a :width 50 :height 20 :x 10 :y 5)) |
| 2101 |
window-b |
| 2102 |
gosh> (d window-b) |
| 2103 |
#<<window> 0x80db140> is an instance of class <window> |
| 2104 |
slots: |
| 2105 |
parent : #<<window> 0x80db1b0> |
| 2106 |
width : 50 |
| 2107 |
height : 20 |
| 2108 |
x : 10 |
| 2109 |
y : 5 |
| 2110 |
@end example |
| 2111 |
|
| 2112 |
@c EN |
| 2113 |
If you're like me, you don't want to expose a global variable |
| 2114 |
such as @code{*root-window*} for users of your toolkit. |
| 2115 |
One way to encapsulate it (to certain extent) is to keep |
| 2116 |
the pointer to the root window in a class variable. |
| 2117 |
Add the following slot option to the definition of @code{<window>}, |
| 2118 |
and the slot @code{root-window} of the @code{<window>} class |
| 2119 |
refers to the same storage space. |
| 2120 |
@c JP |
| 2121 |
筆者と同じ感覚の持ち主なら、@code{*root-window*} のようなグローバル |
| 2122 |
変数をツールキットのユーザに見せたいとは思わないでしょう。これを |
| 2123 |
カプセル化するひとつの方法は、ルート window へのポインタをクラス変数に |
| 2124 |
保持させることです。@code{<window>} の定義に以下のようなスロットオプションを |
| 2125 |
追加すると @code{<window>} クラスの @code{root-window} スロットは同じ |
| 2126 |
格納領域を参照するようになります。 |
| 2127 |
@c COMMON |
| 2128 |
|
| 2129 |
@example |
| 2130 |
(define-class <window> () |
| 2131 |
(... |
| 2132 |
... |
| 2133 |
(root-window :allocation :class) |
| 2134 |
...)) |
| 2135 |
@end example |
| 2136 |
|
| 2137 |
@c EN |
| 2138 |
You can use @code{slot-ref} and @code{slot-set!} on an instance |
| 2139 |
of @code{<window>}, |
| 2140 |
or use @code{class-slot-ref} and @code{class-slot-set!} on the |
| 2141 |
@code{<window>} class itself, |
| 2142 |
to get/set the value of the @code{root-window} slot. |
| 2143 |
|
| 2144 |
The users of the toolkit may want to get the absolute position |
| 2145 |
of the window (the coordinates in the root window) instead of |
| 2146 |
the relative position. You may provide virtual slots that |
| 2147 |
returns the absolute positions, like the following: |
| 2148 |
@c JP |
| 2149 |
@code{<window>} のインスタンスに対して、@code{slot-ref} および |
| 2150 |
@code{slot-set!} を、また、@code{<window>} クラスそのものに対しては、 |
| 2151 |
@code{class-slot-ref} や @code{class-slot-set!} を使って |
| 2152 |
@code{root-window} スロットの値を取得したり、セットしたりできます。 |
| 2153 |
|
| 2154 |
ツールキットのユーザは window の相対座標のかわりに絶対座標(ルートウィンドウ |
| 2155 |
中の座標)が欲しいと思うことがあるでしょう。以下のようにして、絶対座標を |
| 2156 |
返す仮想スロットを提供することができます。 |
| 2157 |
@c COMMON |
| 2158 |
|
| 2159 |
@example |
| 2160 |
(define-class <window> () |
| 2161 |
(... |
| 2162 |
... |
| 2163 |
(root-x :allocation :virtual |
| 2164 |
:slot-ref (lambda (o) |
| 2165 |
(if (ref o 'parent) |
| 2166 |
(+ (ref (ref o 'parent) 'root-x) |
| 2167 |
(ref o 'x)) |
| 2168 |
(ref o 'x))) |
| 2169 |
:slot-set! (lambda (o v) |
| 2170 |
(set! (ref o 'x) |
| 2171 |
(if (ref o 'parent) |
| 2172 |
(- v (ref (ref o 'parent) 'root-x)) |
| 2173 |
v))) |
| 2174 |
) |
| 2175 |
...)) |
| 2176 |
@end example |
| 2177 |
|
| 2178 |
@c EN |
| 2179 |
Whether providing such interface via methods or virtual slots is |
| 2180 |
somewhat a matter of taste. Using virtual slots has an advantage |
| 2181 |
of being able to hide the change of implementation, i.e. you can |
| 2182 |
change to keep @code{root-x} in a real slot and make @code{x} a virtual |
| 2183 |
slot later without breaking the code using @code{<window>}. |
| 2184 |
(In the mainstream object-oriented languages, |
| 2185 |
such kind of "hiding implementation" is usually achieved |
| 2186 |
by hiding instance variables and exposing methods. In Gauche |
| 2187 |
and other CLOS-like systems, slots are always visible to the |
| 2188 |
users, so the situation is a bit different. |
| 2189 |
@c JP |
| 2190 |
メソッドあるいは仮想スロット経由のこのようなインタフェースを供給することは |
| 2191 |
いくぶんか趣味の問題です。仮想スロットは実装の変更を隠すことができるという |
| 2192 |
利点があります。つまり、@code{root-x} を実スロットに保持するように変更し、 |
| 2193 |
@code{x} をあとで仮想スロットに変更するということを @code{<window>}を |
| 2194 |
使うコードをだめにすることなくおこなえます。オブジェクト指向の主流の言語 |
| 2195 |
では、通常このような「実装変更の隠蔽」はインスタンス変数を隠し、メソッドを |
| 2196 |
公開するということでおこなわれています。Gauche やその他の CLOS 風システムでは |
| 2197 |
スロットは常にユーザから見えており状況はすこし違うのです。 |
| 2198 |
@c COMMON |
| 2199 |
|
| 2200 |
@c ---------------------------------------------------------------------- |
| 2201 |
@node Instance, Generic function and method, Class, Object system |
| 2202 |
@section Instance |
| 2203 |
@c NODE インスタンス |
| 2204 |
|
| 2205 |
@c EN |
| 2206 |
In this section, we explain how to create and use an instance. |
| 2207 |
@c JP |
| 2208 |
この節では、インスタンスの生成のしかたと使い方について説明します。 |
| 2209 |
@c COMMON |
| 2210 |
|
| 2211 |
@menu |
| 2212 |
* Creating instance:: |
| 2213 |
* Accessing instance:: |
| 2214 |
* Describing instance:: |
| 2215 |
* Changing classes:: |
| 2216 |
@end menu |
| 2217 |
|
| 2218 |
@node Creating instance, Accessing instance, Instance, Instance |
| 2219 |
@subsection Creating instance |
| 2220 |
@c NODE インスタンスの作成 |
| 2221 |
|
| 2222 |
@c EN |
| 2223 |
Using class object, you can create an instance of the class |
| 2224 |
by a generic function @code{make}. |
| 2225 |
A specialized method for standard @code{<class>} is defined: |
| 2226 |
@c JP |
| 2227 |
クラスオブジェクトをつかうと、ジェネリック関数 @code{make} でその |
| 2228 |
クラスのインスタンスを生成できます。標準の @code{<class>} に対して |
| 2229 |
特定化されたメソッドは以下のとおり定義されています。 |
| 2230 |
@c COMMON |
| 2231 |
|
| 2232 |
@deffn {Generic Function} make |
| 2233 |
@deffnx {Method} make (class <class>) arg @dots{} |
| 2234 |
@c EN |
| 2235 |
Creates an instance of @var{class} and returns it. |
| 2236 |
@var{Arg} @dots{} is typically a keyword-value list |
| 2237 |
to initialize the instance. |
| 2238 |
@c JP |
| 2239 |
@var{class} のインスタンスを生成し、それを返します。@var{arg} @dots{} は |
| 2240 |
典型的な場合には、そのインスタンスを初期化するためのキーワード値のリストです。 |
| 2241 |
@c COMMON |
| 2242 |
@end deffn |
| 2243 |
|
| 2244 |
@c EN |
| 2245 |
Conceptually, the default @code{make} method is defined |
| 2246 |
as follows: |
| 2247 |
@c JP |
| 2248 |
概念としては、デフォルトの @code{make} メソッドは以下のように定義されて |
| 2249 |
います。 |
| 2250 |
@c COMMON |
| 2251 |
|
| 2252 |
@example |
| 2253 |
(define-method make ((class <class>) . initargs) |
| 2254 |
(let ((obj (allocate-instance class initargs))) |
| 2255 |
(initialize obj initargs) |
| 2256 |
obj)) |
| 2257 |
@end example |
| 2258 |
|
| 2259 |
@c EN |
| 2260 |
That is, first it allocates memory for @var{class}'s instance, |
| 2261 |
then initialize it with the @code{initialize} method. |
| 2262 |
@c JP |
| 2263 |
すなわち、最初、@code{class} のインスタンス用にメモリをアロケートし、 |
| 2264 |
それから、@code{initialize} メソッドを用いてそれを初期化します。 |
| 2265 |
@c COMMON |
| 2266 |
|
| 2267 |
@deffn {Generic Function} allocate-instance |
| 2268 |
@deffnx {Method} allocate-instance (class <class>) initargs |
| 2269 |
@c EN |
| 2270 |
Returns a newly-allocated uninitialized instance of @var{class}. |
| 2271 |
@c JP |
| 2272 |
@var{class}の新規にアロケートされた、初期化されていないインスタンスを返します。 |
| 2273 |
@c COMMON |
| 2274 |
@end deffn |
| 2275 |
|
| 2276 |
@deffn {Generic Function} initialize |
| 2277 |
@deffnx {Method} initialize (obj <object>) initargs |
| 2278 |
@c EN |
| 2279 |
The default initialize method for @code{<object>} works as follows: |
| 2280 |
@c JP |
| 2281 |
@code{<object>} に対するデフォルトの初期化メソッドは以下のように働きます。 |
| 2282 |
@c COMMON |
| 2283 |
|
| 2284 |
@c EN |
| 2285 |
@itemize @bullet |
| 2286 |
@item |
| 2287 |
For each initializable slot of the class |
| 2288 |
@itemize @bullet |
| 2289 |
@item |
| 2290 |
If (the slot has the :init-keyword slot option |
| 2291 |
AND the keyword appears in @var{initargs}): |
| 2292 |
Then the corresponding value is used to initialize the slot |
| 2293 |
@item |
| 2294 |
Else if the slot has :init-value slot option: |
| 2295 |
Then the value given to the slot option is used to initialize the slot |
| 2296 |
@item |
| 2297 |
Else if the slot has :init-thunk slot option: |
| 2298 |
Then the thunk is called, and the returned value is used to initialize |
| 2299 |
the slot. |
| 2300 |
@item |
| 2301 |
Else: The slot is left unbound. |
| 2302 |
@end itemize |
| 2303 |
@end itemize |
| 2304 |
@c JP |
| 2305 |
@itemize @bullet |
| 2306 |
@item |
| 2307 |
そのクラスの初期化可能なスロットのそれぞれに対して、 |
| 2308 |
@itemize @bullet |
| 2309 |
@item |
| 2310 |
もし (そのスロットが :init-keyword スロットオプションを持ち、「かつ」 |
| 2311 |
そのキーワードが @var{initargs} 中にある) |
| 2312 |
そのときは、対応する値がそのスロットを初期化するのに使われます。 |
| 2313 |
@item |
| 2314 |
そうではないとき、もし、そのスロットが、:init-value スロットオプションを |
| 2315 |
持てば、そのオプションに与えられた値がそのスロットを初期化するのに使われます。 |
| 2316 |
@item |
| 2317 |
そうでもないとき、もし、そのスロットが、:init-thunk スロットオプションを |
| 2318 |
持てば、その thunk が呼ばれその返り値が、そのスロットを初期化するのに |
| 2319 |
使われます。 |
| 2320 |
@item |
| 2321 |
さらにどれでもなければ、そのスロットは未束縛のままです。 |
| 2322 |
@end itemize |
| 2323 |
@end itemize |
| 2324 |
@c COMMON |
| 2325 |
|
| 2326 |
@c EN |
| 2327 |
Among the default slot allocation classes, only |
| 2328 |
instance-allocated slots are initializable and are handled by |
| 2329 |
the above sequence. Class-allocated slots (e.g. its |
| 2330 |
slot allocation is either @code{:class} or @code{:each-subclass}) |
| 2331 |
are initialized when the class object is created, if |
| 2332 |
@code{:init-value} or @code{:init-form} slot option is given. |
| 2333 |
Virtual slots aren't initialized at all. |
| 2334 |
@c JP |
| 2335 |
デフォルトのスロットアロケーションクラスのなかで、インスタンスアロケート |
| 2336 |
スロットだけが初期化可能で、上の流れで処理されます。クラスアロケート |
| 2337 |
スロット(すなわち、スロットアロケーションが @code{:class} あるいは |
| 2338 |
@code{:each-subclass} のどちらかの場合)は、@code{:init-value} あるいは |
| 2339 |
@code{:init-form} スロットオプションが与えられていれば、クラスオブジェクト |
| 2340 |
生成時に初期化されます。仮想スロットが、初期化されることはありません。 |
| 2341 |
@c COMMON |
| 2342 |
|
| 2343 |
@c EN |
| 2344 |
An user-defined allocation class can be |
| 2345 |
configured either initializable or not initializable; see |
| 2346 |
@ref{Metaobject protocol} for the details. |
| 2347 |
@c JP |
| 2348 |
ユーザ定義アロケーションクラスは、初期化可能にも不可能にも設定することが |
| 2349 |
できます。詳しくは @ref{Metaobject protocol} を参照してください。 |
| 2350 |
@c COMMON |
| 2351 |
|
| 2352 |
@c EN |
| 2353 |
If you specialize @code{initialize} method, make sure to call |
| 2354 |
@code{next-method} so that the slots are properly initialized |
| 2355 |
by the default sequence, before accessing any slot of the |
| 2356 |
newly created instance. |
| 2357 |
@c JP |
| 2358 |
@code{initialize} メソッドを特定化する場合には @code{next-method} が |
| 2359 |
確実に呼ばれるようにして、新しく生成されたインスタンスのすべての |
| 2360 |
スロットにアクセスする前にデフォルトの流れで、そのスロットが正しく |
| 2361 |
初期化されるようにしてください。 |
| 2362 |
@c COMMON |
| 2363 |
@end deffn |
| 2364 |
|
| 2365 |
@c EN |
| 2366 |
Typically you specialize @code{initialize} method for |
| 2367 |
your class to customize how the instance is initialized. |
| 2368 |
|
| 2369 |
It is not common to specialize @code{allocate-instance} method. |
| 2370 |
However, knowing that how @code{make} works, you can specialize |
| 2371 |
@code{make} itself to avoid allocation of instance in |
| 2372 |
some circumstances (e.g. using pre-allocated instances). |
| 2373 |
@c JP |
| 2374 |
定義したクラスに対応する @code{initialize} メソッドを特定化して |
| 2375 |
インスタンスの初期化の方法をカスタマイズするというのが、典型的な |
| 2376 |
やりかたです。 |
| 2377 |
|
| 2378 |
@code{allocate-instance} メソッドを特定化するというのは一般的な方法では |
| 2379 |
ありません。しかしながら、@code{make} がどのように働くかを知っているなら、 |
| 2380 |
@code{make} そのものを特定化して、なんらかの状況(たとえば、あらかじめ |
| 2381 |
アロケートしてあるインスタンスを使うというような状況)でインスタンスの |
| 2382 |
アロケーションを回避できます。 |
| 2383 |
@c COMMON |
| 2384 |
|
| 2385 |
@node Accessing instance, Describing instance, Creating instance, Instance |
| 2386 |
@subsection Accessing instance |
| 2387 |
@c NODE インスタンスへのアクセス |
| 2388 |
|
| 2389 |
@c EN |
| 2390 |
@subsubheading Standard accessors |
| 2391 |
@c JP |
| 2392 |
@subsubheading 標準アクセサ |
| 2393 |
@c COMMON |
| 2394 |
|
| 2395 |
@defun slot-ref obj slot |
| 2396 |
@c EN |
| 2397 |
Returns a value of the slot @var{slot} of object @var{obj}. |
| 2398 |
|
| 2399 |
If the specified slot is not bound to any value, a generic function |
| 2400 |
@code{slot-unbound} is called with three arguments, @var{obj}'s class, |
| 2401 |
@var{obj}, and @var{slot}. The default behavior of @code{slot-unbound} |
| 2402 |
is to signal an error. |
| 2403 |
|
| 2404 |
If the object doesn't have the specified slot, |
| 2405 |
a generic function @code{slot-missing} is called with three |
| 2406 |
arguments, @var{obj}'s class, |
| 2407 |
@var{obj}, and @var{slot}. The default behavior of @code{slot-missing} |
| 2408 |
is to signal an error. |
| 2409 |
@c JP |
| 2410 |
オブジェクト@var{obj}のスロット@var{slot}の値を返します。 |
| 2411 |
|
| 2412 |
指定したスロットが値に束縛されていない場合、ジェネリック関数 |
| 2413 |
@code{slot-unbound}が3つの引数、@var{obj}のクラス、@var{obj}、@var{slot} |
| 2414 |
を伴って呼び出されます。@code{slot-unbound}のデフォルトの振る舞いは、 |
| 2415 |
エラーの通知です。 |
| 2416 |
|
| 2417 |
オブジェクトが指定されたスロットを持っていない場合は、ジェネリック関数 |
| 2418 |
@code{slot-missing}が3つの引数、@var{obj}のクラス、@var{obj}、@var{slot}を |
| 2419 |
伴って呼び出されます。@code{slot-missing}のデフォルトの振る舞いは、 |
| 2420 |
エラーの通知です。 |
| 2421 |
@c COMMON |
| 2422 |
@end defun |
| 2423 |
|
| 2424 |
@defun slot-set! obj slot value |
| 2425 |
@c EN |
| 2426 |
Alters the value of the slot @var{slot} of object @var{obj} to |
| 2427 |
the value @var{value}. |
| 2428 |
|
| 2429 |
If the object doesn't have the specified slot, a generic function |
| 2430 |
@code{slot-missing} is called with four arguments, |
| 2431 |
@var{obj}'s class, @var{obj}, @var{slot}, @var{value}. |
| 2432 |
@c JP |
| 2433 |
オブジェクト@var{obj}のスロット@var{slot}の値を、@var{value}に |
| 2434 |
セットします。 |
| 2435 |
|
| 2436 |
オブジェクトが指定したスロットを持っていない場合は、ジェネリック関数 |
| 2437 |
@code{slot-missing}が4つの引数、@var{obj}のクラス、@var{obj}、 |
| 2438 |
@var{slot}、@var{value}を伴って呼び出されます。 |
| 2439 |
@c COMMON |
| 2440 |
@end defun |
| 2441 |
|
| 2442 |
@defun slot-bound? obj slot |
| 2443 |
@c EN |
| 2444 |
Returns true if object @var{obj}'s slot @var{slot} is bound, |
| 2445 |
otherwise returns false. |
| 2446 |
|
| 2447 |
If the object doesn't have the specified slot, a generic function |
| 2448 |
@code{slot-missing} is called with three arguments, |
| 2449 |
@var{obj}'s class, @var{obj}, @var{slot}. |
| 2450 |
@c JP |
| 2451 |
オブジェクト@var{obj}のスロット@var{slot}が束縛されていれば真を、 |
| 2452 |
そうでなければ偽を返します。 |
| 2453 |
|
| 2454 |
オブジェクトが指定したスロットを持っていない場合は、ジェネリック関数 |
| 2455 |
@code{slot-missing}が3つの引数、@var{obj}のクラス、@var{obj}、 |
| 2456 |
@var{slot}を伴って呼び出されます。 |
| 2457 |
@c COMMON |
| 2458 |
@end defun |
| 2459 |
|
| 2460 |
@defun slot-exists? obj slot |
| 2461 |
@c EN |
| 2462 |
Returns true if @var{obj} has the slot named @var{slot}. |
| 2463 |
@c JP |
| 2464 |
@var{obj}が@var{slot}を持っていれば真を返します。 |
| 2465 |
@c COMMON |
| 2466 |
@end defun |
| 2467 |
|
| 2468 |
@defun slot-push! obj slot value |
| 2469 |
@c EN |
| 2470 |
This function implements the common idiom. |
| 2471 |
It can be defined like the following code |
| 2472 |
(but it may be optimized in the future versions). |
| 2473 |
@c JP |
| 2474 |
この関数は、一般的なイディオムの実装です。 |
| 2475 |
これは以下のようなコードで定義できます(が、将来のバージョンでは |
| 2476 |
最適化されるでしょう)。 |
| 2477 |
@c COMMON |
| 2478 |
@example |
| 2479 |
(define (slot-push! obj slot value) |
| 2480 |
(slot-set! obj slot (cons value (slot-ref obj slot)))) |
| 2481 |
@end example |
| 2482 |
@end defun |
| 2483 |
|
| 2484 |
@defun slot-pop! obj slot :optional fallback |
| 2485 |
@c EN |
| 2486 |
Reverse operation of @code{slot-push!}. |
| 2487 |
If the value of @var{slot} of @var{obj} |
| 2488 |
is a pair, removes its car and returns the removed item. |
| 2489 |
|
| 2490 |
When the value of @var{slot} is not a pair, or the @var{slot} is unbound, |
| 2491 |
@var{fallback} is returned if it is provided, otherwise an error is |
| 2492 |
signaled. |
| 2493 |
@c JP |
| 2494 |
@code{slot-push!}と逆の操作です。@var{obj}の@var{slot}の値が |
| 2495 |
ペアの場合、そのcarを@var{slot}の値から取り除き、取り除かれた値を返します。 |
| 2496 |
|
| 2497 |
@var{slot}の値がペアでない、あるいは@var{slot}が未束縛の場合、 |
| 2498 |
@var{fallback}が与えられていればそれが返され、そうでなければエラーが報告されます。 |
| 2499 |
@c COMMON |
| 2500 |
@end defun |
| 2501 |
|
| 2502 |
@deffn {Method} ref (obj <object>) (slot <symbol>) |
| 2503 |
@deffnx {Method} {(setter ref)} (obj <object>) (slot <symbol>) value |
| 2504 |
@c EN |
| 2505 |
These methods just calls @code{slot-ref} and @code{slot-set!}, respectively. |
| 2506 |
They are slightly less efficient than directly calling @code{slot-ref} |
| 2507 |
and @code{slot-set!}, but more compact in the program code. |
| 2508 |
@c JP |
| 2509 |
これらのメソッドはそれぞれ、単に @code{slot-ref} および @code{slot-set!} |
| 2510 |
を呼ぶだけです。直接 @code{slot-ref} や @code{slot-set!} を呼ぶよりも |
| 2511 |
効率はすこし悪いですが、プログラムコードはコンパクトになります。 |
| 2512 |
@c COMMON |
| 2513 |
@end deffn |
| 2514 |
|
| 2515 |
@c EN |
| 2516 |
@subsubheading Fallback methods |
| 2517 |
@c JP |
| 2518 |
@subsubheading フォールバックメソッド |
| 2519 |
@c COMMON |
| 2520 |
|
| 2521 |
@deffn {Generic Function} slot-unbound |
| 2522 |
@deffnx {Method} slot-unbound (class <class>) obj slot |
| 2523 |
@c EN |
| 2524 |
This generic function is called when an unbound slot value is |
| 2525 |
retrieved. The return value of this generic function will be |
| 2526 |
returned to the caller that tried to get the value. |
| 2527 |
|
| 2528 |
The default method just signals an error. |
| 2529 |
@c JP |
| 2530 |
このジェネリック関数は束縛されていないスロットの値を取り出そうとしたときに |
| 2531 |
呼び出されます。このジェネリック関数の返り値は値を得ようとした呼出しもとに |
| 2532 |
返されます。 |
| 2533 |
|
| 2534 |
デフォルトのメソッドは単にエラーのシグナルをあげるだけです。 |
| 2535 |
@c COMMON |
| 2536 |
@end deffn |
| 2537 |
|
| 2538 |
@deffn {Generic Function} slot-missing |
| 2539 |
@deffnx {Method} slot-missing (class <class>) obj slot :optional value |
| 2540 |
@c EN |
| 2541 |
This generic function is called when a non-existent slot |
| 2542 |
value is retrieved or set. The return value of this generic |
| 2543 |
function will be returned to the caller that tried to get the value. |
| 2544 |
|
| 2545 |
The default method just signals an error. |
| 2546 |
@c JP |
| 2547 |
このジェネリック関数は存在しないスロットの値を取り出そうとしたとき、あるいは |
| 2548 |
設定しようとしたときに呼びだされます。このジェネリック関数の返り値は、 |
| 2549 |
値を得ようとした呼出しもとに返されます。 |
| 2550 |
|
| 2551 |
デフォルトのメソッドは単にエラーのシグナルをあげるだけです。 |
| 2552 |
@c COMMON |
| 2553 |
@end deffn |
| 2554 |
|
| 2555 |
@c EN |
| 2556 |
@subsubheading Special accessors |
| 2557 |
@c JP |
| 2558 |
@subsubheading 特殊アクセサ |
| 2559 |
@c COMMON |
| 2560 |
|
| 2561 |
@defun current-class-of obj |
| 2562 |
@c EN |
| 2563 |
Returns a class metaobject of @var{obj}. If @var{obj}'s class |
| 2564 |
has been redefined, but @var{obj} is not updated for the change, |
| 2565 |
then this procedure returns the original class of @var{obj} |
| 2566 |
without updating @var{obj}. |
| 2567 |
|
| 2568 |
You need this procedure in rare occasions, such as within |
| 2569 |
@code{change-class} method, in which you don't want to trigger |
| 2570 |
updating @var{obj} (which would cause infinite loop). |
| 2571 |
@c JP |
| 2572 |
@var{obj} のクラスメタオブジェクトを返します。@var{obj} のクラスが |
| 2573 |
再定義されてしまった場合でも、@var{obj} がその変更に合せて更新されて |
| 2574 |
いない場合には、この手続きは @var{obj} の元のクラスを返します。 |
| 2575 |
この手続きは、@var{obj} を更新しません。 |
| 2576 |
|
| 2577 |
この手続きはめったに必要にはなりません。必要になるのは @code{change-class} |
| 2578 |
メソッド内で、@var{obj} の更新のトリガーを引きたくないような場合 |
| 2579 |
(無限ループを起す可能性がある場合)です。 |
| 2580 |
@c COMMON |
| 2581 |
@end defun |
| 2582 |
|
| 2583 |
@defun class-slot-ref class slot-name |
| 2584 |
@defunx class-slot-set! class slot-name obj |
| 2585 |
@defunx class-slot-bound? class slot-name obj |
| 2586 |
@c EN |
| 2587 |
When slot's @code{:allocation} option is either @code{:class} |
| 2588 |
or @code{:each-subclass}, these procedures allow you to |
| 2589 |
get/set the value of the slot without having an instance. |
| 2590 |
@c JP |
| 2591 |
スロットの@code{:allocation}オプションが@code{:class}もしくは |
| 2592 |
@code{:each-subclass}である場合、これらの手続きを使って、 |
| 2593 |
インスタンス無しでそれらのスロットの値を取得/設定できます。 |
| 2594 |
@c COMMON |
| 2595 |
@end defun |
| 2596 |
|
| 2597 |
@deffn {Method} slot-ref-using-class (class <class>) (obj <object>) slot-name |
| 2598 |
@deffnx {Method} slot-set-using-class! (class <class>) (obj <object>) slot-name value |
| 2599 |
@deffnx {Method} slot-bound-using-class? (class <class>) (obj <object>) slot-name |
| 2600 |
@c EN |
| 2601 |
Generic function version of @code{slot-ref}, @code{slot-set!} and |
| 2602 |
@code{slot-bound?}. @var{Class} must be the class of @var{obj}. |
| 2603 |
|
| 2604 |
Besides being generic, these functions are different from |
| 2605 |
their procedural versions that they don't trigger class |
| 2606 |
redefinition when @var{obj}'s class has been redefined |
| 2607 |
(i.e. in which case, @var{class} should be the original class |
| 2608 |
of @var{obj}). |
| 2609 |
@c JP |
| 2610 |
@code{slot-ref}、@code{slot-set!}、@code{slot-bound?} のジェネリック |
| 2611 |
関数版です。@var{class} は @var{object} のクラスでなければなりません。 |
| 2612 |
|
| 2613 |
これらの関数は、ジェネリックであることに加えて、 |
| 2614 |
@var{obj} のクラスが再定義されてもクラスの再定義を起動しない |
| 2615 |
(そのような場合、@var{class}は @var{obj} の元々のクラスで |
| 2616 |
なければならない)という点で手続き版とは違います。 |
| 2617 |
@c COMMON |
| 2618 |
|
| 2619 |
@c EN |
| 2620 |
Note: Unlike CLOS, @code{slot-ref} etc. don't call the generic |
| 2621 |
function version in it, so you can't customize the behavior |
| 2622 |
of @code{slot-ref} by specializing @code{slot-ref-using-class}. |
| 2623 |
So the primary purpose of those generic functions are to be |
| 2624 |
used within @code{change-class} method; especially, @code{slot-ref} etc. |
| 2625 |
can't be used during @var{obj}'s being redefined, since they |
| 2626 |
trigger class redefinition again (see @ref{Changing classes} for details). |
| 2627 |
@c JP |
| 2628 |
覚書: CLOS とはちがい、@code{slot-ref} などは、その中でジェネリック |
| 2629 |
関数版を呼ぶことはありません。それゆえ、@code{slot-ref-using-class} を |
| 2630 |
特定化することによって、@code{slot-ref} をカスタマイズすることはできません。 |
| 2631 |
つまり、これらのジェネリック関数の主たる目的は @code{change-class} |
| 2632 |
メソッドの内部で使われることです。とくに、@code{slot-ref} などは |
| 2633 |
クラス再定義を再度起動する(詳細については @ref{Changing classes} を |
| 2634 |
参照)ので、@var{obj} の再定義中には使えません。 |
| 2635 |
@c COMMON |
| 2636 |
@end deffn |
| 2637 |
|
| 2638 |
@node Describing instance, Changing classes, Accessing instance, Instance |
| 2639 |
@subsection Describing instance |
| 2640 |
@c NODE インスタンスの記述 |
| 2641 |
|
| 2642 |
@c EN |
| 2643 |
In REPL, you can invoke @code{,d} or @code{,describe} toplevel |
| 2644 |
command to view the detailed description of an object |
| 2645 |
(@pxref{Working in REPL}). It is realized as a generic function |
| 2646 |
@code{describe}: |
| 2647 |
@c JP |
| 2648 |
REPLで@code{,d}もしくは@code{,describe}トップレベルコマンドを使うと |
| 2649 |
オブジェクトの詳しい情報が表示されます(@ref{Working in REPL}参照)。 |
| 2650 |
これは、ジェネリックファンクション@code{describe}により実現されています。 |
| 2651 |
@c COMMON |
| 2652 |
|
| 2653 |
@deffn {Generic Function} describe obj |
| 2654 |
@c EN |
| 2655 |
Prints the detail information about a Scheme object @var{obj}. |
| 2656 |
The default method shows @var{obj}'s class, and if it has |
| 2657 |
any slots, the list of slot names and their values. |
| 2658 |
@c JP |
| 2659 |
Schemeオブジェクト@var{obj}の詳細情報を表示します。 |
| 2660 |
既定メソッドは@var{obj}のクラスを表示し、さらに@var{obj}がスロットを |
| 2661 |
持っていればその名前と内容をリストします。 |
| 2662 |
@c COMMON |
| 2663 |
|
| 2664 |
@example |
| 2665 |
gosh> (describe (sys-stat "Makefile")) |
| 2666 |
#<<sys-stat> 0x1e7de60> is an instance of class <sys-stat> |
| 2667 |
slots: |
| 2668 |
type : regular |
| 2669 |
perm : 436 |
| 2670 |
mode : 33204 |
| 2671 |
ino : 3242280 |
| 2672 |
dev : 2097 |
| 2673 |
rdev : 0 |
| 2674 |
nlink : 1 |
| 2675 |
uid : 500 |
| 2676 |
gid : 500 |
| 2677 |
size : 19894 |
| 2678 |
atime : 1435379061 |
| 2679 |
mtime : 1432954340 |
| 2680 |
ctime : 1432954340 |
| 2681 |
@end example |
| 2682 |
|
| 2683 |
@c EN |
| 2684 |
When you load @code{gauche.interactive} module (which is already done |
| 2685 |
when you're in REPL), a bunch of methods for built-in objects |
| 2686 |
are defined to @code{describe}, showing information specific |
| 2687 |
to their classes. @xref{Interactive session}, for more details. |
| 2688 |
@c JP |
| 2689 |
@code{gauche.interactive}モジュールをロードした場合 |
| 2690 |
(REPLを使っていれば自動的にロードされています)、多くの組み込みオブジェクトに対して |
| 2691 |
@code{describe}メソッドが追加されるので、それぞれのクラスに応じた |
| 2692 |
詳細情報が表示されます。詳しくは@ref{Interactive session}を見てください。 |
| 2693 |
@c COMMON |
| 2694 |
|
| 2695 |
@c EN |
| 2696 |
You can also define a specialized @code{describe} method on your own class |
| 2697 |
for customized description. The common pattern of a customized |
| 2698 |
describe method is (1) call @code{describe-common} first, which |
| 2699 |
prints the common opening line, and (2) at the end, make sure to |
| 2700 |
return zero values---which will play nicely with REPL. |
| 2701 |
You can print whatever you like inbetween. |
| 2702 |
@c JP |
| 2703 |
自分で定義したクラスに特殊化した@code{describe}メソッドを定義することで、 |
| 2704 |
記述をカスタマイズすることができます。 |
| 2705 |
@code{describe}メソッドに共通するパターンは: |
| 2706 |
(1) まず@code{describe-common}を呼びます。これは共通した最初の一行を表示します。 |
| 2707 |
(2) 最後に@code{(values)}を呼んで値を返さないようにします。 |
| 2708 |
これはREPLで便利です。 |
| 2709 |
その間では好きな情報を表示できます。 |
| 2710 |
@c COMMON |
| 2711 |
|
| 2712 |
@example |
| 2713 |
(define-method describe ((obj <my-class>)) |
| 2714 |
;; common opening line, "#<...> is an instance of class <my-class>" |
| 2715 |
(describe-common obj) |
| 2716 |
;; print relevant information |
| 2717 |
... custom description ... |
| 2718 |
;; if you have slots to show: |
| 2719 |
(describe-slots obj) |
| 2720 |
;; return 0 values. This works nicely in REPL. |
| 2721 |
(values)) |
| 2722 |
@end example |
| 2723 |
@end deffn |
| 2724 |
|
| 2725 |
@defun describe-common obj |
| 2726 |
@c EN |
| 2727 |
This procedure prints the common opening line of @code{describe}. |
| 2728 |
Custom @code{describe} method should call this at the beginning. |
| 2729 |
@c JP |
| 2730 |
この手続きは@code{describe}で共通して表示される最初の行を出力します。 |
| 2731 |
カスタム@code{describe}メソッドはこれを最初に呼んでください。 |
| 2732 |
@c COMMON |
| 2733 |
|
| 2734 |
@example |
| 2735 |
(describe-common 1) |
| 2736 |
@result{} @r{prints} (a b c) is an instance of class <pair> |
| 2737 |
@end example |
| 2738 |
@end defun |
| 2739 |
|
| 2740 |
@defun describe-slots obj |
| 2741 |
@c EN |
| 2742 |
This shows the list of slots and their values of @var{obj}, as |
| 2743 |
in the default @code{describe} method. Useful for the custom |
| 2744 |
describe method, when you want to print some extra information |
| 2745 |
@emph{and} list of slot values. |
| 2746 |
@c JP |
| 2747 |
この手続きはデフォルトの@code{describe}メソッドがやるように、 |
| 2748 |
@var{obj}のスロットとその値のリストを表示します。 |
| 2749 |
カスタムのdescribeメソッドの中で、他の情報と一緒にスロットの情報を |
| 2750 |
表示したい場合に使えます。 |
| 2751 |
@c COMMON |
| 2752 |
|
| 2753 |
@example |
| 2754 |
(describe-slots (current-input-port)) |
| 2755 |
@result{} @r{prints} |
| 2756 |
slots: |
| 2757 |
name : "(standard input)" |
| 2758 |
buffering : :full |
| 2759 |
sigpipe-sensitive?: #f |
| 2760 |
current-line: 7 |
| 2761 |
current-column: 0 |
| 2762 |
link : #<oport (standard output) 0x7ff0cc6cddd0> |
| 2763 |
@end example |
| 2764 |
|
| 2765 |
@c EN |
| 2766 |
By default, this procedure does not display the slots whose name begins |
| 2767 |
with @code{%}. It reflects the convention that private or internal |
| 2768 |
slots are named so. There're no mechanism in the Gauche object |
| 2769 |
system to ``hide'' such slots; it is purely for users' convenience, |
| 2770 |
not to clutter @code{describe} output with excess details. |
| 2771 |
If necessary, you can set the dynamic state @code{describe-details} @code{#t} |
| 2772 |
to make @code{describe-slots} show all the slots. |
| 2773 |
@c JP |
| 2774 |
デフォルトでは、この手続きは@code{%}で始まる名前を持つスロットを表示しません。 |
| 2775 |
これは内部的に使われるスロットを@code{%}で始める慣習を反映したものです。 |
| 2776 |
オブジェクトシステムとしては、特定の名前のスロットを「隠す」機能はありません。 |
| 2777 |
これは純粋に、@code{describe}の出力を余分な情報で溢れさせないための |
| 2778 |
ユーザインタフェースとしての機能です。 |
| 2779 |
必要なら、動的状態@code{describe-details}を真の値に設定することで、 |
| 2780 |
@code{describe-slots}が全てのスロットを表示するようにできます。 |
| 2781 |
@c COMMON |
| 2782 |
@end defun |
| 2783 |
|
| 2784 |
@defun describe-details :optional flag |
| 2785 |
@c EN |
| 2786 |
This is a global dynamic state to control whether @code{describe} |
| 2787 |
shows auxiliary details. When called without arguments, it |
| 2788 |
returns the current state. When given @var{flag}, which is a boolean, |
| 2789 |
it sets @var{flag} as the current state, and returns the previous state. |
| 2790 |
|
| 2791 |
The defalut value is @code{#f}, in which case @code{describe} does not |
| 2792 |
show the details. Notably, an object's slots whose name begins with |
| 2793 |
@code{%} won't be shown. |
| 2794 |
|
| 2795 |
Currently, the state is global and changing it is not thread-safe. |
| 2796 |
We intend this as a REPL user-interface parameter, rather than something |
| 2797 |
manipulated by a program. We may change it to thread-safe, |
| 2798 |
or even a parameter, in future if it is found useful. |
| 2799 |
@c JP |
| 2800 |
これはグローバルな動的状態で、@code{describe}が補助的な詳細を表示するかどうかを制御します。 |
| 2801 |
引数なしで呼ばれた場合が現在の状態を返します。引数@var{flag}を与えた場合、 |
| 2802 |
それは真偽値でなければならず、@var{flag}の値が現在の状態となり、 |
| 2803 |
直前の状態が返されます。 |
| 2804 |
|
| 2805 |
デフォルトの値は@code{#f}で、@code{describe}は詳細を表示しません。 |
| 2806 |
特に、オブジェクトのスロット名が@code{%}ではじまるものは省略されます。 |
| 2807 |
|
| 2808 |
今のところ、状態はグローバルであり、変更はスレッドセーフではありません。 |
| 2809 |
この状態はプログラムで頻繁にいじるものではなく、 |
| 2810 |
REPLユーザインタフェースの調整パラメタと考えているためです。 |
| 2811 |
ただ、将来、便利だとわかればこれをスレッドセーフにしたり、 |
| 2812 |
パラメータにしたりするかもしれません。 |
| 2813 |
@c COMMON |
| 2814 |
@end defun |
| 2815 |
|
| 2816 |
|
| 2817 |
@node Changing classes, , Describing instance, Instance |
| 2818 |
@subsection Changing classes |
| 2819 |
@c NODE クラスの変更 |
| 2820 |
|
| 2821 |
@c EN |
| 2822 |
@subsubheading Class change protocol |
| 2823 |
|
| 2824 |
An unique feature of CLOS-family object system is that |
| 2825 |
you can change classes of an existing instance. The two |
| 2826 |
classes doesn't need to be related; you can change a sewing |
| 2827 |
machine into an umbrella, if you like. |
| 2828 |
@c JP |
| 2829 |
@subsubheading クラス変更プロトコル |
| 2830 |
|
| 2831 |
CLOS系のオブジェクトシステムのユニークな機能は既存のインスタンスの |
| 2832 |
クラスを変更できるということです。新旧二つのクラスに関連性がある必要は |
| 2833 |
ありません。おのぞみなら、ミシンを雨がさに変更することもできます。 |
| 2834 |
@c COMMON |
| 2835 |
|
| 2836 |
@deffn {Generic Function} change-class |
| 2837 |
@deffnx {Method} change-class (obj <object>) (new-class <class>) |
| 2838 |
@c EN |
| 2839 |
Changes an object @var{obj}'s class to @var{new-class}. |
| 2840 |
The default method just calls @code{change-object-class} procedure. |
| 2841 |
@c JP |
| 2842 |
オブジェクト @var{obj} のクラスを @var{new-class} に変更します。 |
| 2843 |
デフォルトのメソッドは単に @code{change-object-class} 手続きを呼ぶだけです。 |
| 2844 |
@c COMMON |
| 2845 |
@end deffn |
| 2846 |
|
| 2847 |
@defun change-object-class obj orig-class new-class |
| 2848 |
@c EN |
| 2849 |
Changes an object @var{obj}'s class from @var{orig-class} to |
| 2850 |
@var{new-class}. This isn't a generic function---changing object's |
| 2851 |
class needs some secret magic, and this procedure encapsulates it. |
| 2852 |
|
| 2853 |
The precise steps of changing class are as follow: |
| 2854 |
|
| 2855 |
@enumerate |
| 2856 |
@item |
| 2857 |
A new instance of @var{new-class} is allocated by @code{allocate-instance}. |
| 2858 |
|
| 2859 |
@item |
| 2860 |
For each slot of @var{new-class}: |
| 2861 |
@enumerate |
| 2862 |
@item |
| 2863 |
If the slot also exists in @var{old-class}, and is bound in @var{obj}, |
| 2864 |
the value is retrieved from @var{obj} and set to the new instance. |
| 2865 |
(The slot is @emph{carried over}). |
| 2866 |
@item |
| 2867 |
Otherwise, the slot of the new instance is initialized by standard |
| 2868 |
slot initialization protocol, as described in @ref{Creating instance}. |
| 2869 |
@end enumerate |
| 2870 |
|
| 2871 |
@item |
| 2872 |
Finally, the content of the new instance is @emph{transplanted} to |
| 2873 |
the @var{obj}---that is, @var{obj} becomes the instance of @var{new-class} |
| 2874 |
without changing its identity. |
| 2875 |
@end enumerate |
| 2876 |
|
| 2877 |
Note that @code{initialize} method of @var{new-class} |
| 2878 |
isn't called on @var{obj}. |
| 2879 |
If you desire, you can call it by your own @code{change-class} method. |
| 2880 |
|
| 2881 |
@code{Change-object-class} returns @var{obj}. |
| 2882 |
@c JP |
| 2883 |
オブジェクト @var{obj} のクラスを @var{orig-class} から @var{new-class} |
| 2884 |
に変更します。これはジェネリック関数ではありません。 |
| 2885 |
オブジェクトのクラスを変更するにはちょっとした秘密の内部的操作が必要で、 |
| 2886 |
この手続きはそれを隠蔽しています。 |
| 2887 |
|
| 2888 |
クラスを変更する正確なステップは以下のようになっています。 |
| 2889 |
|
| 2890 |
@enumerate |
| 2891 |
@item |
| 2892 |
@var{new-class} の新しいインスタンスが @code{allocate-instance} によって |
| 2893 |
アロケートされる。 |
| 2894 |
|
| 2895 |
@item |
| 2896 |
@var{new-class}の各スロットに対して、 |
| 2897 |
@enumerate |
| 2898 |
@item |
| 2899 |
もし、そのスロットが @var{old-class} に存在し、@var{obj} で束縛されていれば、 |
| 2900 |
その値は @var{obj} から取り出され、新しいインスタンスにセットされる。 |
| 2901 |
(そのスロットは@emph{持ち越され}ます。) |
| 2902 |
@item |
| 2903 |
そうでなければ、新しいインスタンスのスロットは、@ref{Creating instance} で |
| 2904 |
説明された手順で、標準のスロット初期化プロトコルによって初期化されます。 |
| 2905 |
@end enumerate |
| 2906 |
|
| 2907 |
@item |
| 2908 |
最後に、新しいインスタンスの内容が @var{obj} に@emph{移植}されます。すなわち、 |
| 2909 |
@var{obj} がアイデンティティを変えることなく @var{new-class} のインスタンス |
| 2910 |
となります。 |
| 2911 |
@end enumerate |
| 2912 |
|
| 2913 |
@var{obj}に対して@var{new-class}の@code{initialize} メソッドは呼ばれないことに |
| 2914 |
注意してください。必要なら、独自の @code{change-class} メソッドを |
| 2915 |
定義して@code{initialize}を呼ぶようにすることができます。 |
| 2916 |
|
| 2917 |
@code{change-object-class} は @var{obj} を返します。 |
| 2918 |
@c COMMON |
| 2919 |
@end defun |
| 2920 |
|
| 2921 |
@c EN |
| 2922 |
Usually a user is not supposed to call @code{change-object-class} directly. |
| 2923 |
Instead, she can define a specialized @code{change-class}. For example, |
| 2924 |
if she wants to carry over the slot @code{x} of old class to the |
| 2925 |
slot @code{y} of new class, she may write something like this: |
| 2926 |
@c JP |
| 2927 |
ユーザは大抵の場合、@code{change-object-class}を直接呼ぶ必要は無いでしょう。 |
| 2928 |
そのかわり、特定化した @code{change-class} を定義するべきです。 |
| 2929 |
たとえば、旧いクラスのスロット @code{x} を |
| 2930 |
新しいクラスのスロット @code{y} へ持ち越すことは、 |
| 2931 |
こんな風に書けば可能です。 |
| 2932 |
@c COMMON |
| 2933 |
|
| 2934 |
@example |
| 2935 |
(define-method change-class ((obj <old-class>) <new-class>) |
| 2936 |
(let ((old-val (slot-ref obj 'x))) |
| 2937 |
(next-method) ;; calls default change-class |
| 2938 |
(slot-set! obj 'y old-val) ;; here, obj's class is already <new-class>. |
| 2939 |
obj)) |
| 2940 |
@end example |
| 2941 |
|
| 2942 |
@c EN |
| 2943 |
@subsubheading Customizing instance update |
| 2944 |
|
| 2945 |
Updating an instance for a redefined class is also handled |
| 2946 |
as class change. When an object is accessed via normal |
| 2947 |
slot accessor/modifier, its class is checked whether |
| 2948 |
it has been redefined. And if it has indeed been redefined, |
| 2949 |
@code{change-class} is called with the |
| 2950 |
redefined class as @var{new-class}; that is, updating |
| 2951 |
an instance is regarded as changing object's class from |
| 2952 |
the original one to the redefined one. |
| 2953 |
|
| 2954 |
By specializing @code{change-class}, you can customize the |
| 2955 |
way an instance is updated for a redefined class. |
| 2956 |
However, you need a special care to write @code{change-class} |
| 2957 |
for class redefinition. |
| 2958 |
|
| 2959 |
First, the redefinition changes global binding of the class object. |
| 2960 |
So you need to keep the reference to the old class before |
| 2961 |
redefining the class, and use the old class to specialize |
| 2962 |
@code{change-class} method: |
| 2963 |
@c JP |
| 2964 |
@subsubheading インスタンス更新のカスタマイズ |
| 2965 |
|
| 2966 |
再定義されたクラスのインスタンスが更新される場合は、それもクラス変更として |
| 2967 |
扱われます。オブジェクトは通常のスロットアクセサ/モディファイア |
| 2968 |
経由でアクセスされるときに、そのクラスが再定義されたかどうかを |
| 2969 |
チェックされます。もし再定義が行われていれば、 |
| 2970 |
再定義されたクラスを@var{new-class}として |
| 2971 |
@code{change-class}が呼ばれます。 |
| 2972 |
すなわち、インスタンスの更新はオブジェクトの |
| 2973 |
クラスを元々のものから再定義されたものへ変更することと看倣されます。 |
| 2974 |
|
| 2975 |
@code{change-class} を特定化することで、インスタンスを再定義された |
| 2976 |
クラス用に更新する方法をカスタマイズできます。しかし、クラス再定義 |
| 2977 |
用の @code{change-class} を書くには特別な注意が必要です。 |
| 2978 |
|
| 2979 |
まず、再定義はクラスオブジェクトのグローバルな束縛を変更してしまいます。 |
| 2980 |
それゆえ、クラス再定義がおこる前の旧いクラスへの参照を保持しておく |
| 2981 |
必要があり、@code{change-class} メソッドの特定化をするには、この旧い |
| 2982 |
クラスを使う必要があります。 |
| 2983 |
@c COMMON |
| 2984 |
|
| 2985 |
@example |
| 2986 |
;; save old <myclass> |
| 2987 |
(define <old-myclass> <myclass>) |
| 2988 |
|
| 2989 |
;; redefine <myclass> |
| 2990 |
(define-class <myclass> () |
| 2991 |
...) |
| 2992 |
|
| 2993 |
;; define customized change-class method |
| 2994 |
(define-method change-class ((obj <old-myclass>) <myclass>) |
| 2995 |
... |
| 2996 |
(next-method) |
| 2997 |
...) |
| 2998 |
@end example |
| 2999 |
|
| 3000 |
@c EN |
| 3001 |
Next, note that the above @code{change-class} method may be |
| 3002 |
triggered implicitly when you access to @var{obj} via |
| 3003 |
@code{slot-ref}, @code{slot-set!}, @code{class-of}, etc. |
| 3004 |
If you use such procedures like @code{slot-ref} on @var{obj} |
| 3005 |
again within @code{change-class}, it would trigger the |
| 3006 |
instance update protocol recursively, which would cause |
| 3007 |
an infinite loop. |
| 3008 |
You can only use the methods that doesn't trigger |
| 3009 |
instance update, that is, @code{slot-ref-using-class}, |
| 3010 |
@code{slot-set-using-class!}, @code{slot-bound-using-class?} |
| 3011 |
and @code{current-class-of}. |
| 3012 |
|
| 3013 |
If you want to carry over a slot whose value is calculated |
| 3014 |
procedurally, such as a virtual slot, then @code{slot-ref} etc. |
| 3015 |
might be called implicitly on @var{obj} during calculating the |
| 3016 |
slot value. Actually @code{change-object-class} has a |
| 3017 |
special protection to detect such a recursion. If that |
| 3018 |
happens, @code{change-object-class} gives up to retrieve |
| 3019 |
the slot value and just initializes the slot of the new instance |
| 3020 |
as if the old slot were unbound. |
| 3021 |
|
| 3022 |
Customizing instance update is highly tricky business, |
| 3023 |
although very powerful. You can find some nontrivial |
| 3024 |
cases in the test program of Gauche source code; |
| 3025 |
take a look at @code{test/object.scm}. |
| 3026 |
@c JP |
| 3027 |
次に、上の @code{change-class} メソッドは、@code{slot-ref}、@code{slot-set!}、 |
| 3028 |
@code{class-of} などを経由して、暗黙のうちに起動される得ることに |
| 3029 |
注意してください。もし、@code{change-class} 内で、@var{obj} に対して |
| 3030 |
再度 @code{slot-ref} のような手続き使うと、インスタンス更新プロトコルが |
| 3031 |
再帰的に起動され、無限ループをひきおこすことになります。インスタンス |
| 3032 |
更新を起動しないようなメソッドしか使えません。すなわち、 |
| 3033 |
@code{slot-ref-using-class}、@code{slot-set-using-class!}、 |
| 3034 |
@code{slot-bound-using-class?}、@code{current-class-of} しか使えません。 |
| 3035 |
|
| 3036 |
仮想スロットのように、手続きによって計算される値をもつスロットを |
| 3037 |
持ち越したいのであれば、@code{slot-ref} その他が、そのスロットの値を |
| 3038 |
計算している最中に、暗黙裏に @var{obj} に対して呼ばれることがありえます。 |
| 3039 |
実際のところは、@code{change-object-class} はこのような再帰を検出する |
| 3040 |
保護機構をもっています。もし、このようなことが起これば、 |
| 3041 |
@code{change-object-class} はそのスロットの値を取り出すのを諦め、 |
| 3042 |
新しいインスタンスのスロットを旧いスロットが未束縛であるとして、 |
| 3043 |
初期化します。 |
| 3044 |
|
| 3045 |
インスタンス更新をカスタマイズするのは非常に強力ですがたいへん |
| 3046 |
トリッキーな仕事です。Gauche のソース中のテストプログラム |
| 3047 |
には自明ではないいくつかのケースが含まれています。@code{test/object.scm} |
| 3048 |
見てみてください。 |
| 3049 |
@c COMMON |
| 3050 |
|
| 3051 |
@c ---------------------------------------------------------------------- |
| 3052 |
@node Generic function and method, Metaobject protocol, Instance, Object system |
| 3053 |
@section Generic function and method |
| 3054 |
@c NODE ジェネリックファンクションとメソッド |
| 3055 |
|
| 3056 |
@c EN |
| 3057 |
@subheading Defining methods |
| 3058 |
@c JP |
| 3059 |
@subheading メソッドの定義 |
| 3060 |
@c COMMON |
| 3061 |
|
| 3062 |
@defmac define-generic name :key class |
| 3063 |
@c EN |
| 3064 |
Creates a generic function and bind it to @var{name}. |
| 3065 |
|
| 3066 |
You don't usually need to use this, since the @code{define-method} |
| 3067 |
macro implicitly creates a generic function if it doesn't exist yet. |
| 3068 |
|
| 3069 |
You can pass a subclass of @code{<generic>} to the @var{class} |
| 3070 |
keyword argument so that the created generic function will be the |
| 3071 |
instance of the passed class, instead of the default @code{<generic>} class. |
| 3072 |
It is useful when you defined a subclass of @code{<generic>} to customize |
| 3073 |
generic function application behavior. |
| 3074 |
@c JP |
| 3075 |
ジェネリック関数を生成し、@var{name} に束縛します。 |
| 3076 |
|
| 3077 |
通常は、これを使う必要はありません。もし、まだ存在していなければ、 |
| 3078 |
@code{define-method} マクロが暗黙裏にジェネリック関数を生成してくれるからです。 |
| 3079 |
|
| 3080 |
キーワード引数@code{class}に、@code{<generic>}のサブクラスを渡すことで、 |
| 3081 |
作られるジェネリック関数をデフォルトの@code{<generic>}クラスのインスタンスでは |
| 3082 |
なく指定のクラスのインスタンスにすることができます。@code{<generic>}の |
| 3083 |
サブクラスを定義してメソッド適用をカスタマイズする場合に便利です。 |
| 3084 |
@c COMMON |
| 3085 |
@end defmac |
| 3086 |
|
| 3087 |
@defmac define-method name [qualifier @dots{}] specs body |
| 3088 |
@c EN |
| 3089 |
Defines a method whose name is @var{name}. If there's already |
| 3090 |
a generic function object globally bound to @var{name}, the created |
| 3091 |
method is added to the generic function. If @var{name} is unbound, |
| 3092 |
or bound to an object except a generic function, then a new generic |
| 3093 |
function is created, bound to @var{name}, then a new method is |
| 3094 |
added to it. |
| 3095 |
@c JP |
| 3096 |
@var{name} という名前のメソッドを定義します。すでにグローバルに @var{name} |
| 3097 |
に束縛されているジェネリック関数オブジェクトが存在していれば、生成された |
| 3098 |
メソッドはそのジェネリック関数に追加されます。@var{name} が未束縛であるか |
| 3099 |
またはジェネリック関数以外に束縛されているなら、新しいジェネリック関数が |
| 3100 |
生成され、@var{name} に束縛されて、新しいメソッドがそれに追加されます。 |
| 3101 |
@c COMMON |
| 3102 |
|
| 3103 |
@c EN |
| 3104 |
The name can be followed by optional @var{qualifiers}, each of |
| 3105 |
which is a keyword. Currently, only the following qualifier is |
| 3106 |
valid. |
| 3107 |
@c JP |
| 3108 |
名前の後に、省略可能な@var{qualifier}を置くことができます。 |
| 3109 |
各@var{qualifier}はキーワードです。 |
| 3110 |
現在、次の@var{qualifier}のみが有効です。 |
| 3111 |
@c COMMON |
| 3112 |
|
| 3113 |
@table @code |
| 3114 |
@item :locked |
| 3115 |
@c EN |
| 3116 |
Declares that you won't redefine |
| 3117 |
the method with the same specifiers. Attempt to redefine it |
| 3118 |
will raise an error. (You can still define methods with |
| 3119 |
different specifiers.) |
| 3120 |
|
| 3121 |
Most methods concerning basic operations on built-in objects |
| 3122 |
are locked, for redefining them would case Gauche's infrastracture |
| 3123 |
unstable. It also allows Gauche to perform certain optimizations. |
| 3124 |
@c JP |
| 3125 |
同じ特定化子(@var{specs})を持つメソッドを再定義しないことを宣言します。 |
| 3126 |
再定義しようとするとエラーが投げられます。 |
| 3127 |
(特定化子の異なるメソッドは定義できます)。 |
| 3128 |
|
| 3129 |
組み込みオブジェクトの基本的な操作に関わるメソッドの多くはロックされています。 |
| 3130 |
それらを再定義してしまうと、Gaucheの基盤が不安定になる恐れがあるからです。 |
| 3131 |
また、最適化が可能になります。 |
| 3132 |
@c COMMON |
| 3133 |
@end table |
| 3134 |
|
| 3135 |
@c EN |
| 3136 |
@var{Specs} specifies the arguments and their types for this method. |
| 3137 |
It's like the argument list of lambda form, except you can specify |
| 3138 |
the type of each argument. |
| 3139 |
@c JP |
| 3140 |
@var{specs} はこのメソッドに対応する引数とその型を指定します。これは |
| 3141 |
lambda 形式の引数リストに似ていますが、それぞれの引数の型を指定できる |
| 3142 |
ところが違います。 |
| 3143 |
@c COMMON |
| 3144 |
|
| 3145 |
@example |
| 3146 |
@i{specs} : ( @i{arg} @dots{} ) |
| 3147 |
| ( @i{arg} @dots{} . @i{symbol} ) |
| 3148 |
| ( @i{arg} @dots{} @i{extended-spec} @dots{}) |
| 3149 |
| @i{symbol} |
| 3150 |
|
| 3151 |
@i{arg} : ( @i{symbol} @i{class} ) |
| 3152 |
| @i{symbol} |
| 3153 |
@end example |
| 3154 |
|
| 3155 |
@c EN |
| 3156 |
@i{Class} specifies the class that the argument has to belong to. |
| 3157 |
If @code{@i{arg}} is just a symbol, it is equivalent to |
| 3158 |
@code{(@i{arg} <top>)}. You can't specify the type for |
| 3159 |
the ``rest'' argument, for it is always bound to a list. |
| 3160 |
|
| 3161 |
You can use extended argument specifications such as |
| 3162 |
@code{:optional}, @code{:key} and @code{:rest} as well. |
| 3163 |
(@xref{Making procedures}, for the explanation of extended |
| 3164 |
argument specifications). Those extended arguments are treated |
| 3165 |
as if a single ``rest'' argument in terms of dispatching; they |
| 3166 |
aren't used for method dispatch, and you can't specify classes for |
| 3167 |
these optional and keyword arguments. |
| 3168 |
|
| 3169 |
The list of classes of the argument list is called |
| 3170 |
@emph{method specializer list}, based on which the generic |
| 3171 |
function will select appropriate methods(s). |
| 3172 |
Here are some examples of @var{specs} and the corresponding |
| 3173 |
specializer list (note that the rest argument isn't considered |
| 3174 |
as a part of specializer list; we know it's always a list.) |
| 3175 |
The @code{optional} item indicates whether the method takes |
| 3176 |
rest arguments or not. |
| 3177 |
@c JP |
| 3178 |
@i{class} は引数が所属すべきクラスを指定します。@code{@i{arg}} が単に |
| 3179 |
シンボルであれば、@code{(@i{arg} <top>)} と同じです。rest 引数の |
| 3180 |
型を指定することはできません。それは常にリストに束縛されるからです。 |
| 3181 |
|
| 3182 |
@code{:optional}、@code{:key}、@code{:rest}等の拡張引数指定を使うことも |
| 3183 |
できます。(拡張引数指定については@ref{Making procedures}を参照してください)。 |
| 3184 |
拡張引数指定は、メソッドディスパッチに関しては rest引数と同様に扱われます。 |
| 3185 |
すなわち、省略可能引数やキーワード引数にクラスを指定することはできません。 |
| 3186 |
|
| 3187 |
引数リストのクラスのリストは@emph{メソッド特定化子リスト}といい、 |
| 3188 |
これを基に、ジェネリック関数は適切なメソッドを選択します。@var{specs} と |
| 3189 |
それに対応する特定化子リストの例をあげておきます。 |
| 3190 |
(rest引数は特定化子リストには考慮されないことに注意。リストに決まっているからです。) |
| 3191 |
@code{optional}の項はメソッドがrest引数を取るかどうかを示します。 |
| 3192 |
@c COMMON |
| 3193 |
|
| 3194 |
@example |
| 3195 |
specs: ((self <myclass>) (index <integer>) value) |
| 3196 |
specializers: (<myclass> <integer> <top>) |
| 3197 |
optional: #f |
| 3198 |
|
| 3199 |
specs: (obj (attr <string>)) |
| 3200 |
specializers: (<top> <string>) |
| 3201 |
optional: #f |
| 3202 |
|
| 3203 |
specs: ((self <myclass>) obj . options) |
| 3204 |
specializers: (<myclass> <top>) |
| 3205 |
optional: #t |
| 3206 |
|
| 3207 |
specs: ((self <myclass>) obj :optional (a 0) (b 1) :key (c 2)) |
| 3208 |
specializers: (<myclass> <top>) |
| 3209 |
optional: #t |
| 3210 |
|
| 3211 |
specs: args |
| 3212 |
specializers: () |
| 3213 |
optional: #t |
| 3214 |
@end example |
| 3215 |
|
| 3216 |
@c EN |
| 3217 |
If you define a method on @var{name} whose specializer list, |
| 3218 |
and whether it takes rest arguments, |
| 3219 |
match with one in the generic function's methods, |
| 3220 |
then the existing method is |
| 3221 |
replaced by the newly defined one, unless the original method is locked. |
| 3222 |
@c JP |
| 3223 |
その特定化子リストがジェネリック関数の中のメソッドの一つに一致し、 |
| 3224 |
rest引数の有無も同じである |
| 3225 |
@var{name} 上のメソッドを定義すると、既存のメソッドは新しく定義された |
| 3226 |
メソッドに置き換えられます。(既存のメソッドがロックされていない限り)。 |
| 3227 |
@c COMMON |
| 3228 |
|
| 3229 |
@c EN |
| 3230 |
Note: If you're running Gauche with keyword-symbol integrated |
| 3231 |
mode (@pxref{Keyword and symbol integration}), there's an ambiguity |
| 3232 |
if you specify a keyword as the sole @var{specs} (to receive entire |
| 3233 |
arguments in a single variable). |
| 3234 |
Gauche parses keywords following @var{name} |
| 3235 |
as qualifiers, so avoid using a keyword as such a variable. |
| 3236 |
@c JP |
| 3237 |
註: Gaucheがキーワード-シンボル統合モード |
| 3238 |
((@ref{Keyword and symbol integration}参照)で実行されている場合、 |
| 3239 |
@var{specs}が単独のシンボルで、それがキーワードでもあった場合 |
| 3240 |
(引数すべてを単一の変数で受け取る、その変数がキーワードでもあった場合) |
| 3241 |
に曖昧性が生じます。Gaucheでは@var{name}に続くキーワードはすべて |
| 3242 |
qualifierとしてパーズするので、単一の変数からなる@var{specs}には |
| 3243 |
キーワードを使わないようにしてください。 |
| 3244 |
@c COMMON |
| 3245 |
@end defmac |
| 3246 |
|
| 3247 |
@c EN |
| 3248 |
@subheading Applying generic function |
| 3249 |
@c JP |
| 3250 |
@subheading ジェネリック関数の適用 |
| 3251 |
@c COMMON |
| 3252 |
|
| 3253 |
@c EN |
| 3254 |
When a generic function is applied, first it selects |
| 3255 |
methods whose specializer list matches the given arguments. |
| 3256 |
For example, suppose a generic function @code{foo} has |
| 3257 |
three methods, whose specializer lists are |
| 3258 |
@code{(<string> <top>)}, @code{(<string> <string>)}, |
| 3259 |
and @code{(<top> <top>)}, respectively. |
| 3260 |
When @code{foo} is applied like @code{(foo "abc" 3)}, |
| 3261 |
the first and the third method will be selected. |
| 3262 |
|
| 3263 |
Then the selected methods are sorted from the most |
| 3264 |
@emph{specific} method to the least specific method. |
| 3265 |
It is calculated as follows: |
| 3266 |
@c JP |
| 3267 |
ジェネリック関数は適用されると、まず、与えられた引数に適合する |
| 3268 |
特定化子リストを持つメソッドを選択します。たとえば、ジェネリック関数 |
| 3269 |
@code{foo} が 3つのメソッドを持っており、それらの特定化子リストが |
| 3270 |
それぞれ、@code{(<string> <top>)}、@code{(<string> <string>)}、 |
| 3271 |
@code{(<top> <top>)} であるとします。@code{foo} が @code{(foo "abc" 3)} の |
| 3272 |
ように適用されたとき、最初と 3番目のメソッドが選択されます。 |
| 3273 |
|
| 3274 |
選択されたメソッドは、もっとも@emph{特定化された}ものから、もっとも一般的なものへ |
| 3275 |
の順でソートされます。これは以下のように計算されます。 |
| 3276 |
@c COMMON |
| 3277 |
|
| 3278 |
@itemize |
| 3279 |
@c EN |
| 3280 |
@item |
| 3281 |
Suppose we have a method @code{a} that has |
| 3282 |
specializers @code{(A1 A2 @dots{})}, and |
| 3283 |
a method @code{b} that has @code{(B1 B2 @dots{})}. |
| 3284 |
|
| 3285 |
@item |
| 3286 |
Find the minimum @var{n} where the classes @code{An} and @code{Bn} |
| 3287 |
differ. Then the class of @var{n}-th argument is taken, |
| 3288 |
and its class precedence list is checked. |
| 3289 |
If @code{An} comes before @code{Bn} in the CPL, then |
| 3290 |
method @code{a} is more specific than @code{b}. |
| 3291 |
Otherwise, @code{b} is more specific than @code{a}. |
| 3292 |
|
| 3293 |
@item |
| 3294 |
If all the specializers of @code{a} and @code{b} are the same, |
| 3295 |
except that one has an improper tail ("rest" argument) |
| 3296 |
and another doesn't, then the method that doesn't have an improper |
| 3297 |
tail is more specific than the one that has. |
| 3298 |
@c JP |
| 3299 |
@item |
| 3300 |
@code{(A1 A2 @dots{})} という特定化子リストをもつメソッド @code{a} と |
| 3301 |
@code{(B1 B2 @dots{})} という特定化子リストをもつメソッド @code{b} とが |
| 3302 |
あるとします。 |
| 3303 |
|
| 3304 |
@item |
| 3305 |
@code{An} と @code{Bn} とが異る最小の @var{n} を見つけます。@var{n} 番目の |
| 3306 |
引数のクラスをとり、そのクラスの順位リストをチェックします。もし、 |
| 3307 |
その CPL の中で、@code{An} が @code{Bn} より先にくれば、method @code{a} は |
| 3308 |
メソッド @code{b} より特定化されているということにし、さもなければ、 |
| 3309 |
@code{b} が @code{a} がより特定化されているということにします。 |
| 3310 |
|
| 3311 |
@item |
| 3312 |
もし、@code{a} および @code{b} のすべての特定化子が、一方は rest 引数 |
| 3313 |
をもち、もう一方はもたないという以外同じであれば、rest 引数をもたない |
| 3314 |
メソッドのほうがそうでないものより特定化されているとします。 |
| 3315 |
@c COMMON |
| 3316 |
@end itemize |
| 3317 |
|
| 3318 |
@c EN |
| 3319 |
Once methods are sorted, the body of the first method is |
| 3320 |
called with the actual argument. |
| 3321 |
|
| 3322 |
Within the method body, a special local variable @code{next-method} |
| 3323 |
is bound implicitly. |
| 3324 |
@c JP |
| 3325 |
メソッドがソートされたら、最初のメソッドの本体が実引数で呼ばれます。 |
| 3326 |
|
| 3327 |
メソッド本体内部では、特別なローカル変数 @code{next-method}が暗黙裏に |
| 3328 |
束縛されます。 |
| 3329 |
@c COMMON |
| 3330 |
|
| 3331 |
@deffn {Next method} next-method |
| 3332 |
@deffnx {Next method} next-method args @dots{} |
| 3333 |
@c EN |
| 3334 |
This variable is bound within a method body |
| 3335 |
to a special object that encapsulates |
| 3336 |
the next method in the sorted method list. |
| 3337 |
|
| 3338 |
Calling without arguments invokes the next method with |
| 3339 |
the same arguments as this method is called with. |
| 3340 |
Passing @var{args} @dots{} explicitly invokes the next method |
| 3341 |
with the passed arguments. |
| 3342 |
|
| 3343 |
If @code{next-method} is called in the least specific method, |
| 3344 |
i.e. there's no "next method", an error is signaled. |
| 3345 |
@c JP |
| 3346 |
この変数は、メソッド本体内部で、ソートされたメソッドリストで次のメソッド |
| 3347 |
をカプセル化した特別なオブジェクトに束縛されます。 |
| 3348 |
|
| 3349 |
引数なしで呼ぶと、次のメソッドがこのメソッドよばれたときとと同じ引数で |
| 3350 |
起動されます。@var{args} @dots{} を明示的にわたすと、次のメソッドがその |
| 3351 |
渡された引数で起動されます。 |
| 3352 |
|
| 3353 |
@code{next-method} がもっとも特定化されていないメソッドで呼ばれた場合、 |
| 3354 |
すなわち、「次のメソッド」がない場合、エラーシグナルがあがります。 |
| 3355 |
@c COMMON |
| 3356 |
@end deffn |
| 3357 |
|
| 3358 |
@c ---------------------------------------------------------------------- |
| 3359 |
@node Metaobject protocol, , Generic function and method, Object system |
| 3360 |
@section Metaobject protocol |
| 3361 |
@c NODE メタオブジェクトプロトコル |
| 3362 |
|
| 3363 |
@c EN |
| 3364 |
In CLOS-like object systems, the object system is built on |
| 3365 |
top of itself---that is, things such as the structure of the class, |
| 3366 |
how a class is created, how an instance is created and initialized, |
| 3367 |
and how a method is dispatched and called, are all defined in terms |
| 3368 |
of the object system. For example, a class is just an instance of |
| 3369 |
the class @code{<class>} that defines a generic structure and behavior |
| 3370 |
of standard classes. If you subclass @code{<class>}, then you can |
| 3371 |
create your own set of classes that behaves differently than the |
| 3372 |
default behavior; in effect, you are creating your own object system. |
| 3373 |
|
| 3374 |
@emph{Metaobject protocols} are the definitions of APIs concerning |
| 3375 |
about how the object systems are built---building-block classes, |
| 3376 |
and the names and orders of generic functions to be called |
| 3377 |
during operations of the object system. Subclassing these classes |
| 3378 |
and specializing these methods are the means of customizing object |
| 3379 |
system behaviors. |
| 3380 |
@c JP |
| 3381 |
CLOS 風のオブジェクトシステムでは、オブジェクトシステムがそれ自身の上に |
| 3382 |
構築されます。すなわち、クラス構造のようなもの、クラスをどのように |
| 3383 |
生成するか、インスタンスをどのように生成し初期化するか、メソッドをどのように |
| 3384 |
ディスパッチし呼び出すか、これらはすべてオブジェクトシステムによって、 |
| 3385 |
定義されます。たとえば、クラスはジェネリックな構造と標準的クラスの |
| 3386 |
振舞いを定義する @code{<class>} クラスのインスタンスです。@code{<class>} |
| 3387 |
をサブクラス化すると、デフォルトのものとは違う振舞いをする、独自の |
| 3388 |
クラス集合をつくることができます。これは結局、独自のオブジェクトシステムを |
| 3389 |
つくることになります。 |
| 3390 |
|
| 3391 |
@emph{メタオブジェクトプロトコル}は、どのようにオブジェクトシステムを |
| 3392 |
構築するかに関連する API 群の定義です。ブロック構築のクラス、オブジェクト |
| 3393 |
システムを操作するあいだに呼ばれるジェネリック関数の名前と順序などです。 |
| 3394 |
これらのクラスをサブクラス化し、これらのメソッドを特定化することは、 |
| 3395 |
オブジェクトシステムの振舞いをカスタマイズすることを意味します。 |
| 3396 |
@c COMMON |
| 3397 |
|
| 3398 |
@menu |
| 3399 |
* Class instantiation:: |
| 3400 |
* Customizing slot access:: |
| 3401 |
* Method instantiation:: |
| 3402 |
* Customizing method application:: |
| 3403 |
* Customizing class redefinition:: |
| 3404 |
@end menu |
| 3405 |
|
| 3406 |
|
| 3407 |
@node Class instantiation, Customizing slot access, Metaobject protocol, Metaobject protocol |
| 3408 |
@subsection Class instantiation |
| 3409 |
@c NODE クラスのインスタンシエーション |
| 3410 |
|
| 3411 |
@c EN |
| 3412 |
Every class is an instance of a group of special classes. |
| 3413 |
A class that can be a class of another class is called @emph{metaclass}. |
| 3414 |
In Gauche, only the @code{<class>} class or its subclasses |
| 3415 |
can be a metaclass. |
| 3416 |
|
| 3417 |
@subsubheading Expansion of @code{define-class} |
| 3418 |
|
| 3419 |
The @code{define-class} macro is basically a wrapper of |
| 3420 |
the code that creates an instance of @code{<class>} |
| 3421 |
(or specified metaclass) and bind it to the given name. |
| 3422 |
Suppose you have the following @code{define-class} form. |
| 3423 |
@c JP |
| 3424 |
すべてのクラスはある特殊なクラスのグループのインスタンスになっています。 |
| 3425 |
他のクラスのクラスになれるようなクラスのことを @emph{メタクラス} と呼びます。 |
| 3426 |
Gauche では @code{<class>} クラスおよびそのサブクラスのみがメタクラスに |
| 3427 |
なれます。 |
| 3428 |
|
| 3429 |
@subsubheading @code{define-class} の展開 |
| 3430 |
|
| 3431 |
@code{define-class} マクロは基本的には @code{<class>} (あるいは指定された |
| 3432 |
メタクラス)のインスタンスを生成するコードのラッパーで、それを与えられた |
| 3433 |
名前に束縛します。以下のような @code{define-class} 形式を前提とします。 |
| 3434 |
|
| 3435 |
@c COMMON |
| 3436 |
@example |
| 3437 |
(define-class @var{name} (@var{supers}) |
| 3438 |
@var{slot-specs} |
| 3439 |
@var{options} @dots{}) |
| 3440 |
@end example |
| 3441 |
|
| 3442 |
@c EN |
| 3443 |
It is expanded into a form like this (you can see the exact form |
| 3444 |
by looking at the definition of @code{define-class} macro in |
| 3445 |
@code{src/libobj.scm} of the source code tree. |
| 3446 |
@c JP |
| 3447 |
これを次のように展開します。 |
| 3448 |
(完全な展開形を知りたければ、ソースツリーの@code{src/libobj.scm} |
| 3449 |
にある@code{default-class}の定義を見てください。) |
| 3450 |
@c COMMON |
| 3451 |
@example |
| 3452 |
(define @var{name} |
| 3453 |
(let ((tmp1 (make @var{metaclass} |
| 3454 |
:name '@var{name} :supers (list @var{supers}) |
| 3455 |
:slots (map @var{process-slot-definitions} |
| 3456 |
@var{slot-specs}) |
| 3457 |
:defined-modules (list (current-module)) |
| 3458 |
@var{options} @dots{}))) |
| 3459 |
@dots{} @var{check class redefinition} @dots{} |
| 3460 |
@dots{} @var{registering accessor methods} @dots{} |
| 3461 |
tmp1)) |
| 3462 |
@end example |
| 3463 |
|
| 3464 |
@c EN |
| 3465 |
The created class's class, i.e. @var{metaclass}, |
| 3466 |
is determined by the following rules. |
| 3467 |
@c JP |
| 3468 |
生成されるクラスのクラス、つまり、@var{metaclass} は以下のルールで |
| 3469 |
決定されます。 |
| 3470 |
@c COMMON |
| 3471 |
|
| 3472 |
@enumerate |
| 3473 |
@c EN |
| 3474 |
@item |
| 3475 |
If @code{:metaclass} option is given to the @code{define-class} |
| 3476 |
macro, its value is used. The value must be the @code{<class>} |
| 3477 |
class or its descendants. |
| 3478 |
@item |
| 3479 |
Otherwise, the metaclasses of the classes in the class precedence list |
| 3480 |
is examined. |
| 3481 |
@itemize @bullet |
| 3482 |
@item |
| 3483 |
If all the metaclasses are @code{<class>}, then |
| 3484 |
the created class's metaclass is also @code{<class>}. |
| 3485 |
@item |
| 3486 |
If all the metaclasses are either @code{<class>} or another metaclass |
| 3487 |
@code{A}, then the created class' metaclass is @code{A}. |
| 3488 |
@item |
| 3489 |
If the set of metaclasses contains more than one metaclass |
| 3490 |
(@code{A}, @code{B}, @code{C} @dots{}) other than @code{<class>}, |
| 3491 |
then the created class' metaclass |
| 3492 |
is a metaclass that inherits all of those metaclasses |
| 3493 |
@code{A}, @code{B}, @code{C} @dots{}. |
| 3494 |
@end itemize |
| 3495 |
@c JP |
| 3496 |
@item |
| 3497 |
もし、@code{:metaclass} オプションが @code{define-class} マクロに |
| 3498 |
与えられていれば、その値を使います。その値は、@code{<class>} クラスか |
| 3499 |
あるいはその子孫でなければなりません。 |
| 3500 |
@item |
| 3501 |
さもなければ、クラス順位リスト中のクラスのメタクラスが試されます。 |
| 3502 |
@itemize @bullet |
| 3503 |
@item |
| 3504 |
もし、すべてのメタクラスが @code{<class>} であるなら、生成される |
| 3505 |
クラスのメタクラスも @code{<class>} になります。 |
| 3506 |
@item |
| 3507 |
もし、すべてのメタクラスが @code{<class>} かあるいは別のメタクラス |
| 3508 |
@code{A} のどちらかであれば、生成されるクラスのメタクラスは、@code{A} に |
| 3509 |
なります。 |
| 3510 |
@item |
| 3511 |
もしメタクラスの集合が @code{<class>} 以外の 2つ以上のメタクラス |
| 3512 |
(@code{A}, @code{B}, @code{C} @dots{})を含む場合、生成されるクラスの |
| 3513 |
メタクラスはこれらのメタクラス @code{A}, @code{B}, @code{C} @dots{} すべてを |
| 3514 |
継承したメタクラスになります。 |
| 3515 |
@end itemize |
| 3516 |
@c COMMON |
| 3517 |
@end enumerate |
| 3518 |
|
| 3519 |
@c EN |
| 3520 |
The class's name, superclasses, and slot definitions are passed |
| 3521 |
as the initialization arguments to the @code{make} generic function, |
| 3522 |
with other arguments passed to @code{define-class}. |
| 3523 |
The initialization argument @code{defined-modules} is passed to |
| 3524 |
remember which module the class is defined, for the redefinition of |
| 3525 |
this class. |
| 3526 |
|
| 3527 |
The slot specifications @var{slot-specs} are processed |
| 3528 |
by internal method @var{process-slot-definitions} |
| 3529 |
(which can't be directly called) to be turned |
| 3530 |
into slot definitions. Specifically, an @code{:init-form} slot |
| 3531 |
option is turned into an @code{:init-thunk} option, and |
| 3532 |
@code{:getter}, @code{:setter} and @code{:accessor} slot |
| 3533 |
options are quoted. |
| 3534 |
|
| 3535 |
After the class (an instance of @var{metaclass}) is created, |
| 3536 |
the global binding of @var{name} is checked. If it is bound |
| 3537 |
to a class, then the class redefinition protocol is invoked |
| 3538 |
(see @ref{Class redefinition}). |
| 3539 |
|
| 3540 |
Then, the methods given to @code{:getter}, @code{:setter} |
| 3541 |
and @code{:accessor} slot options in @var{slot-spec} are |
| 3542 |
collected and registered to the corresponding generic functions. |
| 3543 |
@c JP |
| 3544 |
クラスの名前、スーパークラス、スロットの定義は初期化引数として |
| 3545 |
ジェネリック関数 @code{make} に引き渡されます。また、 |
| 3546 |
@code{define-class} のスロット定義以降に渡されたキーワード-値リストも |
| 3547 |
追加の初期化引数として @code{make} に渡されます。 |
| 3548 |
初期化引数 @code{define-modules} は |
| 3549 |
どのモジュールでそのクラスが定義されたかを覚えておくためにのものです。 |
| 3550 |
これはこのクラスの再定義の時に使われます。 |
| 3551 |
|
| 3552 |
スロットの仕様 @var{slot-specs} は内部メソッド @var{process-slot-definitions} |
| 3553 |
(これは直接呼び出すことはできません)で処理され、スロット定義になります。 |
| 3554 |
厳密には、@code{:init-form} スロットオプションは、@code{:init-thunk} オプション |
| 3555 |
になり、@code{:getter}、@code{:setter}、@code{:accessor} のスロットオプションは |
| 3556 |
引用されます。 |
| 3557 |
|
| 3558 |
クラス(@var{metaclass} のインスタンス)が生成された後、@var{name} のグローバル |
| 3559 |
な束縛がチェックされます。それが、クラスに束縛されていれば、クラスの再定義 |
| 3560 |
プロトコルが起動されます(@ref{Class redefinition} 参照)。 |
| 3561 |
|
| 3562 |
その後、@var{slot-specs} 中で、@code{:getter}、@code{:setter}、 |
| 3563 |
@code{:accessor} スロットオプションに与えられたメソッドが集められ、対応する |
| 3564 |
ジェネリック関数に登録されます。 |
| 3565 |
@c COMMON |
| 3566 |
|
| 3567 |
@c EN |
| 3568 |
@subsubheading Class structure |
| 3569 |
@c JP |
| 3570 |
@subsubheading クラス構造 |
| 3571 |
@c COMMON |
| 3572 |
|
| 3573 |
@deftp {Class} <class> |
| 3574 |
@clindex class |
| 3575 |
@c EN |
| 3576 |
The base class of all metaclasses, @code{<class>}, has |
| 3577 |
the following slots. Note that these slots are for internal management, |
| 3578 |
and users can't change those information freely once the class is |
| 3579 |
initialized. |
| 3580 |
|
| 3581 |
It is recommended to obtain information |
| 3582 |
about a class by procedures described in @ref{Class object}, instead |
| 3583 |
of directly accessing those slots. |
| 3584 |
@c JP |
| 3585 |
すべてのメタクラスのベースクラスである @code{<class>} は以下のような |
| 3586 |
スロットを持っています。これらのスロットは内部的な管理のためにあるので |
| 3587 |
クラスが初期化された後に、これらの値を自由に変更することはできません。 |
| 3588 |
|
| 3589 |
クラスの情報を得るには、これらのスロットに直接 |
| 3590 |
アクセスするのではなく、@ref{Class object} にある手続きを使うことをおすすめします。 |
| 3591 |
@c COMMON |
| 3592 |
|
| 3593 |
|
| 3594 |
@defivar <class> name |
| 3595 |
@c EN |
| 3596 |
The name of the class; the symbol given to @code{define-class} macro. |
| 3597 |
@code{class-name} returns this value. |
| 3598 |
@c JP |
| 3599 |
クラスの名前、@code{define-class} マクロに与えられたシンボルです。 |
| 3600 |
@code{class-name} はこの値を返します。 |
| 3601 |
@c COMMON |
| 3602 |
@end defivar |
| 3603 |
|
| 3604 |
@defivar <class> cpl |
| 3605 |
@c EN |
| 3606 |
Class precedence list. @code{class-precedence-list} returns this value. |
| 3607 |
@c JP |
| 3608 |
クラス順位リストです。@code{class-precedence-list} はこの値を返します。 |
| 3609 |
@c COMMON |
| 3610 |
@end defivar |
| 3611 |
|
| 3612 |
@defivar <class> direct-supers |
| 3613 |
@c EN |
| 3614 |
The list of direct superclasses. |
| 3615 |
@code{class-direct-supers} returns this value. |
| 3616 |
@c JP |
| 3617 |
直接スーパークラスのリストです。 |
| 3618 |
@code{class-direct-supers} はこの値を返します。 |
| 3619 |
@c COMMON |
| 3620 |
@end defivar |
| 3621 |
|
| 3622 |
@defivar <class> accessors |
| 3623 |
@c EN |
| 3624 |
An assoc list of slot accessors---it encapsulates how each slot |
| 3625 |
should be accessed. |
| 3626 |
@c JP |
| 3627 |
スロットアクセサの連想リストです。これは各スロットがどのようにアクセスされる |
| 3628 |
べきかをカプセル化しています。 |
| 3629 |
@c COMMON |
| 3630 |
@end defivar |
| 3631 |
|
| 3632 |
@defivar <class> slots |
| 3633 |
@c EN |
| 3634 |
A list of slot definitions. @code{class-slots} returns this value. |
| 3635 |
See @ref{Slot definition object}, for the details of slot definitions. |
| 3636 |
@c JP |
| 3637 |
スロット定義のリストです。@code{class-slots} はこの値を返します。 |
| 3638 |
スロット定義についての詳細は、@ref{Slot definition object} を参照してください。 |
| 3639 |
@c COMMON |
| 3640 |
@end defivar |
| 3641 |
|
| 3642 |
@defivar <class> direct-slots |
| 3643 |
@c EN |
| 3644 |
A list of slot definitions that is directly specified in this |
| 3645 |
class definition (i.e. not inherited). |
| 3646 |
@code{class-direct-slots} returns this value. |
| 3647 |
@c JP |
| 3648 |
このクラスの定義で直接指定された(つまり継承したものではない)スロット定義の |
| 3649 |
リストです。@code{class-direct-slots} はこの値を返します。 |
| 3650 |
@c COMMON |
| 3651 |
@end defivar |
| 3652 |
|
| 3653 |
@defivar <class> num-instance-slots |
| 3654 |
@c EN |
| 3655 |
The number of instance allocated slots. |
| 3656 |
@c JP |
| 3657 |
インスタンスにアロケートされるスロットの数です。 |
| 3658 |
@c COMMON |
| 3659 |
@end defivar |
| 3660 |
|
| 3661 |
@defivar <class> direct-subclasses |
| 3662 |
@c EN |
| 3663 |
A list of classes that directly inherits this class. |
| 3664 |
@code{class-direct-subclasses} returns this value. |
| 3665 |
@c JP |
| 3666 |
このクラスを直接継承しているクラスのリストです。 |
| 3667 |
@code{class-direct-subclasses} はこの値を返します。 |
| 3668 |
@c COMMON |
| 3669 |
@end defivar |
| 3670 |
|
| 3671 |
@defivar <class> direct-methods |
| 3672 |
@c EN |
| 3673 |
A list of methods that has this class in its specializer list. |
| 3674 |
@code{class-direct-methods} returns this value. |
| 3675 |
@c JP |
| 3676 |
このクラスを特定化子リスト中にもつメソッドのリストです。 |
| 3677 |
@code{class-direct-methods} はこの値を返します。 |
| 3678 |
@c COMMON |
| 3679 |
@end defivar |
| 3680 |
|
| 3681 |
@defivar <class> initargs |
| 3682 |
@c EN |
| 3683 |
The initialization argument list when this class is created. |
| 3684 |
The information is used to initialize redefined class |
| 3685 |
(see @ref{Class redefinition}). |
| 3686 |
@c JP |
| 3687 |
このクラスが生成されるときの初期化引数リストです。この情報は |
| 3688 |
再定義されたクラスを初期化するのに使います(@ref{Class redefinition} 参照)。 |
| 3689 |
@c COMMON |
| 3690 |
@end defivar |
| 3691 |
|
| 3692 |
@defivar <class> defined-modules |
| 3693 |
@c EN |
| 3694 |
A list of modules where this class has a global binding. |
| 3695 |
@c JP |
| 3696 |
このクラスがグローバル束縛をもつモジュールのリストです。 |
| 3697 |
@c COMMON |
| 3698 |
@end defivar |
| 3699 |
|
| 3700 |
@defivar <class> redefined |
| 3701 |
@c EN |
| 3702 |
If this class has been redefined, this slot contains a reference |
| 3703 |
to the new class. Otherwise, this slot has @code{#f}. |
| 3704 |
@c JP |
| 3705 |
このクラスが再定義された場合、このスロットは新しいクラスへの参照を含みます。 |
| 3706 |
そうでない場合にはこのスロットは @code{#f} をもっています。 |
| 3707 |
@c COMMON |
| 3708 |
@end defivar |
| 3709 |
|
| 3710 |
@defivar <class> category |
| 3711 |
@c EN |
| 3712 |
The value of this slot indicates how this class is created. |
| 3713 |
Scheme defined class has a symbol @code{scheme}. Other values |
| 3714 |
are for internal use. |
| 3715 |
@c JP |
| 3716 |
このスロットの値は、このクラスがどのように生成されたかを示しています。 |
| 3717 |
Scheme 定義のクラスは、@code{scheme} というシンボルを持っています。それ以外の |
| 3718 |
値は内部的に使用するだけです。 |
| 3719 |
@c COMMON |
| 3720 |
@end defivar |
| 3721 |
@end deftp |
| 3722 |
|
| 3723 |
@c EN |
| 3724 |
@subsubheading The initialize method for @code{<class>} |
| 3725 |
@c JP |
| 3726 |
@subsubheading @code{<class>} 用の initialize メソッド |
| 3727 |
@c COMMON |
| 3728 |
|
| 3729 |
@deffn {Method} initialize (class <class>) initargs |
| 3730 |
@c EN |
| 3731 |
The @code{define-class} macro expands into a call of |
| 3732 |
@code{(make <class> @dots{})}, which allocates a class metaobject |
| 3733 |
and calls @code{initialize} method. This method takes care of |
| 3734 |
computing inheritance order (class precedence list) and calculate slots, |
| 3735 |
and set up various internal slots. Then, at the very end of |
| 3736 |
this method, it @emph{freezes} the essential class slots; they became |
| 3737 |
immutable. |
| 3738 |
@c JP |
| 3739 |
@code{define-class}マクロは、@code{(make <class> @dots{})}という呼び出しへと |
| 3740 |
展開されます。@code{make}はクラスメタオブジェクトをアロケートし、 |
| 3741 |
その@code{initialize}メソッドを呼びます。 |
| 3742 |
このメソッド内で、クラスの継承順位(class precedence list)や |
| 3743 |
持つべきスロットが計算され、クラス内部のスロットに適切な値が与えられます。 |
| 3744 |
そしてこのメソッドの末尾で、クラスの重要なスロットが@emph{凍結}され、 |
| 3745 |
以降の変更が禁止されます。 |
| 3746 |
@c COMMON |
| 3747 |
|
| 3748 |
@c EN |
| 3749 |
Calculation of inheritance and slots are handle by generic |
| 3750 |
functions. If you define a metaclass, you can define methods |
| 3751 |
for them to customize how those calculations are done. |
| 3752 |
Class inheritance is calculated by @code{compute-cpl} defined below. |
| 3753 |
Slot calculation is a bit involved, and explained in the |
| 3754 |
next subsection (@pxref{Customizing slot access}). |
| 3755 |
@c JP |
| 3756 |
継承とスロットの計算はジェネリックファンクションにより行われます。 |
| 3757 |
メタクラスを定義し、それに適切なメソッドを定義することで、 |
| 3758 |
振る舞いをカスタマイズできます。クラスの継承は、下に示す |
| 3759 |
@code{compute-cpl}メソッドにより計算されます。 |
| 3760 |
スロットの計算はもうちょっと複雑なので、次のサブセクションで説明します |
| 3761 |
(@ref{Customizing slot access})。 |
| 3762 |
@c COMMON |
| 3763 |
|
| 3764 |
@c EN |
| 3765 |
If your class needs to initialize auxiliary slots, you can |
| 3766 |
define your own @code{initialize} method on its metaclass, in which you call |
| 3767 |
@code{next-method} first to set up the core part of the |
| 3768 |
@code{<class>} structure, then you sets up class-specific part. |
| 3769 |
One caveat is that, after @code{next-method} handles |
| 3770 |
initialization of the core @code{<class>} part, you can no longer |
| 3771 |
modify essential class slots. If you need to tweak those slots, |
| 3772 |
you can override @code{class-post-initialize} method, which |
| 3773 |
is called right before the core class slots are frozen. |
| 3774 |
@c JP |
| 3775 |
クラスが独自のスロットを持っていて初期化が必要な場合は、 |
| 3776 |
メタクラスに@code{initialize}メソッドを定義し、 |
| 3777 |
その中でまず@code{next-method}を呼んで@code{<class>}構造の基本部分部分を |
| 3778 |
初期化してから、独自部分を初期化します。 |
| 3779 |
@code{next-method}により@code{<class>}の基本部分が初期化されたら、 |
| 3780 |
それはもう変更できないことに注意してください。 |
| 3781 |
基本部分のスロットに手を加えたい場合は、 |
| 3782 |
クラスの基本部分が凍結される直前に呼ばれる |
| 3783 |
@code{class-post-initialize}メソッドをオーバライドすることにより可能です。 |
| 3784 |
@c COMMON |
| 3785 |
@end deffn |
| 3786 |
|
| 3787 |
@deffn {Generic function} compute-cpl class |
| 3788 |
@c EN |
| 3789 |
This generic function is called from @code{initialize} method |
| 3790 |
on @code{<class>}, and responsible to compute the class |
| 3791 |
precedence list (CPL). |
| 3792 |
|
| 3793 |
At the time this generic function is called, |
| 3794 |
only @code{name} and @code{direct-supers} slots of @var{class} |
| 3795 |
are set. The @code{direct-supers} slot contains a list of |
| 3796 |
classes @var{class} directly inherits from. All classes in |
| 3797 |
it is already initialized. |
| 3798 |
|
| 3799 |
It must return a list of classes, starting with @var{class} |
| 3800 |
itself and ending with @code{<top>}, representing the order |
| 3801 |
of precedence with which methods are searched. The method |
| 3802 |
defined for @code{<class>} uses C3 linearlization, which |
| 3803 |
topologically sorts all the classes involved in the inheritance. |
| 3804 |
|
| 3805 |
Override this method if you need to change how CPL is computed. |
| 3806 |
You might not want to change the actual algorithm unless you emulate |
| 3807 |
different object system, but you can use the method to ensure |
| 3808 |
certain class is always inherited, for example. |
| 3809 |
@c JP |
| 3810 |
@code{<class>}に定義されている@code{initialize}メソッドから呼ばれ、 |
| 3811 |
クラスの継承優先順リスト(class precedence list, CPL)を計算する |
| 3812 |
ジェネリックファンクションです。 |
| 3813 |
|
| 3814 |
これが呼ばれる時点で、クラスメタオブジェクト |
| 3815 |
@var{class}は@code{name}および@code{direct-supers}スロットだけが |
| 3816 |
設定されています。@code{direct-supers}スロットの値は@var{class}が直接継承する |
| 3817 |
クラスのリストです。ここに含まれるクラスは全て初期化済みです。 |
| 3818 |
|
| 3819 |
返り値はクラスのリストで、@var{class}から始まり、@code{<top>}で終わっていなければ |
| 3820 |
なりません。このリストの順に、マッチするメソッドが探されます。 |
| 3821 |
@code{<class>}に対して定義されているメソッドはC3線形化アルゴリズムを使用します。 |
| 3822 |
これは、直接間接を問わず継承している全てのクラスをトポロジカルソートするものです。 |
| 3823 |
|
| 3824 |
CPLの計算をカスタマイズしたい場合にこのメソッドをオーバライドしてください。 |
| 3825 |
計算のアルゴリズムを変えることは、他のオブジェクトシステムをエミュレートするというのでも |
| 3826 |
なければあまり必要がないかもしれませんが、例えば自分のメタクラスでは |
| 3827 |
必ずあるクラスを継承することを保証する、といった使い方もできます。 |
| 3828 |
@c COMMON |
| 3829 |
@end deffn |
| 3830 |
|
| 3831 |
@deffn {Generic function} class-post-initialize class initargs |
| 3832 |
@c EN |
| 3833 |
This generic function is called after all core initialization of @var{class} |
| 3834 |
is finished, but before the class is ``freezed'', that is, the essential |
| 3835 |
parts of @var{class} becomes immutable. If you want to trick |
| 3836 |
object system in some weird way, override this method. |
| 3837 |
|
| 3838 |
We assume you know what you are doing, for object system assumes the essential |
| 3839 |
parts are computed in the standard way. Messing with them |
| 3840 |
can easily break the system. |
| 3841 |
@c JP |
| 3842 |
このジェネリックファンクションは、@var{class}のコア部分の初期化が終わった後、 |
| 3843 |
クラスが「凍結」される、つまくクラスの主要なスロットが変更不可になる直前に呼ばれます。 |
| 3844 |
このメソッドをオーバライドすることで、オブジェクトシステムを「騙す」ことができます。 |
| 3845 |
|
| 3846 |
自分が何をしているかわかっている場合のみ、このメソッドをオーバライドしてください。 |
| 3847 |
オブジェクトシステムは、クラスのコア部分が標準的な方法で設定されていることを前提に |
| 3848 |
動作します。それを変えることは容易にシステムを壊します。 |
| 3849 |
@c COMMON |
| 3850 |
@end deffn |
| 3851 |
|
| 3852 |
|
| 3853 |
@node Customizing slot access, Method instantiation, Class instantiation, Metaobject protocol |
| 3854 |
@subsection Customizing slot access |
| 3855 |
@c NODE スロットアクセスのカスタマイズ |
| 3856 |
|
| 3857 |
|
| 3858 |
@deffn {Generic Function} compute-slots class |
| 3859 |
@deffnx {Generic Function} compute-get-n-set class slot-definition |
| 3860 |
@c EN |
| 3861 |
These two generic functions are responsible to determine |
| 3862 |
what slots a class has, and how each slot is accessed. |
| 3863 |
@c JP |
| 3864 |
これらふたつのジェネリックファンクションによって、クラスの持つべきスロット、 |
| 3865 |
及び各スロットがどのようにアクセスされるかが決定されます。 |
| 3866 |
@c COMMON |
| 3867 |
|
| 3868 |
@c EN |
| 3869 |
In the @code{initialize} method of a class, |
| 3870 |
@code{compute-slots} is called after the class's |
| 3871 |
@code{direct-supers}, @code{cpl} and @code{direct-slots} are set. |
| 3872 |
It must decide what slots the class should have, and what |
| 3873 |
slot options each slot should have, based on those three piece |
| 3874 |
of information. The returned value should have the following |
| 3875 |
form, and it is used as the value of the @code{slots} slot of the class. |
| 3876 |
@c JP |
| 3877 |
クラスの@code{initialize}メソッドは、クラスの |
| 3878 |
@code{direct-supers}、@code{cpl}、@code{direct-slots}スロットを |
| 3879 |
セットしてから、@code{compute-slots}メソッドを呼びます。 |
| 3880 |
このメソッドは既にセットされた3つのスロットの情報から、 |
| 3881 |
該当クラスの持つべきスロットと、 |
| 3882 |
各スロットのスロットオプションを決定します。 |
| 3883 |
メソッドの返り値は以下の形式のフォームで、これがクラスの@code{slots}スロットに |
| 3884 |
セットされます。 |
| 3885 |
@c COMMON |
| 3886 |
|
| 3887 |
@example |
| 3888 |
<slots> : (<slot-definition> ...) |
| 3889 |
<slot-definition> : (<slot-name> . <slot-options>) |
| 3890 |
<slot-name> : symbol |
| 3891 |
<slot-options> : keyword-value alternating list. |
| 3892 |
@end example |
| 3893 |
|
| 3894 |
@c EN |
| 3895 |
After the @code{slots} slot of the class is set by the returned |
| 3896 |
value from @code{compute-slots}, |
| 3897 |
@code{compute-get-n-set} is called for each slot |
| 3898 |
to calculate how to access and modify the slot. |
| 3899 |
The class and the slot definition are the arguments. |
| 3900 |
It must return either one of the followings: |
| 3901 |
@c JP |
| 3902 |
@code{compute-slots}の返り値によって@code{slots}スロットが設定されたら、 |
| 3903 |
次に各スロットについて@code{compute-get-n-set}が呼ばれます。 |
| 3904 |
このメソッドは、各スロットをどのようにアクセスするかを決定します。 |
| 3905 |
引数はクラスとスロット定義(上の@code{<slot-definition>})です。 |
| 3906 |
返り値は以下のいずれかでなけばなりません。 |
| 3907 |
@c COMMON |
| 3908 |
|
| 3909 |
@table @asis |
| 3910 |
@c EN |
| 3911 |
@item an integer @var{n} |
| 3912 |
This slot becomes @var{n}-th instance slot. This is the only way to |
| 3913 |
allocate a slot per instance. |
| 3914 |
|
| 3915 |
The base method of @var{compute-get-n-set} keeps track of the current |
| 3916 |
number of allocated instance slots in the class's @code{num-instance-slots} |
| 3917 |
slot. It is not recommended for other specialized methods to use or |
| 3918 |
change the value of this slot, unless you know a very good reason to |
| 3919 |
override the object system behavior in deep down. Usually it is suffice |
| 3920 |
to call @code{next-method} to let the base method reserve an instance |
| 3921 |
slot for you. |
| 3922 |
|
| 3923 |
See the examples below for modifying instance slot access behaviors. |
| 3924 |
@c JP |
| 3925 |
@item 整数@var{n} |
| 3926 |
このスロットは@var{n}番目のインスタンススロットになります。 |
| 3927 |
インスタンスにスロットを割り当てる唯一の方法です。 |
| 3928 |
|
| 3929 |
@var{compute-get-n-set}のベースメソッドは、それまでに割り当てられた |
| 3930 |
インスタンススロットの数をクラスの@code{num-instance-slots}スロットに格納しています。 |
| 3931 |
他の特殊化されたメソッドでこのスロットの値を参照したり変更したりすることは |
| 3932 |
避けてください(オブジェクトシステムの中身を知悉していて、そうすべき十分な理由が |
| 3933 |
ある場合は別ですが。) |
| 3934 |
通常の場合、単に@code{next-method}を呼び出せば、ベースメソッドが |
| 3935 |
インスタンススロットを新たに割り当ててそのインデックスを返してくれます。 |
| 3936 |
|
| 3937 |
インスタンススロットアクセスのふるまいを変更する、下に示す例も参照してください。 |
| 3938 |
@c COMMON |
| 3939 |
|
| 3940 |
@item a list @code{(@var{get-proc} @var{set-proc} @var{bound?-proc} @var{initializable})} |
| 3941 |
The @var{get-proc}, @var{set-proc} and @var{bound?-proc} elements are procedures |
| 3942 |
invoked when this slot of an instance is accessed (either via |
| 3943 |
@code{slot-ref}/@code{slot-set!}/@code{slot-bound?}, |
| 3944 |
or an accessor method specified by @code{:getter}/@code{:setter} slot options). |
| 3945 |
The value other than @var{get-proc} may be @code{#f}, and can be omitted |
| 3946 |
if all the values after it is also @code{#f}. That is, the simplest |
| 3947 |
form of this type of return value is a list of one element, |
| 3948 |
@var{get-proc}. |
| 3949 |
|
| 3950 |
@itemize @bullet |
| 3951 |
@item |
| 3952 |
When this slot is about to be read, @var{get-proc} is called with |
| 3953 |
an argument, the instance. The returned value of @var{get-proc} is |
| 3954 |
the value of the slot. |
| 3955 |
|
| 3956 |
The procedure may return @code{#<undef>} to indicate the slot is |
| 3957 |
unbound. It triggers the @code{slot-unbound} generic function. |
| 3958 |
(That is, this type of slot cannot have @code{#<undef>} as its value.) |
| 3959 |
|
| 3960 |
@item |
| 3961 |
When this slot is about to be written, @var{set-proc} is called |
| 3962 |
with two arguments, the instance and the new value. It is called |
| 3963 |
purely for the side effect; the procedure may change the value |
| 3964 |
of other slot of the instance, for example. |
| 3965 |
|
| 3966 |
If this element is @code{#f} or omitted, the slot becomes read-only; |
| 3967 |
any attempt to write to the slot will raise an error. |
| 3968 |
|
| 3969 |
@item |
| 3970 |
When @code{slot-bound?} is called to check whether the slot of |
| 3971 |
an instance is bound, @var{bound?-proc} is called with |
| 3972 |
an argument, the instance. It should return a boolean value |
| 3973 |
which will be the result of @code{slot-bound?}. |
| 3974 |
|
| 3975 |
If this element is @code{#f} or omitted, @code{slot-bound?} will |
| 3976 |
call @var{get-proc} and returns true if it returns |
| 3977 |
@code{#<undef>}. |
| 3978 |
|
| 3979 |
@item |
| 3980 |
The last element, @var{initializable}, is a flag that indicates |
| 3981 |
whether this slot should be initialized when @code{:init-value} |
| 3982 |
or @code{:init-form}. |
| 3983 |
@end itemize |
| 3984 |
|
| 3985 |
@item A @code{<slot-accessor>} object |
| 3986 |
Access to this slot is redirected through the returned |
| 3987 |
slot-accessor object. See below for more on @code{<slot-accessor>}. |
| 3988 |
@end table |
| 3989 |
|
| 3990 |
The value returned by @code{compute-get-n-set} is immediately passed |
| 3991 |
to @code{compute-slot-accessor} to create a @emph{slot accessor} object, |
| 3992 |
which encapsulates how to access and modify the slot. |
| 3993 |
|
| 3994 |
After all slot definitions are processed by @code{compute-get-n-set} |
| 3995 |
and @code{compute-slot-accessor}, an assoc list of |
| 3996 |
slot names and @code{<slot-accessor>} objects are stored in the |
| 3997 |
class's @code{accessors} slot. |
| 3998 |
@end deffn |
| 3999 |
|
| 4000 |
@deffn {Generic Function} compute-slot-accessor |
| 4001 |
@deffnx {Method} compute-slot-accessor (class <class>) slot access-specifier |
| 4002 |
@var{Access-specifier} is a value returned from |
| 4003 |
@code{compute-get-n-set}. The base method creates an instance |
| 4004 |
of @code{<slot-accessor>} that encapsulates how to |
| 4005 |
access the given slot. |
| 4006 |
|
| 4007 |
Created slot accessor objects are stored (as an assoc list using |
| 4008 |
slot names as keys) in the class's @code{accessors} slot. |
| 4009 |
Standard slot accessors and mutators, such as @code{slot-ref}, |
| 4010 |
@code{slot-set!}, @code{slot-bound?}, and the slot accessor |
| 4011 |
methods specified in @code{:getter}, @code{:setter} and @code{:accessor} |
| 4012 |
slot options, all go through slot accessor object eventually. |
| 4013 |
Specifically, those functions and methods first looks up |
| 4014 |
the slot accessor object of the desired slot, then calls |
| 4015 |
@code{slot-ref-using-accessor} etc. |
| 4016 |
@end deffn |
| 4017 |
|
| 4018 |
@deffn {Method} compute-slots (class <class>) |
| 4019 |
The standard method walks CPL of @var{class} and gathers |
| 4020 |
all direct slots. If slots with the same name are found, |
| 4021 |
the one of a class closer to @var{class} in CPL takes precedence. |
| 4022 |
@end deffn |
| 4023 |
|
| 4024 |
@deffn {Method} compute-get-n-set (class <class>) slot |
| 4025 |
The standard processes the slot definition with the following |
| 4026 |
slot allocations: @code{:instance}, @code{:class}, |
| 4027 |
@code{each-subclass} and @code{:virtual}. |
| 4028 |
@end deffn |
| 4029 |
|
| 4030 |
@defun slot-ref-using-accessor obj slot-accessor |
| 4031 |
@defunx slot-set-using-accessor! obj slot-accessor value |
| 4032 |
@defunx slot-bound-using-accessor? obj slot-accessor |
| 4033 |
@defunx slot-initialize-using-accessor! obj slot-accessor initargs |
| 4034 |
The low-level slot accessing mechanism. Every function or |
| 4035 |
method that needs to read or write to a slot eventually comes |
| 4036 |
down to one of these functions. |
| 4037 |
|
| 4038 |
Ordinary programs need not call these functions directly. |
| 4039 |
If you ever need to call them, you have to be careful not |
| 4040 |
to grab the reference to @var{slot-accessor} too long; if |
| 4041 |
@var{obj}'s class is changed or redefined, @var{slot-accessor} |
| 4042 |
can no longer be used. |
| 4043 |
@end defun |
| 4044 |
|
| 4045 |
Here we show a couple of small examples to illustrate how slot access |
| 4046 |
protocol can be customized. You can also look at @code{gauche.mop.*} |
| 4047 |
modules (in the source tree, look under @code{lib/gauche/mop/}) |
| 4048 |
for more examples. |
| 4049 |
|
| 4050 |
The first example implements the same functionality of |
| 4051 |
@code{:virtual} slot allocation. We add @code{:procedural} |
| 4052 |
slot allocation, which adds @code{:ref}, @code{:set!} and @code{:bound?} |
| 4053 |
slot options. |
| 4054 |
|
| 4055 |
@example |
| 4056 |
(define-class <procedural-slot-meta> (<class>) ()) |
| 4057 |
|
| 4058 |
(define-method compute-get-n-set ((class <procedural-slot-meta>) slot) |
| 4059 |
(if (eqv? (slot-definition-allocation slot) :procedural) |
| 4060 |
(let ([get-proc (slot-definition-option slot :ref)] |
| 4061 |
[set-proc (slot-definition-option slot :set!)] |
| 4062 |
[bound-proc (slot-definition-option slot :bound?)]) |
| 4063 |
(list get-proc set-proc bound-proc)) |
| 4064 |
(next-method))) |
| 4065 |
@end example |
| 4066 |
|
| 4067 |
A specialized @code{compute-get-n-set} is defined on a metaclass |
| 4068 |
@code{<procedural-slot-meta>}. It checks the slot allocation, |
| 4069 |
handles it if it is @code{:procedural}, and delegates other |
| 4070 |
slot allocation cases to @code{next-method}. This is a typical |
| 4071 |
way to add new slot allocation by layering. |
| 4072 |
|
| 4073 |
To use this @code{:procedural} slot, give @code{<procedural-slot-meta>} |
| 4074 |
to a @code{:metaclass} argument of @code{define-class}: |
| 4075 |
|
| 4076 |
@example |
| 4077 |
(define-class <temp> () |
| 4078 |
((temp-c :init-keyword :temp-c :init-value 0) |
| 4079 |
(temp-f :allocation :procedural |
| 4080 |
:ref (lambda (o) (+ (*. (ref o 'temp-c) 9/5) 32)) |
| 4081 |
:set! (lambda (o v) |
| 4082 |
(set! (ref o 'temp-c) (*. (- v 32) 5/9))) |
| 4083 |
:bound? (lambda (o) (slot-bound? o 'temp-c)))) |
| 4084 |
:metaclass <procedural-slot-meta>) |
| 4085 |
@end example |
| 4086 |
|
| 4087 |
An instance of @code{<temp>} keeps a temperature in both |
| 4088 |
Celsius and Fahrenheit. Here's an example interaction. |
| 4089 |
|
| 4090 |
@example |
| 4091 |
gosh> (define T (make <temp>)) |
| 4092 |
T |
| 4093 |
gosh> (d T) |
| 4094 |
#<<temp> 0xb6b5c0> is an instance of class <temp> |
| 4095 |
slots: |
| 4096 |
temp-c : 0 |
| 4097 |
temp-f : 32.0 |
| 4098 |
gosh> (set! (ref T 'temp-c) 100) |
| 4099 |
#<undef> |
| 4100 |
gosh> (d T) |
| 4101 |
#<<temp> 0xb6b5c0> is an instance of class <temp> |
| 4102 |
slots: |
| 4103 |
temp-c : 100 |
| 4104 |
temp-f : 212.0 |
| 4105 |
gosh> (set! (ref T 'temp-f) 450) |
| 4106 |
#<undef> |
| 4107 |
gosh> (d T) |
| 4108 |
#<<temp> 0xb6b5c0> is an instance of class <temp> |
| 4109 |
slots: |
| 4110 |
temp-c : 232.22222222222223 |
| 4111 |
temp-f : 450.0 |
| 4112 |
@end example |
| 4113 |
|
| 4114 |
Our next example is a simpler version of @code{gauche.mop.validator}. |
| 4115 |
We add a slot option @code{:filter}, which takes a procedure |
| 4116 |
that is applied to a value to be set to the slot. |
| 4117 |
|
| 4118 |
@example |
| 4119 |
(define-class <filter-meta> (<class>) ()) |
| 4120 |
|
| 4121 |
(define-method compute-get-n-set ((class <filter-meta>) slot) |
| 4122 |
(cond [(slot-definition-option slot :filter #f) |
| 4123 |
=> (lambda (f) |
| 4124 |
(let1 acc (compute-slot-accessor class slot (next-method)) |
| 4125 |
(list (lambda (o) (slot-ref-using-accessor o acc)) |
| 4126 |
(lambda (o v) (slot-set-using-accessor! o acc (f v))) |
| 4127 |
(lambda (o) (slot-bound-using-accessor? o acc)) |
| 4128 |
#t)))] |
| 4129 |
[else (next-method)])) |
| 4130 |
@end example |
| 4131 |
|
| 4132 |
The trick here is to call @code{next-method} and |
| 4133 |
@code{compute-slot-accessor} to calculate the slot accessor |
| 4134 |
and wrap it. See how this metaclass works: |
| 4135 |
|
| 4136 |
@example |
| 4137 |
(define-class <foo> () |
| 4138 |
((v :init-value 0 :filter x->number)) |
| 4139 |
:metaclass <filter-meta>) |
| 4140 |
|
| 4141 |
gosh> (define foo (make <foo>)) |
| 4142 |
foo |
| 4143 |
gosh> (ref foo'v) |
| 4144 |
0 |
| 4145 |
gosh> (set! (ref foo'v) "123") |
| 4146 |
#<undef> |
| 4147 |
gosh> (ref foo'v) |
| 4148 |
123 |
| 4149 |
@end example |
| 4150 |
|
| 4151 |
|
| 4152 |
|
| 4153 |
|
| 4154 |
|
| 4155 |
@node Method instantiation, Customizing method application, Customizing slot access, Metaobject protocol |
| 4156 |
@subsection Method instantiation |
| 4157 |
@c NODE メソッドのインスタンシエーション |
| 4158 |
|
| 4159 |
@deffn {Method} make (class <method>) :rest initargs |
| 4160 |
@end deffn |
| 4161 |
|
| 4162 |
@node Customizing method application, Customizing class redefinition, Method instantiation, Metaobject protocol |
| 4163 |
@subsection Customizing method application |
| 4164 |
@c NODE メソッド適用のカスタマイズ |
| 4165 |
|
| 4166 |
@deffn {Generic Function} apply-generic gf args |
| 4167 |
@end deffn |
| 4168 |
|
| 4169 |
@deffn {Generic Function} sort-applicable-methods gf methods args |
| 4170 |
@end deffn |
| 4171 |
|
| 4172 |
@deffn {Generic Function} method-more-specific? method1 method2 classes |
| 4173 |
@end deffn |
| 4174 |
|
| 4175 |
@deffn {Generic Function} apply-methods gf methods args |
| 4176 |
@end deffn |
| 4177 |
|
| 4178 |
@deffn {Generic Function} apply-method gf method build-next args |
| 4179 |
@end deffn |
| 4180 |
|
| 4181 |
@node Customizing class redefinition, , Customizing method application, Metaobject protocol |
| 4182 |
@subsection Customizing class redefinition |
| 4183 |
@c NODE クラスの再定義のカスタマイズ |
| 4184 |
|
| 4185 |
@deffn {Generic Function} class-redefinition old-class new-class |
| 4186 |
@c EN |
| 4187 |
When a class is redefined (@pxref{Class redefinition}), |
| 4188 |
a new class metaobject is instantiated by |
| 4189 |
@code{(make @var{metaclass} @var{initargs} @dots{})}, |
| 4190 |
then this generic function is called with the old class metaobject and |
| 4191 |
new class metaobject. It should transform the information in the |
| 4192 |
old class into the new class. |
| 4193 |
@c JP |
| 4194 |
クラスが再定義されると(@ref{Class redefinition}参照)、 |
| 4195 |
まず新たなクラスメタオブジェクトが |
| 4196 |
@code{(make @var{metaclass} @var{initargs} @dots{})}によって生成され、 |
| 4197 |
それからこのジェネリックファンクションが旧クラスメタオブジェクトと |
| 4198 |
新クラスメタオブジェクトを引数にして呼ばれます。 |
| 4199 |
このジェネリックファンクションは、旧クラスの必要な情報を |
| 4200 |
新クラスに移行する役割を担います。 |
| 4201 |
@c COMMON |
| 4202 |
|
| 4203 |
@c EN |
| 4204 |
The default method, @code{(class-redefinition @var{<class>} @var{<class>})}, |
| 4205 |
takes care of updating all the methods referencing to the old class, |
| 4206 |
and propagate changes to the superclasses and subclasses. If you |
| 4207 |
customize this generic function, you should call the default method |
| 4208 |
with @code{next-method} to make sure those basic bookkeeping is done, |
| 4209 |
or unexpected things can happen. |
| 4210 |
@c JP |
| 4211 |
デフォルトメソッド、@code{(class-redefinition @var{<class>} @var{<class>})}は、 |
| 4212 |
旧クラスを参照している全てのメソッドを更新し、 |
| 4213 |
スーパークラスとサブクラスに変更を伝搬します。 |
| 4214 |
このジェネリックファンクションをカスタマイズする際には、 |
| 4215 |
@code{next-method}でデフォルトメソッドを呼び出して、 |
| 4216 |
基本的な更新操作が行われるようにしてください。デフォルトメソッドが呼ばれなかった場合、 |
| 4217 |
意図しない影響が現れるかもしれません。 |
| 4218 |
@c COMMON |
| 4219 |
|
| 4220 |
@c EN |
| 4221 |
Class redefinition mutates lots of structures. If you throw an error |
| 4222 |
in middle of it, the internal state can be left inconsistent. |
| 4223 |
@c JP |
| 4224 |
クラス再定義はたくさんの構造を変更します。途中でエラーを投げて中断してしまった場合、 |
| 4225 |
内部状態の一貫性が失われるかもしれません。 |
| 4226 |
@c COMMON |
| 4227 |
|
| 4228 |
@c EN |
| 4229 |
Internally, the system uses a single mutex dedicated for the class redefition |
| 4230 |
so that only one thread can execute it at a time. You don't need to worry |
| 4231 |
about other thread stepping on during @code{class-redefinition} method |
| 4232 |
(Other thread can still be running for other operations, though, so |
| 4233 |
if you touch objects that can be touched from outside of class redefinition, |
| 4234 |
you should mutex it.) |
| 4235 |
@c JP |
| 4236 |
内部的に、システムはクラス再定義のためのグローバルなmutexを持っていて、 |
| 4237 |
再定義はたかだか一つのスレッドで処理されるようになっていあす。 |
| 4238 |
従って@code{class-redefinition}の途中で他のスレッドが割り込んでクラス構造が |
| 4239 |
変更する心配はしなくて大丈夫です。 |
| 4240 |
(他の操作に関してはマルチスレッドが走っているので、 |
| 4241 |
クラス再定義に関する構造以外の構造を触る時は排他制御が必要です。) |
| 4242 |
@c COMMON |
| 4243 |
@end deffn |
| 4244 |
|
| 4245 |
|
| 4246 |
|
| 4247 |
@c Local variables: |
| 4248 |
@c mode: texinfo |
| 4249 |
@c coding: utf-8 |
| 4250 |
@c end: |