Using return in deferred computing in Ocaml Async

I know this question may look very stupid, but whenever I try to find the appropriate answer, I have general questions about return values, so ...

I study deferred computing through OCaml, and I get the most basic concept of this. But when I read the functions regarding deferred computing (mainly ASync docs from from Janestreet; https://ocaml.janestreet.com/ocaml-core/111.28.00/doc/async_kernel/#Deferred.t ), I saw the return function and I won’t get for it.

let x = return 1

creates a pending value that is determined immediately, but what is the destination of the closest values ​​if you are using a pending calculation? Why not just give it a normal value? Is it because other functions (for example, a binding function) take only a delayed value or are there other good reasons?

Examples are welcome. Thank:)

+4
source share
1 answer

You can think of returna function that creates pending values. And it really plays a role when you define it in the context of another deferred value. We pass to the example. Suppose we have a function that reads an input channel of form char and uppercase.

let read_uppercased chan = 
  Reader.read_char >>= function
  | `Eof -> return `Eof
  | `Ok ch -> let ch = Char.uppercase ch in
              return (`Ok ch)

return. , "", eof . ,

let return_eof = return `Eof

.

. , - (, , ). , , . , , bind, , return ... , , . .

, , , .

+4

All Articles