Standard Library · io
class Console
The program's standard output as an object.
since 0.1.0-alpha.1linuxwindows
Overview
The io library holds one ready-made Console called console, which a file imports with
use io::console;. write and writeln turn any
value into its text form and print it, and << prints a string and returns the console so
several writes chain. Output is text only; the console has no input side, and reading a line
from standard input is done with sysReadLine.
Description
console is the io library's global of class Console, so a file that prints imports it:
use io::console; (and use io::Console; to name the class). It is an ordinary value: it is
resolved and type-checked like any other, and you can store it in a variable or pass it around
(Console c = console;).
console.write(value)writes any single value as text, with no newline.console.writeln(value)writes the value followed by a newline.console.writeln()writes just a newline.console << textwrites a string and returns the console, so writes chain:console << "a" << "b".
write and writeln accept a value of any type and convert it to text for you: strings print as
they are, numbers print in decimal (a float prints its shortest round-trip digits in scientific notation, 3.5e0), a bool prints
true or false, and an array prints as [1, 2, 3]. << takes a string; any other operand (console << 5) is a compile error, so convert it first (console << 5.toString()).
A program's top-level statements run in order; there is no main function to define.
Writing to the console
use io::Console;
use io::console;
console.write("no newline, ");
console.write(42);
console.writeln();
console.writeln("a string");
console.writeln(3.5);
console.writeln(true);
console.writeln([1, 2, 3]);
console << "chained " << "writes" << "\n";
Console alias = console;
alias.writeln("through an alias");
no newline, 42
a string
3.5e0
true
[1, 2, 3]
chained writes
through an alias
Rules
consoleandConsoleare declared in theionamespace. Without an import ofconsole(or ofio),consoleis an unknown name, and the compiler's note names the import to add.consoleis aconst:console = Console();is a compile error (cannot assign to const 'console').writeandwritelntake one argument of any type; use string interpolation ("${a} and ${b}") to combine several values.<<takes a string, and returns the same console.- The output is standard output. A program that also writes to standard error should not rely on the relative order of the two, because standard output may be buffered.
- Code that should not depend on the global console can take an
IConsoleparameter instead; seelang.capability-interfaces.
Examples
The program above shows each form. A function can also take the console as a value:
Passing the console around
use io::Console;
use io::console;
void banner(Console out, string title) {
out << "== " << title << " ==" << "\n";
out.writeln("done");
}
banner(console, "report");
== report ==
done
Notes
Console is not an OutStream; it has the same << shape without being one. A formal output
interface for it is not part of the library yet.
Examples
use io::console;
console.write("total: ");
console.writeln(6 * 7);
console.writeln(true);
console << "chained " << "output\n";
total: 42
true
chained output
Methods
write
write<T>(T v) -> voidPrint the text form of a value without a trailing newline.
Strings print as they are, numbers and booleans as their usual text, and arrays and other objects in their printed form. Successive calls continue on the same line.
Parameters
- v
- The value to print.
Examples
use io::console;
console.write("a");
console.write(1);
console.write(true);
console.writeln();
console.writeln([1, 2, 3]);
a1true
[1, 2, 3]
See also: writeln
writeln
writeln<T>(T v) -> voidPrint the text form of a value followed by a newline.
A float prints its shortest round-trip digits in scientific notation (3.5e0). Strings print as they are.
Parameters
- v
- The value to print.
Examples
use io::console;
console.writeln("hello");
console.writeln(42);
console.writeln(3.5);
console.writeln(false);
hello
42
3.5e0
false
writeln() -> voidPrint a bare newline.
Use it to end a line that was built up with write, or to leave a blank line.
See also: write
Operators
<<
<<(string s) -> ConsolePrint a string and return the console, so several writes chain.
The argument must be a string; use toString() or an interpolated string to print another
kind of value this way.
Parameters
- s
- The string to print.
Returns
This console.
Examples
use io::console;
int n = 3;
console << "n is " << n.toString() << "\n";
console << "done\n";
n is 3
done
See also
- console — The process console: the one
Consoleevery program can print to, imported withuse io::console;. - sysReadLine — Read one line of text from a file descriptor, without its trailing newline.
- IConsole — What a program may say to the terminal.
- OutStream — The writing end of a stream: you push values of type
Tin, and a reader takes them out.