tags:

views:

52

answers:

1

Why do I have to cast typeof to string with switch to make it work ?

This doesn't work:

  typeof: type? get 'optional
  switch typeof [
    word! [
      print "word"
    ] 
    string! [
      print "string"
    ]           
  ]

This works:

  typeof: type? get 'optional
  switch to-string typeof [
    "word" [
      print "word"
    ] 
    "string" [
      print "string"
    ]           
  ]
+1  A: 
switch type?/word :optional [
    word! [ print "word" ]
    string! [ print "string" ]
]

OR

switch type? :optional reduce [
    word! [ print "word" ]
    string! [ print "string" ] 
]

The reason is that the REBOL doesn't reduce ("evaluate") the cases in the switch statement. Without the /word refinement, the type? function returns a datatype!, but the switch statement tries to match this against a word!, and it fails.

I realize this might be confusing, so your best bet is either to convert the type to a string (as you did), or use one of the two idioms I've suggested. I prefer the first one, using type?/word.

Gregory Higley
Thanks a lot, you're a Rebol Guru for sure : I didn't know /word did exist :)
Rebol Tutorial
Thanks! I didn't know about `/word` either, until a helpful commenter pointed it out to me on my blog. :)
Gregory Higley