(!=)
is physical (pointer) inequality, and the test fails to detect your end mark 0
. When 0
is peeked, Stream.peek returns Some 0
, but it is a different entity from Some 0
of the right hand of the inequality check, and therefore the loop never terminates until it crashes at EOF.
The following demonstrates what is happening:
# Some 0 != Some 0;;
- : bool = true
# let x = Some 0 in x != x;;
- : bool = false
Use (<>)
, structural inequality here. Except it and the omitted run_in_channel
part, the code works fine for me.
A golden rule: do not use physical equality (==)
and (!=)
unless you really need them. Normally, stick to structural equalities (=)
and (<>)
.
-- edit --
There was another issue in the code which was not originally revealed.
Once you create a stream from an in_channel
. Do not touch it by yourself, until you want to close it by close_in
! Let the stream the only reader of it.
The benefit of the stream is that once created, you are freed from taking care of when the actual readings happen. You could still access the channel directly, but it just ruins the benefit completely. Just do not do it. Use Stream.next
or Stream.peek
instead of input_line
in your run
.