Module Lwt_fmt

Contents

Instructions: Use this module in your project

In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:

  1. Add the following to your project's dependencies/CMakeLists.txt:

    Copy
    DkSDKProject_DeclareAvailable(lwt
        CONSTRAINT "= 5.6.1"
        FINDLIBS lwt lwt.unix)
    DkSDKProject_MakeAvailable(lwt)
  2. Add the Findlib::lwt library to any desired targets in src/*/CMakeLists.txt:

    Copy
    target_link_libraries(YourPackage_YourLibraryName
         # ... existing libraries, if any ...
         Findlib::lwt)
  3. Click your IDE's Build button

Not using DkSDK?

FIRST, do one or all of the following:

  1. Run:

    Copy
    opam install lwt.5.6.1
  2. Edit your dune-project and add:

    Copy
    (package
      (name YourExistingPackage)
      (depends
      ; ... existing dependenices ...
      (lwt (>= 5.6.1))))

    Then run:

    Copy
    dune build *.opam # if this fails, run: dune build
  3. Edit your <package>.opam file and add:

    Copy
    depends: [
      # ... existing dependencies ...
      "lwt" {>= "5.6.1"}
    ]

    Then run:

    Copy
    opam install . --deps-only

FINALLY, add the lwt.unix library to any desired (library)and/or (executable) targets in your **/dune files:

Copy
(library
  (name YourLibrary)
  ; ... existing library options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))

(executable
  (name YourExecutable)
  ; ... existing executable options ...
  (libraries
    ; ... existing libraries ...
    lwt.unix))

This module bridges the gap between Stdlib.Format and Lwt. Although it is not required, it is recommended to use this module with the Fmt library.

Compared to regular formatting function, the main difference is that printing statements will now return promises instead of blocking.

val printf : ``('a, Stdlib.Format.formatter, unit, ``unit Lwt.t)`` format4 -> 'a

Returns a promise that prints on the standard output. Similar to Stdlib.Format.printf.

val eprintf : ``('a, Stdlib.Format.formatter, unit, ``unit Lwt.t)`` format4 -> 'a

Returns a promise that prints on the standard error. Similar to Stdlib.Format.eprintf.

Formatters

type formatter

Lwt enabled formatters

typeorder`` =
| String of string * int * int

(* String (s, off, len) indicate the output of s at offset off and length len.

*)

| Flush

(* Flush operation

*)

valmake_stream : ``unit-> order Lwt_stream.t*formatter

make_stream () returns a formatter and a stream of all the writing order given on that stream.

of_channel oc creates a formatter that writes to the channel oc.

valstdout :formatter

Formatter printing on Lwt_io.stdout.

valstderr :formatter

Formatter printing on Lwt_io.stdout.

valmake_formatter : ``commit:``(``unit->``unitLwt.t)`` -> ``fmt:Stdlib.Format.formatter ->``unit-> formatter

make_formatter ~commit ~fmt creates a new lwt formatter based on the Stdlib.Format.formatter fmt. The commit function will be called by the printing functions to update the underlying channel.

valget_formatter :formatter -> Stdlib.Format.formatter

get_formatter fmt returns the underlying Stdlib.Format.formatter. To access the underlying formatter during printing, it is recommended to use %t and %a.

Printing

valfprintf :formatter -> ``('a, Stdlib.Format.formatter, unit, ``unit Lwt.t)`` format4 -> 'a
val kfprintf : ``(formatter ->``unitLwt.t -> 'a)`` -> formatter -> ``('b, Stdlib.Format.formatter, unit, 'a)`` format4 -> 'b
valifprintf :formatter -> ``('a, Stdlib.Format.formatter, unit, ``unit Lwt.t)`` format4 -> 'a
val ikfprintf : ``(formatter ->``unitLwt.t -> 'a)`` -> formatter -> ``('b, Stdlib.Format.formatter, unit, 'a)`` format4 -> 'b
valflush :formatter ->``unitLwt.t

flush fmt flushes the formatter (as with Stdlib.Format.pp_print_flush) and executes all the printing action on the underlying channel.

Low level functions

valwrite_order :Lwt_io.output_channel -> order ->``unitLwt.t

write_order oc o applies the order o on the channel oc.

valwrite_pending :formatter ->``unitLwt.t

Write all the pending orders of a formatter. Warning: This function flush neither the internal format queues nor the underlying channel and is intended for low level use only. You should probably use flush instead.