[datatypes//conversion] CAST / COERCE / CHANGE-TYPE
[1/1] from: greggirwin::mindspring::com at: 10-Aug-2004 11:21
Hi All,
Request for suggestions and comments.
cast: coerce: change-type: func [
"Coerce a value referenced by a word; i.e. in-place coercion"
'word 'type /local val
] [
val: get word
set word either function? :type [do :type val] [
to either word? type [get type] [type] val
]
]
>> my-value: 100
== 100
>> coerce my-value decimal!
== 100.0
>> coerce my-value :to-string
== "100.0"
>> coerce my-value <x>
== <100.0>
Any other name suggestions? CAST, seemed very programmerish to me at
first, but the meaning "to make from a mold" is good, and I actually
like it best at this point. It's nice and short.
Examples:
my-value: to-string my-value
my-value: to string! my-value
my-value: to "" my-value
change-type my-value :to-string
change-type my-value string!
change-type my-value ""
coerce my-value :to-string
coerce my-value string!
coerce my-value ""
cast my-value :to-string
cast my-value string!
cast my-value ""
CHANGE-TYPE seems the most explanatory, but it's also the longest. You
actually lose ground on short var names with it (i.e. more typing) and
I don't really like the way it reads.
The two goals I see are: 1) don't repeat the word, and 2) make it read
naturally so it's clear what you're doing.
Thanks for any comments!
-- Gregg