Example - outputting the Tutorial 2 Building BoM to a file

<-Previous | ^UP^ | Next->

Example Brief

Extend the building example developed during the tutorial to optionally export the Bill of Materials to a file

The first step is to determine how to write the output, bearing in mind we need to be able to export the BoM to a file as well as output it to the screen if required. The current implementation provides a bom-formatted slot which it a single formatted string. It would make sense to use this directly. It would also make sense to write a :function to write the output to a file. The :function would be pretty simple

(define-object building (box):input-slots(......(output-filename nil))......:functions(......(write-bom-file! ()(with-open-file (s (the output-filename) :direction :output:if-exists :supersede:if-does-not-exist :create)(format t "Exporting the BOM to ~a~%" (the output-filename))(format s "~a" (the bom-formatted))(format t "Exporting complete~%")))))
In the above code, we introduce a new :input-slot output-filename which defaults to nil. We also add a new :function write-bom-file!. This function connects a stream to the file specified in output-filename and writes (the bom-formatted) to that stream using the format function. Also included are 2 progress messages which are written to *standard-output*

The next task is to update the function which instantiates the building object and either write the BoM to the screen or send it to a file