realworldocaml / book

V2 of Real World OCaml
https://dev.realworldocaml.org
Other
1.19k stars 174 forks source link

Query handler example - incomplete information #3633

Open robertblackwell opened 2 years ago

robertblackwell commented 2 years ago

This is by way of a bit of a whinge, though by publishing this info it might help some other poor newbie like me.

Its also a more general comment about what I have found to be an impediment and discouragement to learning Ocaml. Namely a lot of simple things are left un-said and they accumulate to be a real impediment.

I am working on the query handler given as an example in "Real World Ocaml" and got to the point of

#require ppx_jane";;
type u = {a:int; b: float} [@@deriving sexp];;

to which I got the error message "Error: Unbound value int_of_sexp"

well a few hours, and much hair tearing, latter I had the following series of utop commands

#require "base";;
open Base;;
#require ppx_jane";;
type u = {a:int; b: float} [@@deriving sexp];;
sexp_of_u {a=3; b=7.}

which gave :

- : Sexp.t =
Sexplib0.Sexp.List
 [Sexplib0.Sexp.List [Sexplib0.Sexp.Atom "a"; Sexplib0.Sexp.Atom "3"];
  Sexplib0.Sexp.List [Sexplib0.Sexp.Atom "b"; Sexplib0.Sexp.Atom "6"]]

Not at all what I expected nor what the book predicted. So more searching and eventually the additional command

#install_printer Sexplib.Sexp.pp_hum

gave the expected output.

yminsky commented 2 years ago

Yeah, this is a general problem in the book. There are installation instructions that tell you what you need to do (linked to from the guided tour and from the prologue:

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

but the document is a little long, and it's easy to miss the right invocation:

#use "topfind";;
#thread;;
#require "core.top";;

And the "open Core" is at the top of the chapter, as it is at the top of nearly every chapter. ("open Base" works too for what you did.)

I'm honestly not sure how to make this better. Open to suggestions.