summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGabriel Scherer <gabriel.scherer@gmail.com>2013-07-21 20:03:52 +0000
committerGabriel Scherer <gabriel.scherer@gmail.com>2013-07-21 20:03:52 +0000
commitcdd43f280062f7d6693eabec6439623ccece6030 (patch)
tree574a2dfc63ebc01110f042e363da2636b0a53f7f
parent4574c718f8b822ae52095e89239df237c4288dfb (diff)
PR#5644: a dumbed-down fix to fix the observable effect (Stream.of_string) with no change to the underlying code (to avoid regressions)
As I learned the hard way, it is essentially impossible to get a satisfying behavior in presence of a mix of updates, concatenations and counts. The best thing to do is thus to not change anything (a good way to preserve compatibility), and only fix the Stream.of_string function to assume nothing of the count passed to the [from] function. I also modified the mli documentation of [Stream.from] to warn other users of this potential pitfall. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13915 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r--stdlib/stream.ml13
-rw-r--r--stdlib/stream.mli7
2 files changed, 18 insertions, 2 deletions
diff --git a/stdlib/stream.ml b/stdlib/stream.ml
index 0a4af3d6e..753bce005 100644
--- a/stdlib/stream.ml
+++ b/stdlib/stream.ml
@@ -145,7 +145,18 @@ let of_list l =
;;
let of_string s =
- from (fun c -> if c < String.length s then Some s.[c] else None)
+ let count = ref 0 in
+ from (fun _ ->
+ (* We cannot use the index passed by the [from] function directly
+ because it returns the current stream count, with absolutely no
+ guarantee that it will start from 0. For example, in the case
+ of [Stream.icons 'c' (Stream.from_string "ab")], the first
+ access to the string will be made with count [1] already.
+ *)
+ let c = !count in
+ if c < String.length s
+ then (incr count; Some s.[c])
+ else None)
;;
let of_channel ic =
diff --git a/stdlib/stream.mli b/stdlib/stream.mli
index 1098a2765..aeb0da1e8 100644
--- a/stdlib/stream.mli
+++ b/stdlib/stream.mli
@@ -32,7 +32,12 @@ val from : (int -> 'a option) -> 'a t
To create a new stream element, the function [f] is called with
the current stream count. The user function [f] must return either
[Some <value>] for a value or [None] to specify the end of the
- stream. *)
+ stream.
+
+ Do note that the indices passed to [f] may not start at [0] in the
+ general case. For example, [[< '0; '1; Stream.from f >]] would call
+ [f] the first time with count [2].
+*)
val of_list : 'a list -> 'a t
(** Return the stream holding the elements of the list in the same