Effects and permissions#
A function is pure unless a comment permits what it reaches. ms check --only lint reports a function that prints, reads a file, calls the network or reads the clock without that permission, and names the call that brings the effect in. Effects are comments, so the language has no extra syntax.
fn total(prices List[Int]) Int {
print(f"summing {prices.length()} prices")
sum := prices.sum()
return sum
}
fn main() Void {
print(total([2, 3]))
}a.ms:2:5: warning MSL116: `total` reaches std.console.write: total -> print; permit it with `// ms: allow_effect std.console.write`, move the call to the caller, or take a capability parametermain, test and fixture may do anything, so main is not reported. total is: the print is a surprise in a function that only adds numbers.
Permit what a function needs#
// ms: allow_effect NAME directly above a function permits it to reach that effect. Put the lines at the top of a file, ended by a blank line, to cover every function in the file. A function’s own lines can only narrow its file’s.
// ms: allow_effect std.fs.read
import (
Bytes from "@std/bytes.ms",
read_file from "@std/os/fs.ms",
)
fn load(path String) Bytes! {
return read_file(path).wait()
}
fn main() Void {
return
}Each line names one effect or a pattern:
| Line | Permits |
|---|---|
std.fs.read | that effect only |
std.fs.* | every effect under std.fs |
*.read | every effect that ends in read |
* | everything |
A file with no line is pure. Prefer the narrowest line, and prefer passing a capability over a wide permission.
Pass a capability instead#
A function that is handed an interface value does not need a permission to use it. The permission belongs where the value is made.
interface Logger {
fn info(message String) Void
}
type ConsoleLogger {
prefix String
}
// ms: allow_effect std.console.write
fn ConsoleLogger.info(message String) Void {
print(f"{self.prefix}{message}")
}
fn total(log Logger, prices List[Int]) Int {
log.info(f"summing {prices.length()} prices")
sum := prices.sum()
return sum
}
fn main() Void {
print(total(ConsoleLogger("> "), [2, 3]))
}total is pure here, so a test can pass a logger that records messages. Turning a ConsoleLogger into a Logger inside a pure function would be reported, because that is where the authority is obtained. The same holds when a function is written as a closure, named as a value, or passed for a generic parameter.
Declare your own effect#
// ms: effect NAME says a function performs NAME. Callers see that name instead of what the body reaches, so a wrapper can hide how it works.
import write_file from "@std/os/fs.ms"
// ms: effect db.write
// ms: allow_effect std.fs.write
fn save(order String) Void! {
return write_file("orders.log", order).wait()
}
// ms: allow_effect db.*
fn checkout(total Int) Void! {
return save(f"order {total}")
}
fn audit(total Int) Void! {
return save(f"audit {total}")
}
fn main() Void {
return
}save may write a file and declares db.write. checkout may use any db effect but not the file system directly. audit has no line, so it is reported for db.write. Names starting with std. belong to the standard library.
Standard-library effects#
Standard-library functions declare what they let a caller do. Calls through a handle you already hold, such as reading from an open file, do not need a second permission.
| Effect | Covers |
|---|---|
std.fs.read, std.fs.write, std.fs.delete | files and directories |
std.net.connect, std.net.listen | network clients and servers |
std.process.run, std.process.signal, std.process.read, std.process.write | starting, signalling and inspecting processes |
std.env.read, std.env.write | environment variables and the working directory |
std.clock.read, std.clock.sleep | the current time and waiting |
std.random.read | random values |
std.console.read, std.console.write | standard input, output and error |
std.system.read | host facts such as the hostname and system roots |
ms search read_file prints effects: std.fs.read, and hovering a function in the editor shows its effects, its permissions, or that it is pure.
Seeing and reviewing effects#
ms check --only lintreportsMSL116for unpermitted effects (--deny-warningsmakes it fail;--fixinserts the minimal// ms: allow_effectline above the function) andMSL136as an error for malformed or unknown// ms:directives and userstd.*declarations.- Hovering a function or method in the editor shows its effects, its permissions, or
effects: pure (ambient authority only). ms map --view review ms.toml --diff change.patchlists new permissions and newly reached effects as “new effect” changes (EFFECT+in--json), permissions first.--effect 'std.net.*'keeps only the findings for one effect.- Understanding effects describes the separate call-graph view.
Rules and limits#
- Defaults charge the caller: an omitted parameter or field default expression is checked against the caller’s permissions, not the callee’s.
- Generics and implicit calls: generic functions and methods charge the interface methods and
closeof their concrete type arguments;==/!=,</<=/>/>=,f"{x}",for x in iter, and scope-endclosecharge the concrete type’sequals,compare,to_string,next, andclosemethods. - Fetched packages: a dependency package cannot exceed the effects declared in its
ms.toml, even if its source writes// ms: allow_effect *. - Foreign functions: a user-declared
extern fncarries theffieffect by default unless it declares explicit// ms: effectlines, so foreign code is never treated as pure. - Capabilities: ambient-pure functions may call interface methods or function values handed to them; effects are not tracked through runtime values or checked while the program runs.