console
Reading from and writing to the terminal
Console is in the console host’s interface package, nanyx.console.core, and the console host’s prelude brings it, with
print, println, eprint and eprintln, into every module of a program, so it needs no import. A library
lists nanyx.console.core in its dependencies and imports nanyx.console.core/console. The program’s @entry function runs in Console, so print and the rest are available in main without any setup; any other function that calls them must declare [Console] in its type.
match readLine()
| #some(line) -> println("You said: {line}")
| #error(#endOfInput) -> println("No input")
dbg
dbg is built into the language rather than defined in this module, and needs no context:
dbg: a -> a
It prints any value after where it was called, the file and the line, and gives the value back, so it can be put in
the middle of a pipeline. It prints to standard error, or wherever the host puts it: the HTTP host writes it to the
server’s log (Where dbg prints).
[1, 2, 3]
\map { * 2 }
\dbg
-- Prints: [main.nyx:3] [2, 4, 6]
The file is named by its path in its package, such as src/main.nyx, or for a script by its name. It doesn’t print the
expression, which for a dbg in a pipeline would be the whole pipeline. A release build (--release) leaves dbg out:
dbg(x) compiles to x, which prints nothing, so a dbg left in code costs nothing there.
A value is printed as you would write it in code, from its type, whatever toString the type has:
| Value | Printed |
|---|---|
42, 1.5, true | 42, 1.5, true |
"Ada" | "Ada", in quotes and escaped |
#some(5), #pair(1, "a") | #some(5), #pair(1, "a") |
(1, "a"), (name = "Ada", age = 36) | (1, "a"), (age = 36, name = "Ada") |
[1, 2] | [1, 2] |
["tea" => 3, "coffee" => 1], an empty map | Map["tea" => 3, "coffee" => 1], Map[] |
set.fromList([1, 2]), an empty set | Set[1, 2], Set[] |
Money(250) | Money(250) |
() | () |
{ n -> n + 1 } | <function int -> int> |
A record’s fields are printed in alphabetical order. A map’s entries and a set’s elements are printed in the order the map or set holds them, after its type’s name. A nominal type is printed by its name around its shape, in its own module or any other.
Reference
How a program talks to the terminal.
Console is a context. Code that needs the terminal declares [Console] and calls its members,
which come from whichever Console instance its caller supplies. The members are primitives that
take text as it is: print, println, eprint and eprintln are written on top of them,
so an instance supplies write and writeError and gets all four. The console host’s prelude
brings them into every module of a program; a library depends on nanyx.console.core and imports
nanyx.console.core/console.
IoError
type IoError = #endOfInput
Why reading from standard input gave no line: the input has ended.
Console
context Console = (
write: string -> ()
writeError: string -> ()
readLine: () -> #some(string) | #error(IoError)
readToEnd: () -> #some(string) | #error(IoError)
flush: () -> ()
)
The terminal: standard output, standard error and standard input. Code that needs it declares
[Console], and its caller supplies an instance; the host supplies one to the program’s entry point
(the console host’s, nanyx.console, reads and writes the standard streams). print, println, eprint and eprintln write through it.
write: Writestextto standard output, as it is.writeError: Writestextto standard error, as it is.readLine: The next line of standard input, without its line break, or#error(#endOfInput)when the input has ended.readToEnd: Everything left on standard input (the empty string when nothing is).flush: Writes out anything the streams hold back. The console host holds nothing back, so there it does nothing.
print
def print: [Console, ToString(a)] a -> ()
Writes value to standard output as its toString writes it: a string as it is, print(42)
as 42, print([1, 2]) as [1, 2]. A value with no ToString, such as a function, is an error
at the call.
println
def println: [Console, ToString(a)] a -> ()
Writes value followed by a line break to standard output, as print writes it.
eprint
def eprint: [Console, ToString(a)] a -> ()
Writes value to standard error, as print writes it.
eprintln
def eprintln: [Console, ToString(a)] a -> ()
Writes value followed by a line break to standard error, as print writes it.