Another example of using structs to represent DVDs
(and practice using the design recipe).
The dvd struct will be used, along with book, in the following examples.
code:
The initial version to follow along and type in yourself
(starting around line 80, the data def'n for dvd);
and, the completed code
In the template for a union-type, if we are in a branch where we know we have a struct,
let's go ahead and pull out the fields of that struct right here in that cond-branch.
code:
The initial version to follow along and type in yourself
(starting around line 100, the data def'n for lib-item);
and, the completed code
But don't nest other templates;
call a helper-function rather than process a union-type inside a struct-type-handling-function.
video (23m53s)
[0:00,3:30) has the main point: call helpers, rather than nest templates (beyond union-of-structs mentioned above)
[3:30,6:44) gives a data-definition for collectors-edition.
Example: struct-of-unions (collectors-edition); its template is merely the struct template (and you'll call helper functions).
[6:44,12:10) (skimmable) practice writing a functions for collectors-editions, ripoff.
Including calling helper-functions.
[12:10,14:20) (skimmable) illustrates debugging an error in the function (part 1):
when you construct objects with the fields swapped — hard to track down!
We pointing out that the pink-highlighted code is where the error was triggered,
and black-highlighted code isn't any error — it's code that was never run (a code-coverage indicator).
[14:20,17:40) (skimmable) write the function verify-title; point out that
when we call a helper function on one of our fields,
that helper function could merely be the getter.
[17:40,23:53) (skimmable) More involved debugging for a real-world issue:
(because an earlier function had an error, but that earlier function's test-cases had been wrong!)
Handy keyboard shortcut illustrated at 22:35 (ESC-Ctrl-t to transpose two expressions).
You can see how to have Racket enforce contracts for constructor-calls.