Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Run Time Object Type Identification

From: dankelg8:cs:man:ac at: 3-Oct-2001 15:21

Here's a function that checks if an object spec implements an interface... It's very simple but it can be useful. This replaces make object! I've just called the function 'implements, but Gregg's ideas are probably better. Gisle implement: func [ "Returns a new object after checking that it conforms to an interface" [catch] interface [object!] "All fields in this object must be implemented" impl [block!] "A block that would normally be passed to make object!" /local result args-rule args args' f1 f2 ][ result: make object! impl args-rule: [copy args [[to /local] | [to end]] to end] foreach word first interface [ if not find first result word [ throw make error! join "Object must implement " word ] if function? f1: get in interface word [ if not function? f2: get in result word [ throw make error! reform [ word "is of type" type? :f2 newline "Should be:" type? :f1 ] ] parse filter copy third :f1 func [x] [not string? x] args-rule args': args parse filter copy third :f2 func [x] [not string? x] args-rule if not-equal? args' args [ throw make error! reform [ "Object implements function" word "with arguments" mold args newline "Should be:" mold args' ] ] if not find third :f2 string! [ args: copy/deep third :f1 foreach item args [ if all [block? item datatype? item/1][ map item func [x] [to-word mold x] ] ] set in result word func args second :f2 ] ] ] result ] map: func [series [series!] ff [any-function!]][ forall series [change/only series ff first series] head series ] filter: func [series [series!] pred [any-function!]] [ while [not tail? series][ either pred first series [series: next series] [remove series] ] head series ] On Tue, 2 Oct 2001, Gregg Irwin wrote: