How to check validity of types ?
[1/4] from: coussement::c::itc::mil::be at: 15-Jan-2001 10:44
Hi REBOLs,
I would like to check if a provided value meets pre-defined type
requirements...
So I define the valid types into a block 'b:
>> b: [integer! string!]
== [integer! string!]
>> first b
== integer!
>> value: make integer! 1
== 1
>> type? value
== integer!
And I check the requirement:
>> (type? value) = first b
== false
Of course the assumed answer was 'right...
What did I wrong ? Is there another way to get what I want ? Any idea,
suggestion ?
thx a lot for answering
CU, chr==
[2/4] 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
<<quoted lines omitted: 6>>
> 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.
[3/4] from: rebol:techscribe at: 15-Jan-2001 11:05
Hi Christian,
the reason you get this behavior is that the words in the block,
[integer! string!], are not bound to the REBOL global context. Simply
say
b: reduce [integer! string!]
and that will do the trick. When you reduce the block REBOL binds the
words to the global context and now integer! = first b == true, and
string! = second b == true as well.
Hope this helps,
Elan
CRS - Psy Sel/SPO, COUSSEMENT Christophe, CPN
wrote:
[4/4] from: coussement:c:itc:mil:be at: 16-Jan-2001 9:10
Thanks Elan.
So stupid of me I didn't think about that ;-(
chr==
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted