[m-rev.] diff: add checked conversion of int64s to ints
Julien Fischer
jfischer at opturion.com
Wed Oct 9 11:08:16 AEDT 2019
Add checked conversion of int64s to ints.
library/int64.m:
As above.
Julien.
diff --git a/library/int64.m b/library/int64.m
index d1e7763..caaf4c1 100644
--- a/library/int64.m
+++ b/library/int64.m
@@ -51,6 +51,20 @@
% Conversion to int.
%
+ % to_int(I64, I):
+ %
+ % Convert an int64 into an int.
+ % Fails if I64 is not in [int.min_int, int.max_int].
+ %
+:- pred to_int(int64::in, int::out) is semidet.
+
+ % det_to_int(I64) = I:
+ %
+ % Convert an int64 into an int.
+ % Throws an exception if I64 is not in [int.min_int, int.max_int].
+ %
+:- func det_to_int(int64) = int.
+
% cast_to_int(I64) = I:
%
% Convert an int64 to an int.
@@ -398,6 +412,20 @@ cast_from_int(_) = _ :-
%---------------------------------------------------------------------------%
+to_int(I64, I) :-
+ I64 =< cast_from_int(int.max_int),
+ I64 >= cast_from_int(int.min_int),
+ I = cast_to_int(I64).
+
+det_to_int(I64) = I :-
+ ( if to_int(I64, IPrime) then
+ I = IPrime
+ else
+ error("int64.det_to_int: cannot convert int64 to int")
+ ).
+
+%---------------------------------------------------------------------------%
+
:- pragma no_determinism_warning(cast_to_int/1).
:- pragma foreign_proc("C",
More information about the reviews
mailing list