Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
intro_example:assertions [2025/05/17 12:02] – created rajit | intro_example:assertions [2025/05/17 12:07] (current) – rajit | ||
---|---|---|---|
Line 16: | Line 16: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Passing in parameter 0 to this buffer would yield: | ||
+ | <code act> | ||
+ | buffer< | ||
+ | </ | ||
+ | < | ||
+ | --[ERROR]-> | ||
+ | In expanding buffer< | ||
+ | In expanding ::< | ||
+ | Error on or near line number XXXX. | ||
+ | id: b[0].L | ||
+ | FATAL: Identifer conditionally created, but used when it does not exist | ||
+ | </ | ||
+ | The definition, as written, does not support zero-length buffers. Rather than getting a message at expansion time that may not be as understandable to a user (especially of a pre-defined library), we can add an assertion to provide a more meaningful error message to a user of the templated buffer. | ||
+ | |||
+ | <code act> | ||
+ | template< | ||
+ | defproc buffer(chan? | ||
+ | { | ||
+ | { N > 0 : " | ||
+ | onebuf b[N]; // create one-place buffer elements | ||
+ | (i:N-1: b[i+1].L = b[i].R; | ||
+ | b[0].L = L; // connect external input | ||
+ | b[N-1].R = R; // connect external output | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | With this change, the error now looks like the following: | ||
+ | < | ||
+ | --[ERROR]-> | ||
+ | In expanding buffer< | ||
+ | In expanding ::< | ||
+ | Error on or near line number XXXX. | ||
+ | *** Assertion failed *** | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||