Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-17 (06:19)
From: Johan Baltié <johan.baltie@w...>
Subject: Re: [Caml-list] Statically detecting arrays bound exceptions ?? (was: Universal Serializer)
Ok as there were the two same comments on my post I do a single answer.

Here my first post:

>> What about defining type that are subranges of int ?
>> à la ADA...

Here the comments:
>Then how do you make sure that the result of an arithmetic expression is
>still within that sub-range? For example, m.( i + j ) ?
>Hao-yang Wang


>as i said to john skaller, won't you then get index incrementation exceptions ?
>anyway, it's a trade-off.
> writes:


Now, as I am not an ADA guru I just show you my source:

------------- BEGIN OF FOLLOWUP -------------

> Does any language or any machine provide some mechanism to:
> 1. index an array without checking its bounds
> 2. throw an exception if the index was actually out of range
> 3. allow the programmer to catch and handle the exception rather than
>    terminate the program

Well, Ada does. The strong typing gives information to the compiler for it to
deduce when range checking is not needed:

  subtype Index is Integer range 1..10;
  type Arr is array (Index) of Integer;
  a : Arr;
  element : Integer;
  j : Index := 1;
  k : Integer := 11;
  for i in a'Range loop
    element := a(i); -- no range checking needed, i is in range by definition
  end loop;
  a(j); -- range checking not needed, j is within Index by definition
  a(k); -- range checking needed due possibility of k being outside of Index
  when Constraint_Error =>
     -- process the out-of-range error from a(k)

Cheers,                                        The Rhythm is around me,
                                               The Rhythm has control.
Ray Blaak                                      The Rhythm is inside me,                            The Rhythm has my soul.

------------- END OF FOLLOWUP -------------

IMHO, the use of subtypes help to avoid a lot of bound check, maybe not all of
them, but I do not think that eliminating *all* the bounds check is useful.

Another point is that using subranges you're not allowed to do many errors as
when you are using bare int.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: