Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006357OCaml~DO NOT USE (was: OCaml general)public2014-03-28 16:322016-12-08 13:25
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusclosedResolutionwon't fix 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0006357: Bind part of a while condition to a name
DescriptionWhen 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...

(pollutes scope with name 'fin', even though not relevant outside loop body)


exception Finished

  while true do
    match input_byte () with
      x when x = eod -> raise Finished
    | x -> ...x...
  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

(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

Now x is only available in the loop body, which is neater.

TagsNo tags attached.
Attached Files

- Relationships
related to 0006685acknowledged Allow "if let" syntax 

-  Notes
frisch (developer)
2014-03-28 17:23

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

In your case:

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

gasche (developer)
2014-03-28 17:48

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

(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.)
doligez (administrator)
2014-07-16 15:52
edited on: 2014-07-16 15:53

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.

shinwell (developer)
2016-12-08 13:24

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

- Issue History
Date Modified Username Field Change
2014-03-28 16:32 johnwhitington New Issue
2014-03-28 17:23 frisch Note Added: 0011134
2014-03-28 17:48 gasche Note Added: 0011135
2014-07-16 15:52 doligez Note Added: 0011848
2014-07-16 15:53 doligez Note Edited: 0011848 View Revisions
2014-07-16 15:53 doligez Status new => feedback
2014-12-01 09:42 yallop Relationship added related to 0006685
2016-12-08 13:24 shinwell Note Added: 0016869
2016-12-08 13:25 shinwell Status feedback => closed
2016-12-08 13:25 shinwell Resolution open => won't fix
2017-02-23 16:36 doligez Category OCaml general => -OCaml general
2017-03-03 17:55 doligez Category -OCaml general => -(deprecated) general
2017-03-03 18:01 doligez Category -(deprecated) general => ~deprecated (was: OCaml general)
2017-03-06 17:04 doligez Category ~deprecated (was: OCaml general) => ~DO NOT USE (was: OCaml general)

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker