The Flix prelude defines two impure functions:
println that can be used to print a
string to standard out.
println function prints with a newline after the
let name = "Lucky Luke"; print("Hello"); print(" "); println(name)
Hello Lucky Luke on one line.
println functions can print any
value whose type implements
ToString type class and
consequently can be converted to a
let o = Some(123); let l = 1 :: 2 :: 3 :: Nil; println(o); println(l)
println functions are rightfully
Consequently they cannot be called from a pure
This can sometimes hinder debugging of a pure function
where you want to log some intermediate computation.
A solution is to cast the
Here is an example:
def sum(x: Int32, y: Int32): Int32 = let _ = println(x) as & Pure; let _ = println(y) as & Pure; x + y
sum remains a pure function despite the
two calls to
Moreover, since the call
println(x) is pure we must
introduce a let-binding with an unused variable to
prevent Flix from rejecting the program due to a
redundant pure computation.