factor/library/generic/union.factor

39 lines
1.0 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
USING: errors hashtables kernel lists namespaces parser
sequences 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 [
2005-08-14 17:33:45 -04:00
"members" word-prop [ builtin-supertypes ] map concat
] "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-predicate ( members -- list )
[
[
\ dup ,
2005-04-16 00:23:27 -04:00
unswons "predicate" word-prop %
[ drop t ] ,
union-predicate ,
\ ifte ,
] make-list
] [
[ drop f ]
] ifte* ;
2004-07-16 02:26:21 -04:00
: define-union ( class predicate members -- )
2004-12-23 18:26:04 -05:00
#! We have to turn the f object into the f word, same for t.
3dup nip "members" set-word-prop
pick union define-class
union-predicate define-predicate ;
PREDICATE: word union metaclass union = ;