Hi,

A message from Elan got me foaming at the mouth about datatypes (it's not
Elan's fault, so don't blame him!), so if anyone out there is interested ...
(and sorry for shamelessly plugging my scripts - if people would just send
me a little feedback I wouldn't have to do this ;-)

If you haven't used HUH (available on rebol.org) yet, here's how you can
get a list of all the datatypes bound to words:

>> huh * datatype?

@@ datatype!
action!        any-block!     any-function!  any-string!    any-type!
any-word!      binary!        bitset!        block!         char!
datatype!      date!          decimal!       email!         error!
file!          function!      get-word!      hash!          integer!
issue!         list!          lit-path!      lit-word!      logic!
money!         native!        none!          number!        object!
op!            paren!         path!          port!          refinement!
series!        set-path!      set-word!      string!        symbol!
tag!           time!          tuple!         unset!         url!
word!

Notice that in REBOL off-the-site, the datatypes ACTION! and UNSET! are
not assigned to words.

>> type? unset!
** Script Error: unset! has no value.      ; fresh instance of REBOL
** Where: type? unset!                     ; with no scripts run
>> type? action!
** Script Error: action! has no value.
** Where: type? action!

This was the very first "bug" I pointed out to feedback (the first time was
last July), and probably the easiest one to correct:

>> unset!: (type?)
== unset!
>> action!: type? :action?
== action!
>> type? unset!
== datatype!
>> type? action!
== datatype!

but it's still unfixed! Bo told me it was probably because there's no use
for it. Yet there is a use for defining the word unset!. One place I use
it is in FORMAT, from format.r (also on rebol.org).

>> format pi
== "3.14"
>> format pi 4
== "3.1416"
>> format pi 6
== "3.141593"
>> format pi "6"
** Script Error: format expected decimal argument of type: integer block unset.
** Where: format pi "6"

Notice the specification of FORMAT:

format: func [
    "CONVERTS Rebol data into formatted strings."
    item {value or block of values to format - block values are reduced first}
    decimal [integer! block! unset!]
    "optional decimal places to leave (2 by default)"
    width [integer! block! unset!]
    "optional width of formatted string"
    /full "use extra precision in forming decimal values"
    /local digits non-zero n fraction exponent neg
][ .... ]

Normally you can specify an optional argument by using any-type!, but then
your function will accept anything at all. To set up an optional argument
that will accept only certain types of data, you have to have the word
'unset! set to the datatype unset! .

Notice that there's a danger in using optional arguments like this. If you
use FORMAT within a script, and don't want to provide all three arguments,
it had better be at the end of a block or paren, or FORMAT will try to
use something else as an argument that you didn't intend!

Have fun!

Eric

Reply via email to