[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Chicken-janitors] Re: #114: Improvements to tinyclos
From: |
Chicken Trac |
Subject: |
[Chicken-janitors] Re: #114: Improvements to tinyclos |
Date: |
Mon, 16 Nov 2009 01:26:58 -0000 |
#114: Improvements to tinyclos
---------------------------+------------------------------------------------
Reporter: tonysidaway | Owner: tonysidaway
Type: task | Status: accepted
Priority: major | Milestone:
Component: extensions | Version: 4.2.x
Keywords: tinyclos clos |
---------------------------+------------------------------------------------
Comment(by tonysidaway):
Something like this:
{{{
(define (make-a-heap-of-useful-classes)
(let-generic (pos-x pos-y move resize radius width height)
(let*-class ((<pos> () (x y))
(<circle> (<pos>) (radius))
(<oblong> (<pos>) (width height)))
(define-method (move (p <pos>) x y)
(and
(or (not (or (not x) (number? x)))
(not (or (not y) (number? y))))
(error "move <pos>: args must be #f or numeric"))
(and x (slot-set! p 'x x))
(and y (slot-set! p 'y y)))
(define-method (resize (c <circle>) (r <number>))
(slot-set! c 'radius r))
(define-method (resize (o <oblong>) (w <number>) (h <number>))
(slot-set! o 'w w)(slot-set! o 'h h))
(define-method (pos-x (p <pos>) (slot-ref p 'x))
(define-method (pos-y (p <pos>) (slot-ref o 'y))
(define-method (pos-x-set! (p <pos>) (x <number>))
(slot-set! p 'x x))
(define-method (pos-y-set! (p <pos>) (y <number>))
(slot-set! p 'y y))
(define-method (radius (c <circle>)) (slot-ref c 'radius))
(define-method (width (o <oblong>)) (slot-ref o 'width))
(define-method (width-set! (o <oblong>) (w <number>)
(slot-set! o 'width w))
(define-method (height-set! (o <oblong>) (h <number>)
(slot-set! o 'height h))
(define-method (height (o <oblong>)) (slot-ref o 'height))
(values (list <pos> <circle> <rectangle>)
(list pos-x pos-x-set! pos-y pos-y-set! move
resize radius height)))))
}}}
Why go through all that? Because it encloses all the crap you have to go
through to make those useful classes. The calling procedure doesn't need
to know about generics, classes and whatnot, it doesn't even need to know
about tinyclos. It only needs to know how to do the usual Scheme things:
call a procedure and handle the results. Possibly even a cleverly written
macro could receive the results and blend them in;
{{{
(define-classes make-a-heap-of-useful-classes)
(let ((p (make <pos>)))
(move p 10 20)
(move p #f (+ (pos-y p) 7)))
}}}
define-class semantics as used above are a little crude--there's no way to
hide the symbolic links to the slots. It would be good to enable hiding
of slots so that a truly encapsulated class system could be written.
There's nothing to stop the programmer using make-class, of course, and
that would make it easy to do that. But there ought to be a simple,
unmessy way to do it all. A renaming scheme for slots would do that I
suppose.
--
Ticket URL: <http://www.irp.oist.jp/trac/chicken/ticket/114#comment:6>
Chicken Scheme <http://www.call-with-current-continuation.org/>
Chicken Scheme is a compiler for the Scheme programming language.
- [Chicken-janitors] #114: Improvements to tinyclos, Chicken Trac, 2009/11/10
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/11
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/11
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/12
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/15
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/15
- [Chicken-janitors] Re: #114: Improvements to tinyclos,
Chicken Trac <=
- [Chicken-janitors] Re: #114: Improvements to tinyclos, Chicken Trac, 2009/11/18