[m-rev.] Improved array error messages

Ralph Becket rafe at cs.mu.OZ.AU
Wed Jan 16 11:18:03 AEDT 2002


In response to a Paul Massey's comment:

Estimated hours taken: 0.3
Branches: main

library/array.m:
	Added a new function out_of_bounds_error/3 to construct more
	informative index-out-of-bounds error messages.  Altered
	array__lookup and array__set to use the new function.

Index: array.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/array.m,v
retrieving revision 1.100
diff -u -r1.100 array.m
--- array.m	20 Sep 2001 13:56:04 -0000	1.100
+++ array.m	16 Jan 2002 00:11:07 -0000
@@ -388,7 +388,7 @@
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module exception, int, require.
+:- import_module exception, int, require, string.
 
 /****
 lower bounds other than zero are not supported
@@ -897,7 +897,7 @@
 
 array__lookup(Array, Index, Item) :-
 	( bounds_checks, \+ array__in_bounds(Array, Index) ->
-		throw(array__index_out_of_bounds("array__lookup"))
+		throw(out_of_bounds_error("array__lookup", Array, Index))
 	;
 		array__unsafe_lookup(Array, Index, Item)
 	).
@@ -935,7 +935,7 @@
 
 array__set(Array0, Index, Item, Array) :-
 	( bounds_checks, \+ array__in_bounds(Array0, Index) ->
-		throw(array__index_out_of_bounds("array__set"))
+		throw(out_of_bounds_error("array__set", Array0, Index))
 	;
 		array__unsafe_set(Array0, Index, Item, Array)
 	).
@@ -1606,6 +1606,18 @@
             R = (>),
             B = merge_subarrays(A, B0^elem(I) := X2, Lo1, Hi1, Lo2+1, Hi2, I+1)
         )
+    ).
+
+%------------------------------------------------------------------------------%
+
+    % out_of_bounds_error(OpName, Array, Index) = IndexOutOfBounds.
+    %
+:- func out_of_bounds_error(string, array(T), int) = index_out_of_bounds.
+
+out_of_bounds_error(Op, A, I) =
+    index_out_of_bounds(
+        string__format("%s: array bounds [%d, %d], index %d",
+    	    [s(Op), i(array__min(A)), i(array__max(A)), i(I)])
     ).
 
 %------------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list