Console
Construction
create Console
Methods
print |
msg: Any |
Void |
Write message without extra formatting. |
print_line |
msg: Any |
Void |
Write message as a line. |
read_line |
prompt?: String |
String |
Read one line from input. |
error |
msg: Any |
Void |
Write to error output. |
new_line |
none |
Void |
Emit blank line. |
flush |
none |
Void |
Flush pending output. |
read_integer |
none |
Integer |
Read and parse integer. |
read_real |
none |
Real |
Read and parse real number. |
Process
A Process value is either the self process (the running Nex program) or a spawned child.
Construction
create Process -- self (the running program)
create Process.self -- same, explicit
create Process.command("ls -lat") -- shell-word split
create Process.command("ls", ["-lat", "/tmp"]) -- explicit argv, no shell
A child is only configured by its constructor — nothing runs until start() is called, which allows set_working_directory/set_redirect_error_to_output/setenv to be applied first.
Methods
getenv |
name: String |
String |
Self: the OS env var. Child: its configured/inherited value. |
setenv |
name: String, value: String |
Void |
Self: sets the current process's env (platform dependent, may raise). Child (pre-start only): records an override applied at start(). |
command_line |
none |
Array[String] |
Self: this program's argv. Child: its own launch argv (command + arguments). |
is_self |
none |
Boolean |
True for the self process. |
is_child |
none |
Boolean |
True for a spawned child. |
set_working_directory |
dir: String |
Void |
Child, pre-start only. |
set_redirect_error_to_output |
flag: Boolean |
Void |
Child, pre-start only. Merges the child's stderr into its stdout stream. |
start |
none |
Void |
Self: no-op. Child: launches the process; raises if the executable can't be found/run. |
is_started |
none |
Boolean |
Self: always true. Child: whether start() has run. |
is_alive |
none |
Boolean |
Self: always true. Child: whether the process is still running. |
pid |
none |
Integer |
Self: the current process id. Child: requires start(). |
wait |
none |
Integer |
Child only. Blocks until exit; returns the exit code. |
wait |
timeout_ms: Integer |
?Integer |
Child only. Blocks up to timeout_ms; nil means still running. |
exit_code |
none |
?Integer |
nil while running, not started, or self. |
terminate |
none |
Void |
Child only. Graceful shutdown (SIGTERM-equivalent). |
kill |
none |
Void |
Child only. Forced shutdown (SIGKILL-equivalent). |
write |
text: String |
Void |
Child only, after start(). Writes to the child's stdin. |
write_line |
text: String |
Void |
Same, plus a line terminator. |
close_stdin |
none |
Void |
Closes the child's stdin (signals EOF to it). |
read_line |
none |
?String |
Reads one line of the child's stdout; nil at EOF. |
read_all |
none |
String |
Reads the rest of the child's stdout up to EOF. |
read_error_line |
none |
?String |
Reads one line of the child's stderr; nil at EOF or when merged via set_redirect_error_to_output. |
read_error_all |
none |
String |
Reads the rest of the child's stderr up to EOF. |
- Every child-only method (
start, wait, terminate, kill, the stream methods, etc.) raises if called on the self process.
start()/wait()/etc. also raise if called before start() where a started process is required, or if start() is called twice.
command(str) splits on whitespace, honoring '...'/"..." grouping (no escape-character support). Use command(str, args) for exact argv control.
- Suspend/resume isn't exposed: there is no portable pause primitive on the JVM (no SIGSTOP/SIGCONT equivalent).
Task
Task values are returned by spawn.
Construction
let t: Task[Integer] := spawn do
result := 42
end
Methods
await |
none |
T |
Wait until the task completes and return its result. |
await |
ms: Integer |
?T |
Wait up to ms milliseconds and return nil on timeout. |
cancel |
none |
Boolean |
Request cancellation of the task. |
is_done |
none |
Boolean |
True if the task has finished. |
is_cancelled |
none |
Boolean |
True if the task was cancelled. |
Notes
- If a task fails,
await re-raises the failure.
- If a cancelled task is awaited, a task-cancelled failure is raised.
await(ms) returns nil only for timeout; failure and cancellation still raise.
Channel[T]
Channel[T] is Nex’s built-in coordination primitive for exchanging values between tasks.
Construction
create Channel[Integer]
create Channel[Integer].with_capacity(2)
Methods
send |
value: T |
Void |
Send a value, blocking until accepted. |
send |
value: T, ms: Integer |
Boolean |
Send with timeout; returns false on timeout. |
try_send |
value: T |
Boolean |
Attempt immediate send. |
receive |
none |
T |
Receive a value, blocking until available. |
receive |
ms: Integer |
?T |
Receive with timeout; returns nil on timeout. |
try_receive |
none |
?T |
Attempt immediate receive. |
close |
none |
Void |
Close the channel for future sends. |
is_closed |
none |
Boolean |
True if the channel is closed. |
capacity |
none |
Integer |
Channel capacity. |
size |
none |
Integer |
Number of buffered values. |
Notes
create Channel[T] creates an unbuffered channel.
create Channel[T].with_capacity(n) creates a buffered channel with capacity n.
- Sends on a closed channel fail.
- Buffered values remain receivable after
close.
- Once a channel is closed and drained,
receive fails.
Atomic_Integer
Construction
create Atomic_Integer.make(0)
Methods
load |
none |
Integer |
Read the current value. |
store |
value: Integer |
Void |
Replace the current value. |
compare_and_set |
expected: Integer, update: Integer |
Boolean |
Replace the value only if it currently matches expected. |
get_and_add |
delta: Integer |
Integer |
Return the old value, then add delta. |
add_and_get |
delta: Integer |
Integer |
Add delta, then return the new value. |
increment |
none |
Integer |
Increment by 1 and return the new value. |
decrement |
none |
Integer |
Decrement by 1 and return the new value. |
Atomic_Integer64
Construction
create Atomic_Integer64.make(0)
Methods
load |
none |
Integer |
Read the current value. |
store |
value: Integer |
Void |
Replace the current value. |
compare_and_set |
expected: Integer, update: Integer |
Boolean |
Replace the value only if it currently matches expected. |
get_and_add |
delta: Integer |
Integer |
Return the old value, then add delta. |
add_and_get |
delta: Integer |
Integer |
Add delta, then return the new value. |
increment |
none |
Integer |
Increment by 1 and return the new value. |
decrement |
none |
Integer |
Decrement by 1 and return the new value. |
Atomic_Boolean
Construction
create Atomic_Boolean.make(false)
Methods
load |
none |
Boolean |
Read the current value. |
store |
value: Boolean |
Void |
Replace the current value. |
compare_and_set |
expected: Boolean, update: Boolean |
Boolean |
Replace the value only if it currently matches expected. |
Atomic_Reference[T]
Construction
create Atomic_Reference[String].make("ready")
create Atomic_Reference.make(nil)
Methods
load |
none |
?T |
Read the current reference value. |
store |
value: ?T |
Void |
Replace the current reference value. |
compare_and_set |
expected: ?T, update: ?T |
Boolean |
Replace the value only if it currently matches expected. |
Notes
Atomic_Reference[T] can store nil.
compare_and_set for Atomic_Reference[T] uses Nex value equality at the language level.
Examples
let con := create Console
con.print("Enter your name: ")
con.flush()
let name := con.read_line()
con.print_line("Hello, " + name)
intern io/Path
let path: Path := create Path.make("notes.txt")
path.write_text("line 1")
path.append_text("\nline 2")
print(path.exists())
print(path.read_text())
let p := create Process
print(p.getenv("HOME"))
print(p.command_line())
let echo := create Process.command("echo", ["hello", "world"])
echo.start()
print(echo.read_line())
print(echo.wait())
let child := create Process.command("cat")
child.start()
child.write_line("ping")
child.close_stdin()
print(child.read_line())
print(child.wait())
let t: Task[Integer] := spawn do
result := 42
end
print(t.await)
let ch: Channel[Integer] := create Channel[Integer].with_capacity(1)
print(ch.try_send(7))
print(ch.receive)
let ai := create Atomic_Integer.make(0)
print(ai.increment)
print(ai.compare_and_set(1, 5))
print(ai.load)
let ref: Atomic_Reference[String] := create Atomic_Reference.make("a")
print(ref.compare_and_set("a", "b"))
print(ref.load)