package ppx_demo

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file test.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
open! Core

let%expect_test _ =
  let x = 5 in
  let value, string = [%demo x + 10] in
  print_endline string;
  print_endline "==========";
  print_endline (Int.to_string value);
  [%expect {|
    x + 10
    ==========
    15 |}]
;;

module M = struct
  type 'a t = T of 'a

  let of_int (x : int) = T x
  let of_string (x : string) = T x

  module Let_syntax = struct
    module Let_syntax = struct
      let bind (T x) ~f = f x
      let map (T x) ~f = T (f x)
    end
  end
end

let%expect_test _ =
  let T value, string =
    [%demo
      let%bind.M pos =
        let%map.M a = M.of_int 70 in
        a
      in
      let%bind.M long_string =
        M.of_string
          "xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     \
           contents     xxxxx"
      in
      let%map.M len = M.of_int 8 in
      String.sub long_string ~pos ~len |> fun x -> x]
  in
  print_endline string;
  print_endline "==========";
  print_endline value;
  [%expect
    {|
    let%bind.M pos =
      let%map.M a = M.of_int 70 in
      a
    in
    let%bind.M long_string =
      M.of_string
        "xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     xxxxx     \
         contents     xxxxx"
    in
    let%map.M len = M.of_int 8 in
    String.sub long_string ~pos ~len |> fun x -> x
    ==========
    contents |}]
;;

let%expect_test "demoing a module's structure" =
  let module Example =
    [%demo
      let x = 1

      module T = struct
        type t =
          | Foo
          | Bar
        [@@deriving sexp]

        let a = Foo
        let b = Bar
      end]
  in
  let x = Example.x in
  let a = Example.T.a in
  let b = Example.T.b in
  print_s [%message (x : int) (a : Example.T.t) (b : Example.T.t)];
  [%expect {| ((x 1) (a Foo) (b Bar)) |}];
  print_endline Example.ppx_demo_string;
  [%expect
    {|
    let x = 1

    module T = struct
      type t =
        | Foo
        | Bar
      [@@deriving sexp]

      let a = Foo
      let b = Bar
    end |}]
;;
OCaml

Innovation. Community. Security.