|
| 1 | +--- |
| 2 | +packages: |
| 3 | +- name: "ppx_deriving" |
| 4 | + tested_version: "6.0.3" |
| 5 | + used_libraries: |
| 6 | + - ppx_deriving |
| 7 | +discussion: | |
| 8 | + To use `[@@deriving show]`, you need to add the `ppx_deriving.show` preprocessor in your dune file, like this: `(preprocess (pps ppx_deriving.show))`. |
| 9 | +--- |
| 10 | + |
| 11 | +(* converting an integer to string *) |
| 12 | +let () = print_endline (string_of_int 7) |
| 13 | + |
| 14 | +(* tuple of integer and string *) |
| 15 | +let show_pair : (int * string) -> string = [%show: (int * string)] |
| 16 | +let () = print_endline (show_pair (3, "hello")) |
| 17 | + |
| 18 | +(* list of tuples, each is a boolean and character *) |
| 19 | +let () = print_endline @@ [%show: (bool * char) list] [ (true, 'a'); (false, 'b') ] |
| 20 | + |
| 21 | +(* user-defined type; binary tree with weighted vertices *) |
| 22 | +type tree = |
| 23 | + | Leaf of float |
| 24 | + | Node of float * tree * tree |
| 25 | + [@@deriving show] |
| 26 | +let () = (Node (0.3, |
| 27 | + Node (0.5, |
| 28 | + Leaf 0.2, Leaf 0.3), |
| 29 | + Leaf 0.1) ) |> show_tree |> print_endline |
| 30 | + |
| 31 | +(* Excluding path in printing from a user-defined type *) |
| 32 | +type tree_char = |
| 33 | + | Leaf of char |
| 34 | + | Node of char * tree_char * tree_char |
| 35 | + [@@deriving show { with_path = false }] |
| 36 | +let foo : tree_char = Node('a', Leaf 'b', Leaf 'c') |
| 37 | +let () = foo |> show_tree_char |> print_endline |
| 38 | + |
| 39 | +(* list of boolean option *) |
| 40 | +let () = print_endline @@ [%show: (bool option) list] @@ [Some true; None; Some false] |
| 41 | + |
| 42 | +(* string integer result *) |
| 43 | +let () = Ok "hello" |> [%show: (string, int) result] |> print_endline |
| 44 | +let () = Error 404 |> [%show: (string, int) result] |> print_endline |
| 45 | + |
| 46 | +(* record of a string, integer, and boolean *) |
| 47 | +type person = { |
| 48 | + last_name : string; |
| 49 | + age : int; |
| 50 | + is_married : bool |
| 51 | +} [@@deriving show] |
| 52 | + |
| 53 | +let gerard = { |
| 54 | + last_name = "Touny"; |
| 55 | + age = 26; |
| 56 | + is_married = true |
| 57 | +} |
| 58 | +let () = print_endline @@ show_person @@ gerard |
| 59 | + |
| 60 | +(* all strings generated above can be used with a formatter like Printf or anything else that works with strings *) |
| 61 | +let () = |
| 62 | + let i = 20 in |
| 63 | + Printf.printf "At line 20 - and variable i is %i" i |
0 commit comments