aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/debug.pk
blob: 92d31ddf67ce0e256bcc3d2ec38f863105568a38 (plain) (blame)
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
## std.debug: Useful functions for debugging.
## This module is imported by default.

## 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 `dbg_assert`
pub macro assert(cond: bool) =
  quote
    if not `cond` then
      panic "assertation failed!\n {}".fmt(dbg(`cond`))

## The `dbg_assert` function provides an assert that is compiled out in release builds.
## This is useful for debugging performance-critical code.
pub macro dbg_assert(cond: bool) =
  quote
    when debug then # fixme: where is this constant coming from?
      assert `cond`

## The `discard` function consumes an object of 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)

## 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.
@[magic]
pub func ...: never =
  panic("unimplemented")