Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind part of a while condition to a name #6357

Closed
vicuna opened this issue Mar 28, 2014 · 4 comments
Closed

Bind part of a while condition to a name #6357

vicuna opened this issue Mar 28, 2014 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Mar 28, 2014

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.

@vicuna
Copy link
Author

vicuna commented Mar 28, 2014

Comment author: @alainfrisch

For complex loops and do-while-kind of conditions, I tend to merge the stop condition and the loop body, using the form while ... do () done.

In your case:

  while
     let x = input_byte () in
     x <> eod && begin
       ... x...;
       true
     end
  do () done

@vicuna
Copy link
Author

vicuna commented Mar 28, 2014

Comment author: @gasche

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.)

@vicuna
Copy link
Author

vicuna commented Jul 16, 2014

Comment author: @damiendoligez

FTR, I also use tail-recursive functions for all non-trivial loops.
Once you've got used to it, it's powerful and quite readable.

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

I don't think there is anything to be done here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant