File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed
Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,24 @@ extension [T](inline t: T)
8181 try f(e)
8282 catch case ee : Throwable => e.addSuppressed(ee)
8383 throw e
84+
85+ /** Print the value of this expression, preceeded with the given label, and return the original value.
86+ *
87+ * @example
88+ * {{{
89+ * import ox.debug
90+ * val x = 20
91+ * x.debug("current value of x")
92+ *
93+ * // prints: current value of x: 20
94+ * }}}
95+ *
96+ * @see
97+ * [[debug ]] as a top-level method for printing the code corresponding to an expression, alongside its value.
98+ */
99+ inline def debug (label : String ): T =
100+ println(s " $label: $t" )
101+ t
84102end extension
85103
86104extension [T ](inline f : Future [T ])
@@ -115,7 +133,9 @@ inline def timed[T](operation: => T): (FiniteDuration, T) =
115133 val duration = (after - before).nanos
116134 (duration, result)
117135
118- /** Prints the code and result of the expression to the standard output. Equivalent to `println(xAsCode + " = " + x)`.
136+ /** Prints the code and the value of the expression to the standard output. Equivalent to `println(xAsCode + " = " + x)`.
137+ * @see
138+ * [[debug ]] as an extension method on a value.
119139 *
120140 * @example
121141 * {{{
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ Top-level methods:
1010* ` sleep(scala.concurrent.Duration) ` blocks the current thread/fork for the given duration; same as ` Thread.sleep ` , but
1111 using's Scala's ` Duration `
1212* ` debug(expression) ` prints the code representing the expression, and the value of the expression to standard output,
13- using ` println `
13+ using ` println ` .
1414
1515Extension functions on arbitrary expressions:
1616
@@ -21,9 +21,29 @@ Extension functions on arbitrary expressions:
2121 operations
2222* ` .tapException(Throwable => Unit) ` and ` .tapNonFatalException(Throwable => Unit) ` allow running the provided
2323 side-effecting callback when the expression throws an exception
24+ * ` .debug(label) ` prints the value preceeded with the given label, and returns the original value
2425
2526Extension functions on ` scala.concurrent.Future[T] ` :
2627
2728* ` .get(): T ` blocks the current thread/fork until the future completes; returns the successful value of the future, or
2829 throws the exception, with which it failed
2930
31+ ## Examples
32+
33+ Debug utilities:
34+
35+ ``` scala mdoc
36+ import ox .*
37+
38+ val x = 20
39+ val y = 10
40+ debug(x * 2 + y)
41+ ```
42+
43+ ``` scala mdoc
44+ import ox .*
45+
46+ def transform (n : Int ): Long = n * n * n
47+
48+ transform(5 ).debug(" transformation result" )
49+ ```
You can’t perform that action at this time.
0 commit comments