Search code examples
reflectionrebolrebol3

How should a Rebol-structured data file (which contains no code) be written and read?


If you build up a block structure, convert it to a string with MOLD, and write it to a file like this:

>> write %datafile.dat mold [
    [{Release} 12-Dec-2012]
    [{Conference} [12-Jul-2013 .. 14-Jul-2013]]
]

You can LOAD it back in later. But what about headers? If a file contains code, it is supposed to start with a header like:

rebol [
    title: "Local Area Defringer"
    date: 1-Jun-1957
    file: %defringe.r
    purpose: {
        Stabilize the wide area ignition transcriber
        using a double ganged defringing algorithm.
    }
]

If you are just writing out data and reading it back in, are you expected to have a rebol [] header, and extend it with any properties you want to add? Should you come up with your own myformat [] header concept with your own properties?

Also, given that LOAD does binding, does it make sense to use it for data or is there a different operation?


Solution

  • Rebol data doesn't have to have a header, but is best practice to include one (even if it's just data).

    Some notes:

    • SAVE is your best bet for serializing to file! or port! and has a mechanism for including a header.

    • MOLD and SAVE both have an /ALL refinement that corresponds to LOAD (without /ALL, some data from MOLD and SAVE cannot be reliably recovered, including Object, Logic and None values).

    • LOAD discards the header, though you can load it using the /HEADER refinement.

    Putting this together:

    save/all/header %datafile.dat reduce [next "some" 'data][
        title: "Some Data"
    ]
    
    header: take data: load/header %datafile.dat
    

    To use a header other than Rebol [], you'd need to devise a separate loader/saver.