factor/core/generic/methods.factor

71 lines
1.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2007 Slava Pestov.
2006-08-02 02:49:23 -04:00
! See http://factorcode.org/license.txt for BSD license.
IN: generic
USING: arrays definitions errors assocs help kernel
sequences words namespaces quotations ;
2006-08-02 02:49:23 -04:00
2006-11-11 00:43:24 -05:00
TUPLE: method loc def ;
2006-08-02 03:49:13 -04:00
M: f method-def ;
M: f method-loc ;
M: quotation method-def ;
M: quotation method-loc drop f ;
2006-08-16 21:55:53 -04:00
: method ( class generic -- method/f )
"methods" word-prop at ;
2006-08-02 03:10:09 -04:00
PREDICATE: pair method-spec
first2 dup generic? [ method >boolean ] [ 2drop f ] if ;
2006-08-16 21:55:53 -04:00
: order ( generic -- seq )
"methods" word-prop keys sort-classes ;
: methods ( generic -- assoc )
dup "methods" word-prop swap order [
dup rot at method-def 2array
] map-with ;
2006-08-02 02:49:23 -04:00
TUPLE: check-method class generic ;
: check-method ( class generic -- class generic )
dup generic? [ <check-method> throw ] unless
over class? [ <check-method> throw ] unless ;
2006-08-15 16:29:35 -04:00
: with-methods ( word quot -- )
2006-08-02 02:49:23 -04:00
swap [ "methods" word-prop swap call ] keep ?make-generic ;
inline
2006-08-02 03:49:13 -04:00
: define-method ( method class generic -- )
2006-08-02 02:49:23 -04:00
>r bootstrap-word r> check-method
[ set-at ] with-methods ;
! Definition protocol
M: method-spec where
dup first2 method method-loc [ ] [ second where ] ?if ;
M: method-spec forget
first2 [ delete-at ] with-methods ;
M: method-spec definer drop \ M: \ ; ;
M: method-spec definition first2 method method-def ;
2006-08-02 02:49:23 -04:00
2006-08-16 21:55:53 -04:00
: implementors ( class -- seq )
all-words
[ generic? ] subset
[ "methods" word-prop key? ] subset-with ;
2006-08-02 15:17:13 -04:00
: forget-methods ( class -- )
dup implementors [ 2array forget ] each-with ;
2006-08-02 16:53:26 -04:00
: forget-predicate ( class -- )
"predicate" word-prop [ forget ] each ;
2006-08-02 15:17:13 -04:00
: forget-class ( class -- )
dup forget-methods
dup forget-predicate
dup uncache-class
forget-word ;
2006-08-02 15:17:13 -04:00
M: class forget forget-class ;