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

Cookbook: Debug Print a Value with ppx_deriving #2917

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions data/cookbook/debug-print-a-value/00-ppx_deriving.ml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mostafatouny. I just wanted to inform you that your comments don't provide helpful information. This needs to be fixed.
image

The discussion block does not need project creation instruction. This is covered elsewhere.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
packages:
- name: "ppx_deriving"
tested_version: "6.0.3"
used_libraries:
- ppx_deriving
discussion: |
It is required to have `(preprocess (pps ppx_deriving.show))` in `dune` file.
---

(* converting an integer to string *)
let () = print_endline (string_of_int 7)

(* tuple of integer and string *)
let show_pair : (int * string) -> string = [%show: (int * string)]
let () = print_endline (show_pair (3, "hello"))

(* list of tuples, each is a boolean and character *)
let () = print_endline @@ [%show: (bool * char) list] [ (true, 'a'); (false, 'b') ]

(* user-defined type; binary tree with weighted vertices *)
type tree =
| Leaf of float
| Node of float * tree * tree
[@@deriving show]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to explain why we are suddenly switching from [%show: ...] to [@@deriving show].

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It helps to take inspiration from the JSON parsing recipes, to give a better explanation of what @@deriving does.

And indeed, makes sense to add a single sentence that explains that [%show: t] generates the code to print values of type t

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sabine would you recommend me background resources to check? maybe I can write a short explanation and enhance the tutorial's accessibility.

let () = (Node (0.3,
Node (0.5,
Leaf 0.2, Leaf 0.3),
Leaf 0.1) ) |> show_tree |> print_endline

(* Excluding path in printing from a user-defined type *)
type tree_char =
| Leaf of char
| Node of char * tree_char * tree_char
[@@deriving show { with_path = false }]
let foo : tree_char = Node('a', Leaf 'b', Leaf 'c')
let () = foo |> show_tree_char |> print_endline

(* list of boolean option *)
let () = print_endline @@ [%show: (bool option) list] @@ [Some true; None; Some false]

(* string integer result *)
let () = Ok "hello" |> [%show: (string, int) result] |> print_endline
let () = Error 404 |> [%show: (string, int) result] |> print_endline

(* record of a string, integer, and boolean *)
type person = {
last_name : string;
age : int;
is_married : bool
} [@@deriving show]

let gerard = {
last_name = "Touny";
age = 26;
is_married = true
}
let () = print_endline @@ show_person @@ gerard

(* all strings generated above can be used with a formatter like Printf or anything else that works with strings *)
let () =
let i = 20 in
Printf.printf "At line 20 - and variable i is %i" i
Loading