You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 6357 Reporter:@johnwhitington Status: closed (set by @mshinwell on 2016-12-08T12:25:32Z) Resolution: won't fix Priority: normal Severity: feature Category: ~DO NOT USE (was: OCaml general) Related to:#6685 Monitored by:@ygrek
Bug description
When doing, for example, input with imperative constructs, there seem to be three ways to test the input for end of data as part of the conditional, whilst also using its value in the loop:
let fin = ref false
while !fin do
match input_byte () with
x when x = eod -> fin := true
| x -> ...x...
done
(pollutes scope with name 'fin', even though not relevant outside loop body)
exception Finished
try
while true do
match input_byte () with
x when x = eod -> raise Finished
| x -> ...x...
done
with
Finished -> ......
(needs an exception - same one each time, or define a new one each time?)
let x = ref 0 in
while x := input_byte (); !x <> eod do
...!x...
done
(pollutes scope with name 'x', even though not relevant outside loop body)
It would be nice to be able to bind part of the expression to a name, like this:
while input_byte () as x <> eod do
...x...
done
Now x is only available in the loop body, which is neater.
The text was updated successfully, but these errors were encountered:
Whenever control gets complex, I tend to revert to tail-recursive functions:
let rec loop () =
let x = input_byte () in
if x <> eod then begin
...
loop ()
end
in loop ()
I personally doubt the proposed change is worth a new language feature. Long-term I would be interested in syntactic sugar for iteration:
for x in input_bytes stdin do
...
done
(but I think for such a feature to be worthwile you need to support folding, monadic comprehensions and possibly generators/yield with a coherent syntax, which is easier said than done.)
Original bug ID: 6357
Reporter: @johnwhitington
Status: closed (set by @mshinwell on 2016-12-08T12:25:32Z)
Resolution: won't fix
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Related to: #6685
Monitored by: @ygrek
Bug description
When doing, for example, input with imperative constructs, there seem to be three ways to test the input for end of data as part of the conditional, whilst also using its value in the loop:
let fin = ref false
while !fin do
match input_byte () with
x when x = eod -> fin := true
| x -> ...x...
done
(pollutes scope with name 'fin', even though not relevant outside loop body)
exception Finished
try
while true do
match input_byte () with
x when x = eod -> raise Finished
| x -> ...x...
done
with
Finished -> ......
(needs an exception - same one each time, or define a new one each time?)
let x = ref 0 in
while x := input_byte (); !x <> eod do
...!x...
done
(pollutes scope with name 'x', even though not relevant outside loop body)
It would be nice to be able to bind part of the expression to a name, like this:
while input_byte () as x <> eod do
...x...
done
Now x is only available in the loop body, which is neater.
The text was updated successfully, but these errors were encountered: