Question

I'm trying to process data from stdin in a line-oriented fashion in OCaml, but I'm having trouble getting the types to line up using Stream.iter. I found the following code snippet on the OCaml website (http://ocaml.org/learn/tutorials/streams.html):

let line_stream_of_channel channel =
    Stream.from
      (fun _ ->
         try Some (input_line channel) with End_of_file -> None)

Using this, I've written a simple function that reads a line does a few things, and prints some data back. I'll elide the details, since I believe the function works just fine. It should have the type string -> unit, though, just like (for example) print_endline, and the error I'm getting from the compiler is the same whether I pass my function or print_endline to Stream.iter.

Here's the call:

let read_data =
  Stream.iter ~f:print_summary (line_stream_of_channel In_channel.stdin)

The error I get from the compiler is this:

Error: The function applied to this argument has type 'a Stream.t -> unit
This argument cannot be applied with label ~f

I've used Stream.iter in the past without any trouble. It's confusing that it thinks print_endline has the type 'a Stream.t -> unit - it should be string -> unit, right?

Was it helpful?

Solution

The function with type 'a Stream.t -> unit is Stream.iter. Basically the compiler is telling you that Stream.iter doesn't have any named parameters. You're trying to pass it a parameter named f, so that's the problem.

The solution would be to delete ~f:. (I haven't tested because I don't have your code.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top