to/make datatype!
[1/7] from: joel::neely::fedex::com at: 16-Oct-2002 13:30
Hi, all,
Some conversions to/from STRING! work nicely (and in the obvious,
intuitive
fashion),
>> join "" 1 == "1"
>> to-string 1 == "1"
>> mold 1 == "1"
and
>> type? to-integer "1" == integer!
>> type? to-decimal "1" == decimal!
>> type? to integer! "1" == integer!
>> type? to decimal! "1" == decimal!
>> type? make integer! "1" == integer!
>> type? make decimal! "1" == decimal!
We can convert a DATATYPE! value to a STRING! value in different
ways, with slightly different results,
>> int-type: type? 1 == integer!
>> join "" int-type == "integer"
>> to-string int-type == "integer"
>> mold int-type == "integer!"
(note the presence/absence of the trailing bang), but the
corresponding transformations *don't* work then other way,
with no TO-xxx shortcut:
>> to-datatype int-type-name
** Script Error: to-datatype has no value
** Where: halt-view
** Near: to-datatype int-type-name
and no support from TO or MAKE either:
>> to datatype! int-type-name
** Script Error: Cannot use to on datatype! value
** Where: halt-view
** Near: to datatype! int-type-name
>> make datatype! int-type-name
** Script Error: Cannot use make on datatype! value
** Where: halt-view
** Near: make datatype! int-type-name
So far the most simplest way I've come up with to create a DATATYPE!
value from a STRING! value is
to-datatype: func [s [string!]] [
first reduce load rejoin [
"[" s either #"!" = last s ["]"] ["!]"]
]
]
>> type? to-datatype "string" == datatype!
>> make to-datatype "integer" "12" == 12
There's *got* to be a better way! Any suggestions?
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
[2/7] from: gscottjones:mchsi at: 16-Oct-2002 14:26
From: "Joel Neely"
...
> So far the most simplest way I've come up with to create a DATATYPE!
> value from a STRING! value is
<<quoted lines omitted: 6>>
> >> make to-datatype "integer" "12" == 12
> There's *got* to be a better way! Any suggestions?
Hi, Joel,
It's kind of like the difference between shaving with a double or triple
razor blade, but here is at least one other way, which is just a short cut
on your version:
to-datatype: func [s [string!]] [
do join s either #"!" = last s [""]["!"]
]
;where
type? to-datatype "string" ;== datatype!
to to-datatype "string" "12" ;== "12"
--Scott Jones
[3/7] from: greggirwin:mindspring at: 16-Oct-2002 13:59
Hi Joel,
Tough one!
I don't know that this is much better, but for versions that support the
serialized format of data, you can do this:
to-datatype: func [s [string!]] [
load rejoin [
"#[datatype! " s either #"!" = last s ["]"] ["!]"]
]
]
--Gregg
[4/7] from: joel:neely:fedex at: 16-Oct-2002 14:52
Thanks, Scott!
G. Scott Jones
wrote:
> From: "Joel Neely"
> ...
<<quoted lines omitted: 9>>
> >
> > There's *got* to be a better way! Any suggestions?
...
> to-datatype: func [s [string!]] [
> do join s either #"!" = last s [""]["!"]
> ]
>
> ;where
> type? to-datatype "string" ;== datatype!
> to to-datatype "string" "12" ;== "12"
>
That's certainly an improvement, IMHO! Anybody know why it has to be
so hard?
-jn-
--
----------------------------------------------------------------------
Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446
[5/7] from: g:santilli:tiscalinet:it at: 17-Oct-2002 0:58
Hi Joel,
On Wednesday, October 16, 2002, 8:30:37 PM, you wrote:
JN> So far the most simplest way I've come up with to create a DATATYPE!
JN> value from a STRING! value is
JN> to-datatype: func [s [string!]] [
JN> first reduce load rejoin [
JN> "[" s either #"!" = last s ["]"] ["!]"]
JN> ]
JN> ]
I cannot think of anything better than:
to-datatype: func [s [string!]] [
get to-word either #"!" = last s [s] [join s "!"]
]
or if you don't like depending on the fact that there are some
words referring to datatypes:
to-datatype: func [s [string!]] [
load rejoin ["#[datatype! " s either #"!" = last s ["]"] ["!]"]]
]
(The latter doesn't work on old versions.)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[6/7] from: g:santilli:tiscalinet:it at: 17-Oct-2002 1:03
Hi Joel,
On Wednesday, October 16, 2002, 9:52:03 PM, you wrote:
JN> That's certainly an improvement, IMHO! Anybody know why it has to be
JN> so hard?
Just out of curiosity, what did you need it for?
Anyway, maybe it's so hard because MAKE DATATYPE! ... is left as a
way to create user datatypes in the future. :-)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[7/7] from: al:bri:xtra at: 18-Oct-2002 16:03
Joel Neely wrote:
> So far the most simplest way I've come up with to create a DATATYPE! value
from a STRING! value is
> to-datatype: func [s [string!]] [
> first reduce load rejoin [
<<quoted lines omitted: 4>>
> >> make to-datatype "integer" "12" == 12
> There's *got* to be a better way! Any suggestions?
I missed seeing this earlier. Here's my solution:
Type: 'string! ; with a lit-word!
Data: 123
to do Type Data
Type: "string!" ; and with a string!
Data: 123
to do Type Data
I hope that helps!
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted