Skip to content

Commit 4ca42fe

Browse files
feat: update debugging.md (#261)
1 parent 304cafb commit 4ca42fe

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/debugging.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ Unfortunately this does not work:
1717
```
1818
❌ -- Type Error -------------------------------------------------- Main.flix
1919
20-
>> Impure function declared as pure.
20+
>> Unable to unify the effect formulas: 'IO' and 'Pure'.
2121
22-
1 | def sum(x: Int32, y: Int32): Int32 =
23-
^^^
24-
impure function.
22+
1 |> def sum(x: Int32, y: Int32): Int32 =
23+
2 |> println(x);
24+
3 |> println(y);
25+
4 |> x + y
2526
```
2627

27-
The problem is that printing is inherently an effectful operation and hence we
28-
cannot use it to debug our pure functions! We could make our `sum` function have
29-
the `IO` effect, but that is rarely what we want. Fortunately, Flix has a
28+
The problem is that `println` has the `IO`. Hence, we cannot use it to for
29+
print debugging inside pure functions. We could make our `sum` function have
30+
the `IO` effect, but that is rarely what we want. Instead, Flix has a
3031
built-in debugging facility that allows us to do print-line debugging.
3132

3233
### The `Debug.dprint` and `Debug.dprintln` Functions
3334

34-
Instead, we can write the following:
35+
We can write:
3536

3637
```flix
3738
use Debug.dprintln;
@@ -46,6 +47,18 @@ Inside the `sum` function, the `dprintln` has the effect `Debug`, but due to
4647
its special nature, the `Debug` effect "disappears" once we exit the function,
4748
i.e. it is not part of its type and effect signature.
4849

49-
We should only use `dprintln` and `dprint` for debugging.
50+
### Debugging with Source Locations
5051

51-
A future version of Flix will disallow it in production mode.
52+
We can use the special _debug string interpolator_ to add source locations
53+
to our print statements:
54+
55+
```flix
56+
use Debug.dprintln;
57+
58+
def sum(x: Int32, y: Int32): Int32 =
59+
dprintln(d"Hello World!");
60+
x + y
61+
```
62+
63+
> A longer introduction to `dprintln` is available in the
64+
> blog post [Effect Systems vs Print Debugging: A Pragmatic Solution](https://blog.flix.dev/blog/effect-systems-vs-print-debugging/)

0 commit comments

Comments
 (0)