Assertions: Difference between revisions
Content added Content deleted
(D entry rewritten and completed) |
(added Nemerle) |
||
Line 550: | Line 550: | ||
Assertions can be ignored in the compiler by using the <code>-a</code> switch. |
Assertions can be ignored in the compiler by using the <code>-a</code> switch. |
||
=={{header|Nemerle}}== |
|||
A basic assertion uses the <tt>assert</tt> keyword: |
|||
<lang Nemerle>assert (foo == 42, $"foo == $foo, not 42.")</lang> |
|||
Assertion violations throw an <tt>AssertionException</tt> with the line number where the assertion failed and the message provided as the second parameter to assert. |
|||
Nemerle also provides macros in the <tt>Nemerle.Assertions</tt> namespace to support preconditions, postconditions and class invariants: |
|||
<lang Nemerle>using Nemerle.Assertions; |
|||
class SampleClass |
|||
{ |
|||
public SomeMethod (input : list[int]) : int |
|||
requires input.Length > 0 // requires keyword indicates precondition, |
|||
// there can be more than one condition per method |
|||
{ ... } |
|||
public AnotherMethod (input : string) : list[char] |
|||
ensures value.Length > 0 // ensures keyword indicates postcondition |
|||
{ ... } // value is a special symbol that indicates the method's return value |
|||
}</lang> |
|||
The design by contract macros throw <tt>Nemerle.AssertionException</tt>'s unless another Exception is specified using the <tt>otherwise</tt> keyword after the <tt>requires/ensures</tt> statement. |
|||
For further details on design by contract macros, see [http://nemerle.org/wiki/index.php?title=Design_by_contract_macros here]. |
|||
=={{header|Nimrod}}== |
=={{header|Nimrod}}== |