[REBOL] Re: R: Re: R: Re: switch and datatype
From: max:ordigraphe at: 17-Jul-2001 11:41
Hi,
I am not sure I catched this thread correctly (I havent' read the prior
posts, sorry) but I built an object loader/saver/translator three days
ago and it does really nice tricks.
I convert the object into a tag-list of value and data pairs (the values
being contained in blocks, so as to make the switch word safe to use).
But the real kicker is that I only save out string, integer and block
data. Any other datatype is ignored.
I realized that "switch" does not work on datatypes! values so, I simply
did
to-string type? for the switch function to work... as in:
switch to-string type? :switch-value [
"integer" [print "integer selected"]
"string" [print "string selected"]
"block" [print "block selected"]
]
note that the ":" in ":switch-value" is ESSENTIAL! Otherwise, any
function! values are EVALUATED BEFORE returning.
My code also detects if the values are empty or null (depending on
datatype this will be: "" [] or 0) and only adds them to the tag pair IF
they aren't empty. cause when you'll create the empty object... that's
the value they'll get by default :-)... what's the point in using up
memory for nothing !?
then If I want to use any value of the tag-pair, I just do:
value: switch tag-name
where an example of the tag list's data (as discussed above) looks like:
[
"name" ["john doe"]
"age" [31]
"skills" [["carpenter" "programmer" "writter"]]
]
If anyone is interested in the actual code, (I do not have in with me
:-( I will gladly give-it out. You could add and remove datatypes at
will, to custom tailor it to your needs. I also plan on adding some
refinements like /all to force it to supply all tags even if empty (but
still of appropriate datatype).
The neat thing is that when storing the object's data using the tag
system above, you can COMPLETELY change the data structure, remove and
even add new fields to an object and NOTHING WILL crash. So improving
one's data and increasing the data in your app isnt too dangerous... as
the objects grow, the new fields just get empty values (and if you
ever -remove- data in an object, it STILL stays safe ;-). It will only
set the tags it recognises in an object and get the tags which actually
have data. transfering data between irrelevent objects is also easy,
since you just transfer matching data from one object to another, and
then (again), only the fields which actually have data (saves space AND
increases speed).
this system makes it REALLY easy to save and load objects to-from disk.
Maybe everything I say is common-knowledge, maybe its total alien to
some of you... I don't know yet... as I'm not yet too sure of the level
of the people on the list (I know some of you are far More advanced than
me though... writing proper dialects and stuff like that! (time...
there's never enough of it)
I just like helping newbies, And its hard to know how much of these are
on the list... which is why some of my posts go a little beyond the
exact thread I reply to.
CIAO!
-Max