Object serialization: Difference between revisions
Content added Content deleted
m (→{{header|Objective-C}}: modernize) |
(Added zkl) |
||
Line 1,651: | Line 1,651: | ||
close $f |
close $f |
||
$obj printGreetings</lang> |
$obj printGreetings</lang> |
||
=={{header|zkl}}== |
|||
zkl can serialize a "root class" (usually a file but any static (ie parentless) class) to bit bucket (such as File). This is done via reflection. The state of the class(es) are not stored so no image write/read. In the "normal" course of events, this isn't used, programs are compiled on the fly and run. However, there is extensive support to pre-compile and package files into applications or just pre-compile for faster load times (or to create an image that can be compiled into C code (which is done to package the compiler with the VM). When the compiler writes a class or app to a file, the preferred extension is ".zsc", which is what the Import method looks for. But no matter, we have ways ... |
|||
<lang zkl>class [static] ARootClass{ // A top level class, no instances |
|||
class A{ self.println(" constructor"); } // a regular class |
|||
class B(A){ // ditto |
|||
var x; |
|||
fcn init(x=123){ self.x=x } |
|||
fcn toString{ "x is "+x } |
|||
} |
|||
} |
|||
ARootClass.B(456).println(); // create an instance |
|||
// prints: |
|||
Class(A) constructor |
|||
x is 456 |
|||
f:=File("object.dat","wb"); |
|||
Compiler.Asm.writeRootClass(ARootClass,f); // serialize to file |
|||
f.close(); |
|||
f:=File("object.dat","rb"); |
|||
rc:=Compiler.Asm.readRootClass(f); // read and re-create |
|||
// prints (readRootClass calls all constructors by default): |
|||
Class(A) constructor |
|||
f.close(); |
|||
rc.B().println(); // create a new instance of B |
|||
// prints: |
|||
x is 123</lang> |
|||
{{omit from|AWK}} |
{{omit from|AWK}} |