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

polymorphic object/block "VWord func" - ...Re:

 [1/1] from: kgd03011::nifty::ne::jp at: 11-Oct-2000 11:13

Hi Doug, I don't think it's usually a good idea to use any-type! arguments for functions, since that can easily lead to bugs.
>> vword [a "APPLE" b "BANANA" c "CARROT"] 'c print "oops"
oops == "CARROT" Programmers less experienced than you might wonder why the statements get carried out in the "wrong" order". How about this: vword: func [ { Test to see if a certain word is found in a block or object. If so, the value of the word is returned. (Hence, the name vword.) If not, then the alternate value will be used. If the word is not found and no alternate value is supplied, none is returned.} item [block! object!] {The item containing the word you wish to search for.} i-word [word! block!] {The word / attribute you wish to search for. An alternate value may be specified as the second item of a block.} /local ret i-alt ][ if block? i-word [ i-alt: pick i-word 2 i-word: first i-word ] either block? item [ either none? ret: select item :i-word [ i-alt ][ ret ] ][ either none? ret: in item :i-word [ i-alt ][ get in item :i-word ] ] ]
>> vword [a "APPLE" b "BANANA" c "CARROT"] [d "DICED PEACHES"]
>> vword [a "APPLE" b "BANANA" c "CARROT"] [a "DICED PEACHES"]
== "APPLE"
>> vword [a "APPLE" b "BANANA" c "CARROT"] 'e
== none
>> vword [a "APPLE" b "BANANA" c "CARROT"] [e]
== none
>> xo: make object! [a: "ABC" b: "BOY"] >> vword xo [a "alpha"]
== "ABC"
>> vword xo [c "alpha"]
== "alpha"
>> vword xo [c]
== none
>> vword xo 'c
== none This also works:
>> vword [1 "APPLE" 2 "BANANA" 3 "CARROT"] [2]
>> vword [1 "APPLE" 2 "BANANA" 3 "CARROT"] [4 "DICED PEACHES"]