# How to get a Caqti\_eio.stdenv from a Eio\_unix.Stdenv.base

**URL:** https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183
**Category:** Ecosystem
**Tags:** eio
**Created:** [February 25, 2024, 8:15pm UTC](https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183 "2024-02-25T20:15:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Frederic\_Loyer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/frederic_loyer/32/3472_2.png) [@Frederic\_Loyer](https://discuss.ocaml.org/u/Frederic_Loyer)
#### Post date: [February 25, 2024, 8:15pm UTC](https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183/1 "2024-02-25T20:15:16Z")

</div>

`Caqti_eio` has a function which needs a parameter of type

```auto
type stdenv = <
  net : [`Generic] Eio.Net.ty Eio.Std.r;
  clock : float Eio.Time.clock_ty Eio.Std.r;
  mono_clock : Eio.Time.Mono.ty Eio.Std.r;
>

```

If I feed it with a `Caqti_eio.stdenv` given by `Eio_main.run`, I have the error:

```plaintext
File "main.ml", line 9, characters 47-53:
9 | Caqti_eio.with_connection ~stdenv
                                                   ^^^^^^
Error: This expression has type Eio_unix.Stdenv.base
       but an expression was expected of type Caqti_eio.stdenv
       The second object type has no method backend_id

```

I do understand that the object I give implements to many methods (`backend_id` is one of them). On most languages, there is an issue when there is not enough methods.

Is there a **simple** way to have a an object which match the type (just the 3 needed methods) based on `stdenv:Eio_unix.Stdenv.base`.

I have tried to patch `caqti`and add a `..` to tolerate other methods, but this doesn’t compile anymore:

```auto
65 | type stdenv = <
66 | net : [`Generic] Eio.Net.ty Eio.Std.r;
67 | clock : float Eio.Time.clock_ty Eio.Std.r;
68 | mono_clock : Eio.Time.Mono.ty Eio.Std.r;
69 | ..
70 | >
Error: A type variable is unbound in this type declaration.
       In type
         < clock : float Eio.Time.clock_ty Eio.Std.r;
           mono_clock : Eio.Time.Mono.ty Eio.Std.r;
           net : [`Generic] Eio.Net.ty Eio.Std.r; .. >
         as 'a the variable 'a is unbound

```

(The error doesn’t print without the `..`).

---

<div class="post-metadata">

### Author: ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)
#### Post date: [February 25, 2024, 8:49pm UTC](https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183/2 "2024-02-25T20:49:31Z")

</div>

Could you give a slightly more complete but still minimalistic code sample which we can use to try to debug the issue?

---

<div class="post-metadata">

### Author: ![Frederic\_Loyer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/frederic_loyer/32/3472_2.png) [@Frederic\_Loyer](https://discuss.ocaml.org/u/Frederic_Loyer)
#### Post date: [February 25, 2024, 8:57pm UTC](https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183/3 "2024-02-25T20:57:36Z")

</div>

I end up to (with a patched ppx\_rapper to support Caqti.2.x):

```auto
let create_query = [%rapper execute "CREATE TABLE t (a INTEGER, b INTEGER)"]

class caqti_env (stdenv:Eio_unix.Stdenv.base) = object
  val s = stdenv
  method net = s#net
  method clock = s#clock
  method mono_clock = s#mono_clock
end

let () =
  match
    Eio_main.run @@ fun stdenv ->
                    Caqti_eio.with_connection ~stdenv:(new caqti_env stdenv)
                      (Uri.of_string "sqlite3:essai.sqlite")
                      (function cnx ->
                         let* () = create_query () cnx in
                         Result.Ok ()
                      )
  with
    | Result.Ok () ->
       output_string "OK\n"
    | Result.Error err ->
       output_string (Caqti_error.show err)

```

converting stdenv to `new caqti_env stdenv` did solved the method issue, but I find the method a little heavy. I guess I should work on a better way.

However, I get a new issue: the `stdenv#net` returns something of type

```auto
[`Generic | `Unix] Eio.Net.ty Eio.Resource.t

```

And the `Unix is not accepted by `Caqti_eio`.

EDIT: I had an simple answer here… [Eio: stdenv type not compatiable with Eio\_unix.Stdenv · Issue #114 · paurkedal/ocaml-caqti · GitHub](https://github.com/paurkedal/ocaml-caqti/issues/114) just type `stdenv :> Caqti_eio.stdenv`

But now, I have the error at the execution:

```auto
Failed to load driver for <sqlite3:essai.sqlite>: Neither caqti-driver-sqlite3 nor the dynamic linker is linked into the application.

```

Curious. The Lwt version works fine, and there is the following stanza in my `dune` file:

```t
  (libraries
      ppx_rapper_eio
      eio eio_main
      caqti-eio
      caqti-driver-sqlite3)

```

---

<div class="post-metadata">

### Author: ![talex5](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/talex5/32/353_2.png) [@talex5](https://discuss.ocaml.org/u/talex5)
#### Post date: [February 26, 2024, 11:30am UTC](https://discuss.ocaml.org/t/how-to-get-a-caqti-eio-stdenv-from-a-eio-unix-stdenv-base/14183/4 "2024-02-26T11:30:19Z")

</div>

It’s because `Caqti_eio.with_connection`’s signature requires something of type `Caqti_eio.stdenv` rather than `<Caqti_eio.stdenv; .. >`. You could work around it by putting this at the start:

```ocaml
module Caqti_eio = struct
  include Caqti_eio

  let with_connection ~stdenv uri fn =
    with_connection ~stdenv:(stdenv :> stdenv) uri fn
end

```

See the [Passing env](https://github.com/ocaml-multicore/eio?tab=readme-ov-file#passing-env) section of the README for more on this.
