Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Fieldslib and ppx_fields_conv with OCaml 4.06.1? #2982

Closed
RogerTarani opened this issue Jun 18, 2018 · 7 comments
Closed

How to use Fieldslib and ppx_fields_conv with OCaml 4.06.1? #2982

RogerTarani opened this issue Jun 18, 2018 · 7 comments

Comments

@RogerTarani
Copy link

RogerTarani commented Jun 18, 2018

RWO 2nd Edition promotes the use of Fieldslib (and ppx_fields_conv, depending both on ppxlib).
See First-Class Fields.

I can't generate the record helpers.
How to make Fieldslib and ppx_fields_conv working?

I'm using OCaml 4.06.1 and the required packages are installed:

$ opam list
fieldslib                   v0.11.0  Syntax extension to define first class valu
...
ppx_fields_conv          v0.11.0  Generation of accessor and iteration functi
ppxlib                        0.2.1  A comprehensive toolbox for ppx development

As well as "base" v0.11.0, "jbuilder" 1.0+beta20 (req. build & >= "1.0+beta18.1"} and "ocaml-migrate-parsetree" 1.0.11 (req. >= "1.0") required by these three packages (fieldslib, ppx_fields_conv, ppxlib).

Unfortunately, when trying the example of the book with utop, I don't get the expected output (all the helpers) triggered by [@@deriving fields]:

# module Logon = struct
  type t =
    { session_id: string;
      time: Time_ns.t;
      user: string;
     credentials: string;
    }
  [@@deriving fields]
end;;
module Logon :
  sig
    type t = {
      session_id : string;
      time : Time_ns.t;
      user : string;
      credentials : string;
    }
  end

I tried unsuccessfully:

# #require "fieldslib";;
# open Fieldslib;;

and other rather random directives and commands.

What are the instructions for using Fieldslib (and ppx_fields_conv as well)?

PS : which OCaml tooling configuration (OCaml version, libraries version, jbuilder and related tools, etc. MUST be used to reliably and comfortably study RWO 2nd Edition?
e.g. I note that odoc, odig, ocaml-manual is a useful set of packages for generating module documentation, but ocaml-manual requires ocaml-version >= "4.05.0" & ocaml-version < "4.06.0".
So I if switch to 4.05.x, is there another lurking version conflict that may arise?...

Thanks

@yminsky
Copy link
Member

yminsky commented Jun 18, 2018

Try:

#require "ppx_jane";;
open Base

You can also use the narrower #require "ppx_fields_conv", but the broader import will work for a bunch of other features explored by RWO.

@yminsky
Copy link
Member

yminsky commented Jun 18, 2018

Also, the instructions here on how to set up your .ocamlinit file will get your utop set up properly:

https://dev.realworldocaml.org/install.html

@RogerTarani
Copy link
Author

RogerTarani commented Jun 18, 2018

The following directive worked immediately for me:

#require "ppx_jane"

Then he helpers code was generated:

# module Logon = struct
  type t =
    { session_id: string;
      time: Time_ns.t;
      user: string;
     credentials: string;
    }
  [@@deriving fields]
end;;
module Logon :
  sig
    type t = {
      session_id : string;
      time : Time_ns.t;
      user : string;
      credentials : string;
    }
    val credentials : t -> string
    val user : t -> string
    val time : t -> Time_ns.t
    val session_id : t -> string
    module Fields :
      sig
      (...)
            val set_all_mutable_fields : 'a -> unit
          end
      end
  end

Whereas the following was not enough:

# #require "ppx_fields_conv";;
No such package: ppx_deriving - required by `ppx_fields_conv'

Do you know what is still necessary to only load ppx_fields_conv?

FYI, my ~/.ocamlinit is compliant to your recommendations:

$ cat ~/.ocamlinit
(* ## added by OPAM user-setup for ocamltop / base ## 3ec62baf6f9c219ae06d98140$
#use "topfind";;
(* ## end of OPAM user-setup addition for ocamltop / base ## keep this line *)
#thread;;
#require "core.top";;
#require "core.syntax";;
open Core

Thanks.

@yminsky
Copy link
Member

yminsky commented Jun 18, 2018

Not offhand. This seems like an issue to raise against https://github.com/janestreet/ppx_fields_conv

@RogerTarani
Copy link
Author

RogerTarani commented Jun 19, 2018

Thanks. Done:
janestreet/ppx_fields_conv#2
I'll update and close the present issue when we get the answer.

@NathanReb
Copy link
Collaborator

Any update on this? I assume this can be closed but feel free to ping me if you want it re-opened!

@vitaliel
Copy link
Contributor

vitaliel commented Oct 6, 2019

Maybe we need to put #require "ppx_jane";; on page
https://dev.realworldocaml.org/records.html

Without it [@@deriving fields] does not work in below snippet and user should search the web.

  type t =
    { session_id: string;
      time: Time_ns.t;
      user: string;
      credentials: string;
    }
  [@@deriving fields]
end
;;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants