Capabilities: uses
● Intermediate3 min
A pub function must write its uses clause. A private one may leave it out: the compiler works it out.
A pub function that reads a file without saying so is refused, and the message names what to add:
use std.fs
pub fn load(name: String) -> String ! FsError {
try fs.read_text(name)
}The compiler refused the program:
kavach run cap-pub.kav
error: `load` is `pub`, so it must say what it uses: add `uses Fs.read` (spec 8.1)A private function may leave uses out. The compiler works out what it touches, and that still counts for whoever calls it:
fn shout(message: String) {
print("{message}!")
}
pub fn greet(name: String) uses Console {
shout("Namaste, {name}")
}
fn main() {
greet("Asha")
}The program ran and printed:
kavach run cap-inferred.kav
Namaste, Asha!So a pub function that calls it must list it too:
fn shout(message: String) {
print("{message}!")
}
pub fn greet(name: String) {
shout("Namaste, {name}")
}
fn main() {
greet("Asha")
}The compiler refused the program:
kavach run cap-inferred-caller.kav
error: `greet` is `pub`, so it must say what it uses: add `uses Console` (through `shout`) (spec 8.1)