Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
intro_example:assertions [2025/05/17 12:02] – created rajitintro_example:assertions [2025/05/17 12:07] (current) rajit
Line 16: Line 16:
 }   }  
 </code> </code>
 +
 +Passing in parameter 0 to this buffer would yield:
 +<code act>
 +buffer<0> x;
 +</code>
 +<code>
 +--[ERROR]->  
 +In expanding buffer<0> (filename.act:XXXX)
 +In expanding ::<Global>
 +Error on or near line number XXXX.
 + id: b[0].L
 +FATAL: Identifer conditionally created, but used when it does not exist
 +</code>
 +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<pint N>
 +defproc buffer(chan?(int) L; chan!(int) R)
 +{
 +    { N > 0 : "buffer size has to be at least 1" };
 +    onebuf b[N];   // create one-place buffer elements
 +    (i:N-1: b[i+1].L = b[i].R; // connect internal channels
 +    b[0].L = L; // connect external input
 +    b[N-1].R = R; // connect external output
 +}  
 +</code>
 +
 +With this change, the error now looks like the following:
 +<code>
 +--[ERROR]-> 
 +In expanding buffer<0> (filename.act:XXXX)
 +In expanding ::<Global>
 +Error on or near line number XXXX.
 +*** Assertion failed ***
 + assertion: N>0
 +   message: buffer size has to be at least 1
 +</code>
 +