[REBOL] Re: How to check validity of types ?
From: brett:codeconscious at: 15-Jan-2001 21:51
Hi Christophe,
> So I define the valid types into a block 'b:
>
> >> b: [integer! string!]
> == [integer! string!]
> >> first b
> == integer!
You didn't get what you thought you got here. What you actually got was the
word "integer!" not the datatype integer!
>> type? first b
== word!
Why? Blocks contain unevaluated data.
> >> value: make integer! 1
> == 1
> >> type? value
> == integer!
>
> And I check the requirement:
>
> >> (type? value) = first b
> == false
>
>...
> What did I wrong ? Is there another way to get what I want ? Any idea,
> suggestion ?
What you need is a reduce on the line. Try this instead
>> b: reduce [integer! string!]
== [integer! string!]
>> type? first b
== datatype!
>> first b
== integer!
>> value: make integer! 1
== 1
>> (type? value) = first b
== true
Also note that functions can check the datatypes of their parameters.
>> my-integer-function: func [an-int [integer!] ] [ an-int * 5]
>> my-integer-function "test"
** Script Error: my-integer-function expected an-int argument of type:
integer.
** Where: my-integer-function "test"
And remember parse in block mode can check datatypes as well. Dialects don't
have to be long!
>> parse [3] [integer!]
== true
>> parse ["test"] [integer!]
== false
Brett.