List Info

Thread: "ocaml_beginners"::[] what is assert?




"ocaml_beginners"::[] what is assert?
country flaguser name
United States
2007-08-09 12:58:24

I see a lot of examples using "assert" and I kind of understand what
it does but I couldn't find any documentation that precisely describe
what assert shall and can do, I certainly can not find it in the
Standard Library and tutorials. Please enlighten me.
Thanks.
MH

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] what is assert?
country flaguser name
United States
2007-08-09 13:11:30

On 8/9/07, cugf_0956 < mzouk%40altavista.com">mzoukaltavista.com> wrote:
&gt; I see a lot of examples using "assert" and I kind of understand what
>; it does but I couldn't find any documentation that precisely describe
> what assert shall and can do, I certainly can not find it in the
> Standard Library and tutorials. Please enlighten me.
> Thanks.
> MH

You can think of "assert" as a function which takes a boolean value
and raises an exception if it is false. You can use assert to document
assumptions you've made in your code and check simple properties you
think should hold... Sort of the first line of runtime testing. When
you're ready to ship, you can compile with -noassert and all (well,
most) of the assert statements will be compiled away

See here:
manual.kwd173">http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#manual.kwd173

Regards,
Chris

>
>
>
&gt; Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
>; Yahoo! Groups Links
&gt;
>
>
>;
>

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] what is assert?
country flaguser name
United Kingdom
2007-08-09 13:00:21

On Thursday 09 August 2007 18:58:24 cugf_0956 wrote:
&gt; I see a lot of examples using "assert" and I kind of understand what
>; it does but I couldn't find any documentation that precisely describe
> what assert shall and can do, I certainly can not find it in the
> Standard Library and tutorials. Please enlighten me.

It throws the Assert_failure exception if the given value is false:

# assert true;;
- : unit = ()
# assert false;;
Exception: Assert_failure ("&quot;, 1, 0).

its definition might be:

# let assert b = if not b then raise (Assert_failure(&quot;", 0, 0));;
val assert : bool -> unit = <fun>;

except that the exception contains the line and character of the assert in the
source code.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )