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

using type? with switch

 [1/16] from: greggirwin::starband::net at: 1-Oct-2001 15:44


Is is possible to use the result of type? with switch? I.e. if I want to use switch to dispatch based on different data types, it seems I have to use to-string on the result of type? for use with switch. Is that the case or am I missing something? --Gregg

 [2/16] from: tim:johnsons-web at: 1-Oct-2001 14:53


On Mon, Oct 01, 2001 at 03:44:47PM -0600, Gregg Irwin wrote:
> Is is possible to use the result of type? with switch? I.e. if I want to use > switch to dispatch based on different data types, it seems I have to use > to-string on the result of type? for use with switch. Is that the case or am > I missing something?
; Here is an example of an approach that I use switch/default form type? filename[ "string"[ either (system/words/find filename "ftp://") = filename[ filename: to-url filename ][filename: to-file filename] ] "file"[] "url"[] ; code blocks to be added as needed ][toss["filename in update must be string, url, or file type"]] ; 'toss is user-defined error handler
> --Gregg > > -- > To unsubscribe from this list, please send an email to > [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [3/16] from: greggirwin:starband at: 1-Oct-2001 16:45


Thanks Tim, It appears that you have to convert the result of type? to use it with switch. --Gregg

 [4/16] from: joel:neely:fedex at: 1-Oct-2001 18:12


Hi, Gregg, Gregg Irwin wrote:
> Is is possible to use the result of type? with switch? I.e. if > I want to use switch to dispatch based on different data types, > it seems I have to use to-string on the result of type? for use > with switch. Is that the case or am I missing something? >
Consider the fragment below:
>> selblk: [
[ integer! [print "int"] [ string! [print "str"] [ block! [print "blk"] [ ] == [ integer! [print "int"] string! [print "str"] block! [print "blk"] ]
>> foreach item selblk [print type? :item]
word block word block word block This shows that the "case" elements of the block are WORD! values, not DATATYPE! values (because they haven't been evaluated yet). OTOH, we can do this:
>> foreach item reduce selblk [print type? :item]
datatype block datatype block datatype block to get a collection of DATATYPE!/BLOCK! pairs. This means we'd expect to reduce the type/action block to get the desired behavior. For example
>> vi: 12
== 12
>> switch/default type? vi selblk [print "unknown"]
unknown
>> switch/default type? vi reduce selblk [print "unknown"]
int and
>> vs: "Hello, world!"
== "Hello, world!"
>> switch/default type? vs selblk [print "unknown"]
unknown
>> switch/default type? vs reduce selblk [print "unknown"]
str because the datatypes resulting from TYPE? Vx don't match the words in the non-REDUCEd version, but do match the datatypes in the REDUCEd version. Unfortunately, there's still a gotcha!
>> vb: [1 2 3]
== [1 2 3]
>> switch/default type? vb selblk [print "unknown"]
== string!
>> switch/default type? vb reduce selblk [print "unknown"]
== string! Which looks totally mysterious, until we take into account that SELECT apparently has a different behavior if the target is a datatype: it matches the first value of that datatype, rather than matching the datatype value itself!
>> foo: [1 "HI" #"X" 1.2 http://www.rebol.com [x--y--com] "EEK"]
== [1 "HI" #"X" 1.2 http://www.rebol.com [x--y--com] "EEK"]
>> select foo integer!
== "HI"
>> select foo decimal!
== http://www.rebol.com
>> select foo url!
== [x--y--com] (And, remember, SWITCH is built on top of SELECT, as you can see by entering SOURCE SWITCH in the console.) This means that TYPE? VB evaluates to BLOCK! which matches the first "action block" in the target/action pairs (item 2) which returns the next element (item 3) as the result to be DOne by the SWITCH. You can use a couple of alternative work-arounds to this very non-intuitive (and undocumented?) state of affairs: 1) make the selection target a string, as you mentioned (it would be nice if TO-WORD could take a datatype as an argument which would then match in the non-REDUCEd block, instead of blowing up in one's face!) 2) create your own SWITCH variation that avoids the problem by forcing target/action parity:
>> case: func [[throw] val cases [block!] /default def] [
[ either val: select/skip cases val 2 [do first val] [ [ either default [do def] [none]] [ ]
>> case/default type? vi reduce selblk [print "unknown"]
int
>> case/default type? vs reduce selblk [print "unknown"]
str
>> case/default type? vb reduce selblk [print "unknown"]
blk HTH! -jn- -- This sentence contradicts itself -- no actually it doesn't. -- Doug Hofstadter joel<dot>neely<at>fedex<dot>com

 [5/16] from: greggirwin:starband at: 1-Oct-2001 17:34


Thanks Joel! Wow, it'll take me some time to digest all that (very useful) info. For now I'll just go with the to-string version and live with it. :) Thanks much! --Gregg

 [6/16] from: tim:johnsons-web at: 1-Oct-2001 16:25


On Mon, Oct 01, 2001 at 04:45:25PM -0600, Gregg Irwin wrote:
> Thanks Tim, > > It appears that you have to convert the result of type? to use it with > switch.
Yes. But I heartily suggest that you read Joe Neely's take on this. His answer is very informative.... In fact, I just put his email on this into my "rebinfobase" tim
> --Gregg > > -- > To unsubscribe from this list, please send an email to > [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [7/16] from: deadzaphod:flyingparty at: 1-Oct-2001 17:18


Great explanation! (although I've got one tiny addition below ;-] )
> This means that TYPE? VB evaluates to BLOCK! which matches > the first "action block" in the target/action pairs (item 2)
<<quoted lines omitted: 8>>
> 2) create your own SWITCH variation that avoids the problem > by forcing target/action parity:
3) use reduce to solve the WORD! / DATATYPE! problem and always have BLOCK! as the first option in the switch to avoid the "feature" of matching the first action block. Cal Dixon

 [8/16] from: joel:neely:fedex at: 1-Oct-2001 16:06


Hi, Cal, Cal Dixon wrote:
> ... I've got one tiny addition below ;-] ... > > 3) use reduce to solve the WORD! / DATATYPE! problem and always > have BLOCK! as the first option in the switch to avoid the > "feature" of matching the first action block. >
Thanks for the additional option! Permit me to add one tiny additional addition ;-} This is the kind of thing that can drive a maintenance programmer (anybody but the original author, or the original author after only one significant distraction) completely around the bend. Suppose that a subsequent edit involves adding an option to the list of case/action pairs. The person performing that edit might look at code like the following: ;; ... cases: reduce [ block! [ ... block action ... ] integer! [ ... integer action ... ] string! [ ... string action ... ] email! [ ... email action ... ] ] ;; ... switch/default type? somevar cases [ ... default action ... ] ;; ... and observe (correctly!) that all of the case selectors are distinct and therefore conclude (incorrectly!) that the order doesn't matter. Needing to add an additional case, (s)he might then decide to stick the new case at the beginning of the case/action pairs... cases: reduce [ url! [ ... url action ... ] block! [ ... block action ... ] integer! [ ... integer action ... ] string! [ ... string action ... ] email! [ ... email action ... ] ] and then be terribly confused when the code goes horribly awry! My suggestion would be either to avoid such subtly fragile constructions or to insert comments warning subsequent readers and maintainers that the BLOCK!/action pair *must* remain as the first choice. Having seen an entire weekend lost in the search for a missing comma... -jn- -- Of course, implementing a compiler for modern C++ must be the second worst C++ programmer's nightmare ;-) [the first is having to work in OO Cobol]. -- Graziano Lo Russo joel[dot[FIX[PUNCTUATION[neely[at[fedex[dot[com

 [9/16] from: jeff:rebol at: 1-Oct-2001 19:40


Howdy, Gregg:
> Is is possible to use the result of type? with switch? I.e. if I want to use > switch to dispatch based on different data types, it seems I have to use > to-string on the result of type? for use with switch. Is that the case or am > I missing something?
Here's an example: probe switch type?/word var [ string! [lowercase var] email! [uppercase var] block! [random var] ... ]

 [10/16] from: chrismorency:videotron:ca at: 1-Oct-2001 23:37


Hi Greg... I had this problem too and I converted to to-string... ie : switch/default to-string type? var [ "string" [ ... ] "integer" [ ... ] ] Best, Chris

 [11/16] from: greggirwin:starband at: 1-Oct-2001 21:48


Howdy Jeff, << probe switch type?/word var [ string! [lowercase var] email! [uppercase var] block! [random var] ... ]
>>
DOH! DOH! DOH! :) OK, so this is a great lesson for me. Now that I've spent a *little* time with REBOL, I don't always think to look for HELP which, obviously, I should. Many, many thanks, to you and to everybody else who jumped in on this thread. Great stuff gang. --Gregg

 [12/16] from: chrismorency:videotron:ca at: 1-Oct-2001 23:48


Hi Jeff,
> Here's an example: > probe switch type?/word var [
<<quoted lines omitted: 3>>
> ... > ]
Can't believe it was so simple ! We really need to check out the source of those native words sometime heh ? Best, Chris

 [13/16] from: tim:johnsons-web at: 1-Oct-2001 21:08


On Mon, Oct 01, 2001 at 11:48:30PM -0400, Christian Morency wrote:
> Hi Jeff, > > Here's an example:
<<quoted lines omitted: 7>>
> Can't believe it was so simple ! We really need to check out the source of > those native words sometime heh ?
When I work in rebol - more and more these days - as a matter of thought - I always keep at least one session of rebol going for: 1)'help 2)'source 3)Testing single lines of rebol code => I use 'probe and 'mold a lot not 'trace as much as I get a little overwhelmed by its verbosity. tj
> Best, > Chris > > -- > To unsubscribe from this list, please send an email to > [rebol-request--rebol--com] with "unsubscribe" in the > subject, without the quotes.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.johnsons-web.com

 [14/16] from: chrismorency:videotron:ca at: 2-Oct-2001 1:00


Hi Tim,
> When I work in rebol - more and more these days - as a matter of thought > - I always keep at least one session of rebol going for: > 1)'help > 2)'source > 3)Testing single lines of rebol code > => I use 'probe and 'mold a lot > not 'trace as much as I get a little overwhelmed > by its verbosity.
Well, I have to admit that I must be the only one crazy enough to write 90% of it's rebol code on a early WindowsCE device with a keyboard. I switch between a text editor and rebol/core... probing can be long on a 85 mhtz machine ! And yes I have a PC. The other day I was in the metro, I used my pocketpc to verify if we could do recursivity with function and with methods (function in objects) ! All of that on a keyboardless device ;) And I'm working on my first rebol library on the same WindowsCE device ! So unfortunately, I use help, probe and source has much as I can withstand the crawl of the screen... and I have to get more familiar with mold... Best, Chris

 [15/16] from: joel:neely:fedex at: 2-Oct-2001 10:59


Jeff Kreis wrote:
> > Is is possible to use the result of type? with switch? > > I.e. if I want to use switch to dispatch based on different
<<quoted lines omitted: 8>>
> ... > ]
Hmmm... Fascinating, in view of
>> vi: 123
== 123
>> type? vi
== integer!
>> to-word type? vi
** Script Error: Expected one of: word! - not: datatype! ** Where: to-word ** Near: to word! :value which precludes the obvious switch to-word type? var [ string! [...] integer! [...] decimal! [...] ... ] -jn- -- This sentence contradicts itself -- no actually it doesn't. -- Doug Hofstadter joel<dot>neely<at>fedex<dot>com

 [16/16] from: rotenca:telvia:it at: 3-Oct-2001 15:14


> which precludes the obvious > switch to-word type? var [
<<quoted lines omitted: 3>>
> ... > ]
You can always: switch to-word mold type? var
> -jn-
--- Ciao Romano

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted