REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 27-Feb-2013 Edit History |
Converts a value to a REBOL-readable string.
Arguments:
value [any-type!] - The value to mold
Refinements:
/only - For a block value, mold only its contents, no outer []
/all - Mold in serialized format
/flat - No indentation
See also:
The mold function converts values to a source-code formatted string (REBOL-readable).
print mold 10:30
10:30
print mold %image.jpg
%image.jpg
print mold [1 2 3]
[1 2 3]
The primary importance of mold is to produce strings that can be reloaded with load.
str: mold [1 + 2]
probe load str
[1 + 2]
The mold function is the cousin of form which produces a human-readable string (used by the print function.) For example a block will be shown with brackets [ ] and strings will be " " quoted or use braces { } (if it is long or contains special characters).
Also, remold first uses reduce then mold.
In some cases it is useful to not mold the outermost brackets of blocks. This is done with the /only refinement:
print mold/only [1 2 3]
1 2 3
This is commonly true for blocks of values that are saved to files:
write %example.r mold/only [1 2 3]
See the save function.
For some values mold produces an approximate string value, not a perfect representation. If you attempt to load such a string, its value may be different.
For example:
mold 'true
"true"
mold true
"true"
The first is the word true the second is the logic! value true -- they are different but represented by the same word. If you load the resulting string, you will only obtain the word true not the logic value:
type? load mold true
word!
The /all option provides a more accurate transformation from values to strings and back (using load.)
mold/all 'true
"true"
mold/all true
"#[true]"
Using load, you can see the difference:
type? load mold/all 'true
word!
type? load mold/all true
logic!
Another difference occurs with strings that are indexed from their head positions. Sometimes this is desired, sometimes not. It can be seen here:
mold next "ABC"
"BC"
mold/all next "ABC"
{#[string! "ABC" 2]}
The following datatypes are affected: unset!, none!, logic!, bitset!, image!, map!, datatype!, typeset!, native!, action!, op!, closure!, function!, object!, module!, error!, task!, port!, gob!, event!, handle!.
The decimal! datatype is implemented as IEEE 754 binary floating point type. When molding decimal! values, mold/all will need to use the maximal precision 17 digits to allow for accurate transformation of Rebol decimals to string and back, as opposed to just mold, which uses a default precision 15 decimal digits.
The /flat refinement is useful for minimizing the size of source strings. It properly removes leading indentation (from code lines, but not multi-line strings.) The /flat option is often used for data exchanged between systems or stored in files.
Here is code often used for saving a script in minimal format (in R3):
write %output.r mold/only/flat code
For code larger than about 1K, you can also compress it:
write %output.rc compress mold/only/flat code
Such a file can be reloaded with:
load/all decompress read %output.rc
Note that if using R2, these lines must be modified to indicate binary format.
It should be noted that mold function is used for computing the relative complexity of code using the Load Mold Sizing method.
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |