Chicken Scheme中coops对象系统中定义泛型的目的是什么
What is the purpose of define-generic in coops object system in Chicken Scheme
我想弄清楚goops库(http://wiki.call-cc.org/eggref/4/coops)中define-generic的目的是什么,如果它能帮助别人的话,它类似于CLOS(我也不知道CLOS)。
我可以这样定义一个 class 和一个方法:
(define-class <complex> () (x y))
(define-method (sum (a <complex>) (b <complex>))
(make <complex>
'x (+ (slot-value a 'x) (slot-value b 'x))
'y (+ (slot-value a 'y) (slot-value b 'y))))
但是这样做的目的是什么?
(define-generic (sum <complex> <complex>))
来自文档:
[syntax] (define-generic (NAME ARGUMENT ...))
Defines a generic procedure, a procedure specialized for one or more
argument types. ARGUMENT ... defines the number of specialized
arguments this generic procedure shoud use to dispatch to the correct
method. The generic procedure may receive additional arguments, but
those will not be used to determine the method.
谁能给我一个简单的例子来解释 define-method
和 define-generic
之间的区别?
通用过程是一种特殊的对象,由(一个或)几个专用方法组成。
define-generic
定义了这样一个对象,define-method
给它添加方法,如果不存在就创建它。
我想弄清楚goops库(http://wiki.call-cc.org/eggref/4/coops)中define-generic的目的是什么,如果它能帮助别人的话,它类似于CLOS(我也不知道CLOS)。
我可以这样定义一个 class 和一个方法:
(define-class <complex> () (x y))
(define-method (sum (a <complex>) (b <complex>))
(make <complex>
'x (+ (slot-value a 'x) (slot-value b 'x))
'y (+ (slot-value a 'y) (slot-value b 'y))))
但是这样做的目的是什么?
(define-generic (sum <complex> <complex>))
来自文档:
[syntax] (define-generic (NAME ARGUMENT ...))
Defines a generic procedure, a procedure specialized for one or more argument types. ARGUMENT ... defines the number of specialized arguments this generic procedure shoud use to dispatch to the correct method. The generic procedure may receive additional arguments, but those will not be used to determine the method.
谁能给我一个简单的例子来解释 define-method
和 define-generic
之间的区别?
通用过程是一种特殊的对象,由(一个或)几个专用方法组成。
define-generic
定义了这样一个对象,define-method
给它添加方法,如果不存在就创建它。