factor/library/generic/union.factor

51 lines
1.2 KiB
Factor
Raw Normal View History

2005-02-18 20:37:01 -05:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: generic
2005-02-18 20:37:01 -05:00
USING: errors hashtables kernel lists namespaces parser strings
words vectors ;
2004-07-16 02:26:21 -04:00
! Union metaclass for dispatch on multiple classes.
SYMBOL: union
2004-07-28 19:02:24 -04:00
union [
[ ] swap "members" word-prop [
builtin-supertypes append
] each
] "builtin-supertypes" set-word-prop
2004-07-16 02:26:21 -04:00
union [
( generic vtable definition class -- )
"members" word-prop [ >r 3dup r> add-method ] each 3drop
] "add-method" set-word-prop
2004-07-16 02:26:21 -04:00
union 30 "priority" set-word-prop
2004-07-16 02:26:21 -04:00
union [ 2drop t ] "class<" set-word-prop
2004-12-29 18:01:23 -05:00
: union-predicate ( definition -- list )
[
[
\ dup ,
unswons "predicate" word-prop append,
[ drop t ] ,
union-predicate ,
\ ifte ,
] make-list
] [
[ drop f ]
] ifte* ;
2004-07-16 02:26:21 -04:00
: define-union ( class predicate definition -- )
2004-12-23 18:26:04 -05:00
#! We have to turn the f object into the f word, same for t.
[
[
[
[[ f POSTPONE: f ]]
[[ t POSTPONE: t ]]
2004-12-23 18:26:04 -05:00
] assoc dup
] keep ?
] map
[ union-predicate define-compound ] keep
dupd "members" set-word-prop
2005-01-13 14:41:08 -05:00
union define-class ;