-
Notifications
You must be signed in to change notification settings - Fork 333
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to explain why we are suddenly switching from There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 And indeed, makes sense to add a single sentence that explains that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.
The discussion block does not need project creation instruction. This is covered elsewhere.