factor/core/generic/generic.factor

108 lines
2.9 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2006, 2007 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: words kernel sequences namespaces assocs hashtables
definitions kernel.private classes classes.private
quotations arrays vocabs ;
IN: generic
2008-01-02 19:36:36 -05:00
PREDICATE: word generic "combination" word-prop >boolean ;
2007-09-20 18:09:08 -04:00
M: generic definer drop f f ;
M: generic definition drop f ;
GENERIC: perform-combination ( word combination -- quot )
2007-10-09 17:41:35 -04:00
M: object perform-combination
#! We delay the invalid method combination error for a
#! reason. If we call forget-vocab on a vocabulary which
#! defines a method combination, a generic using this
#! method combination, and a method on the generic, and the
#! method combination is forgotten first, then forgetting
#! the method will throw an error. We don't want that.
nip [ "Invalid method combination" throw ] curry [ ] like ;
2007-10-09 17:41:35 -04:00
2007-09-20 18:09:08 -04:00
: make-generic ( word -- )
2008-01-02 19:36:36 -05:00
dup dup "combination" word-prop perform-combination define ;
2007-09-20 18:09:08 -04:00
: init-methods ( word -- )
dup "methods" word-prop
H{ } assoc-like
"methods" set-word-prop ;
: define-generic ( word combination -- )
dupd "combination" set-word-prop
dup init-methods make-generic ;
2007-09-20 18:09:08 -04:00
TUPLE: method loc def ;
: <method> ( def -- method )
{ set-method-def } \ method construct ;
: method ( class generic -- method/f )
"methods" word-prop at ;
PREDICATE: pair method-spec
first2 generic? swap class? and ;
: order ( generic -- seq )
"methods" word-prop keys sort-classes ;
: sort-methods ( assoc -- newassoc )
[ keys sort-classes ] keep
2008-02-03 15:19:07 -05:00
[ dupd at method-def ] curry { } map>assoc ;
2007-09-20 18:09:08 -04:00
: methods ( word -- assoc )
"methods" word-prop sort-methods ;
TUPLE: check-method class generic ;
: check-method ( class generic -- class generic )
over class? over generic? and [
\ check-method construct-boa throw
] unless ;
: with-methods ( word quot -- )
swap [ "methods" word-prop swap call ] keep make-generic ;
2007-09-20 18:09:08 -04:00
inline
: define-method ( method class generic -- )
2008-02-03 15:19:07 -05:00
>r >r <method> r> bootstrap-word r> check-method
2007-09-20 18:09:08 -04:00
[ set-at ] with-methods ;
! Definition protocol
M: method-spec where
2008-02-03 15:19:07 -05:00
dup first2 method [ method-loc ] [ second where ] ?if ;
2007-09-20 18:09:08 -04:00
M: method-spec set-where first2 method set-method-loc ;
M: method-spec definer drop \ M: \ ; ;
2008-02-03 15:19:07 -05:00
M: method-spec definition
first2 method dup [ method-def ] when ;
2007-09-20 18:09:08 -04:00
2008-01-11 03:32:25 -05:00
: forget-method ( class generic -- )
check-method [ delete-at ] with-methods ;
M: method-spec forget* first2 forget-method ;
2007-09-20 18:09:08 -04:00
: implementors* ( classes -- words )
all-words [
"methods" word-prop keys
swap [ key? ] curry contains?
2008-01-09 17:36:30 -05:00
] with subset ;
2007-09-20 18:09:08 -04:00
: implementors ( class -- seq )
dup associate implementors* ;
: forget-methods ( class -- )
2007-10-09 17:35:09 -04:00
[ implementors ] keep [ swap 2array ] curry map forget-all ;
2007-09-20 18:09:08 -04:00
M: class forget* ( class -- )
2007-09-20 18:09:08 -04:00
dup forget-methods
dup uncache-class
forget-word ;
2008-01-31 01:49:18 -05:00
M: assoc update-methods ( assoc -- )
implementors* [ make-generic ] each ;