aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/debug.pk
diff options
context:
space:
mode:
Diffstat (limited to 'std/prelude/debug.pk')
-rw-r--r--std/prelude/debug.pk32
1 files changed, 32 insertions, 0 deletions
diff --git a/std/prelude/debug.pk b/std/prelude/debug.pk
new file mode 100644
index 0000000..3af1def
--- /dev/null
+++ b/std/prelude/debug.pk
@@ -0,0 +1,32 @@
+## std.debug: Useful functions for debugging.
+## This module is imported by default.
+
+## The special ... syntax is used to mark unimplemented parts of code.
+## Such code will compile, but panic upon being called at runtime.
+## It is usable almost anywhere, including in type declarations, thanks to compiler magic.
+pub func ...: never
+
+## The `assert` macro checks that a provided assertation is true,
+## and panics and dumps information if it is not.
+## Asserts remain in release builds. If not desired, see `debug_assert`
+pub macro assert(cond: bool) =
+ quote:
+ if not `cond`:
+ panic "assertation failed!\n {}".fmt(dbg(`cond`))
+
+## The `debug_assert` function provides an assert that is compiled out in release builds.
+## This is useful for debugging performance-critical code.
+pub macro debug_assert(cond: bool)
+ quote:
+ when debug: # fixme: where is this coming from?
+ assert `cond`
+
+## The `discard` function consumes any type.
+## Useful for throwing away the result of a computation.
+pub func discard[T](self: T) =
+ return
+
+## The `panic` function prints a message to `stderr` and quits.
+pub func panic(message: str): never =
+ stderr.write(message, "\n")
+ std.os.exit(1)