aboutsummaryrefslogtreecommitdiff
path: root/std/prelude/format.pk
diff options
context:
space:
mode:
Diffstat (limited to 'std/prelude/format.pk')
-rw-r--r--std/prelude/format.pk32
1 files changed, 18 insertions, 14 deletions
diff --git a/std/prelude/format.pk b/std/prelude/format.pk
index 7cc1d81..6fc7c99 100644
--- a/std/prelude/format.pk
+++ b/std/prelude/format.pk
@@ -1,15 +1,15 @@
## std.format: Niceties around printing and debugging.
## This module is imported by default.
-## The Display interface. Any type implementing `str` is printable.
+## The Display class. Any type implementing `str` is printable.
## Any type that is Display must necessarily also implement Debug.
-pub type Display = interface
+pub type Display = class
str(Self): str
dbg(Self): str
-## The Debug interface. Broadly implemented for every type with compiler magic.
+## The Debug class. Broadly implemented for every type with compiler magic.
## Types can (and should) override the generic implementations.
-pub type Debug = interface
+pub type Debug = class
dbg(Self): str
## Prints all of its arguments to the command line.
@@ -17,38 +17,42 @@ pub func print(params: varargs[Display]) =
stdout.write(params.map(x => x.str).join(" "), "\n")
## Prints all of its arguments to the command line, in Debug form.
-## Note: this function is special! does not count as a side effect
+##
+## Note: this function is special! It does not count as a side effect.
+## This breaks effect tracking, of course: but `dbg` is for debugging.
+## It will produce a warning in code compiled for release.
+@[no_side_effect]
pub func dbg(params: varargs[Debug]) =
stdout.write(params.map(x => x.dbg).join(" "), "\n")
-## A dummy implementation of the Display interface for strings.
+## A dummy implementation of the Display class for strings.
pub func str(self: str): str = self
-## An implementation of the Debug interface for strings.
+## An implementation of the Debug class for strings.
pub func dbg(self: str): str = "\"" & self & "\""
-## An implementation of the Debug interface for all structs.
+## An implementation of the Debug class for all structs.
## Uses the special `struct` typeclass.
-pub func dbg(self: struct): str =
+pub func dbg[T: Debug](self: struct[T]): str =
"{{}}".fmt(self.fields.map((key, val) => key & ":" & val.dbg))
-## An implementation of the Debug interface for all tuples.
+## An implementation of the Debug class for all tuples.
## Uses the special `tuple` typeclass.
-pub func dbg(self: tuple): str =
+pub func dbg[T: Debug](self: tuple[T]): str =
"({})".fmt(self.fields.map((key, val) =>
key.map(x => x & ":").get_or("") & val.dbg).join(", "))
-## An implementation of the Debug interface for all arrays and lists.
+## An implementation of the Debug class for all arrays and lists.
pub func dbg[T: Debug](self: Iter[T]): str =
"[{}]".fmt(self.map(x => x.dbg).join(", "))
## The fmt macro. Builds a formatted string from its arguments.
pub macro fmt(self: static[str], args: varargs[Display]): str =
let parts = self.split("{}")
- if parts.len != args.len + 1:
+ if parts.len != args.len + 1 then
macro_error("wrong number of arguments")
use std.ast
var res = parts.get(0)!
- for i, arg in args:
+ for i, arg in args do
res &= quote(`parts` & str(`arg`) &) # fixme
res &= parts.last()!
res