Marshal.to_channel chan v flags
writes the representation of v
on channel chan
. The flags
argument is a possibly empty list of flags that governs the marshaling behavior with respect to sharing, functional values, and compatibility between 32- and 64-bit platforms.
If flags
does not contain Marshal.No_sharing
, circularities and sharing inside the value v
are detected and preserved in the sequence of bytes produced. In particular, this guarantees that marshaling always terminates. Sharing between values marshaled by successive calls to Marshal.to_channel
is neither detected nor preserved, though. If flags
contains Marshal.No_sharing
, sharing is ignored. This results in faster marshaling if v
contains no shared substructures, but may cause slower marshaling and larger byte representations if v
actually contains sharing, or even non-termination if v
contains cycles.
If flags
does not contain Marshal.Closures
, marshaling fails when it encounters a functional value inside v
: only 'pure' data structures, containing neither functions nor objects, can safely be transmitted between different programs. If flags
contains Marshal.Closures
, functional values will be marshaled as a the position in the code of the program together with the values corresponding to the free variables captured in the closure. In this case, the output of marshaling can only be read back in processes that run exactly the same program, with exactly the same compiled code. (This is checked at un-marshaling time, using an MD5 digest of the code transmitted along with the code position.)
The exact definition of which free variables are captured in a closure is not specified and can vary between bytecode and native code (and according to optimization flags). In particular, a function value accessing a global reference may or may not include the reference in its closure. If it does, unmarshaling the corresponding closure will create a new reference, different from the global one.
If flags
contains Marshal.Compat_32
, marshaling fails when it encounters an integer value outside the range -2
30
, 2
30
-1
of integers that are representable on a 32-bit platform. This ensures that marshaled data generated on a 64-bit platform can be safely read back on a 32-bit platform. If flags
does not contain Marshal.Compat_32
, integer values outside the range -2
30
, 2
30
-1
are marshaled, and can be read back on a 64-bit platform, but will cause an error at un-marshaling time when read back on a 32-bit platform. The Mashal.Compat_32
flag only matters when marshaling is performed on a 64-bit platform; it has no effect if marshaling is performed on a 32-bit platform.