Modules

Service

moduleSource
macOSLinuxWindows

#import "Extensions/Service";

Compile-time services. A jaic extension, not official Jai: other Jai compilers have no such module, and it may change between jaic releases. See docs/extensions/service.md.

A service is a long-lived program that compile-time code asks questions: one that keeps a database connection open, say, so #run code can learn a query's types without connecting on every compile. The compiler starts it the first time it is called (jaic <file> -interp), keeps it running between calls, and restarts it when its sources change.

The service side (service.jai):

ExampleOpen in playground ↗
#import "String";
Service :: #import "Extensions/Service";
main :: () {
    Service.serve((request: string) -> Service.Status, string {
        return .OK, to_upper_copy(request);
    });
}

This one is only checked to compile, not run.

Show moreShow less

The compile-time side:

Example
SERVICE :: #run tprint("%service.jai", #filepath);
#run {
    status, answer := Service.call(SERVICE, "hello");
    if status == {
        case .OK;          print("%\n", answer);
        case .PENDING;     Service.unknown("asking the service");   // editor only
        case .UNAVAILABLE; Service.unknown(answer);
        case .FAILED;      compiler_report(answer);
    }
}

Not run: the example is marked ignore.

Not run: it works at compile time, against a running service.

In a build, call waits for the answer. In the editor (a trusted workspace) it answers .PENDING while the service works, and the editor checks the file again once the answer is in. Untrusted workspaces run no compile-time code at all.