Skip to content

Intermediate

Enoch-199811 edited this page Aug 9, 2026 · 3 revisions

Intermediate — Streams, classes, files and projects

English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch

Real-world usage: streams and forking, classes and objects, the need assumption system, arrays, file IO, threads, and building real projects.

Streams: signature and implementation

A signature stream declares what a stream can do; a fork implements it. Calls go through the signature, implementations plug in.

// Signature: what the stream offers
Stream Greeter {
    void greet(name string);
}

// Fork: how it works
Greeter FriendlyGreeter {
    void greet(name string) {
        CIO::println("hello,", name);
    }
}

Calling:

Greeter::greet("TAK");
// hello, TAK

A call by signature-stream name falls back to its implementation stream.

Streams with fields

Stream Counter {
    int count;
    void bump();
}
Counter C {
    void bump() { this::count = count + 1; }
}

this::count refers to the stream's own field; count resolves through the scope chain. Bare calls (bump()) work inside the stream.

Classes and objects

A class is a stream that can be instantiated:

Class Hero {
    int hp;
    void __init__() {
        this::hp = 100;
    }
    void takeDamage(d int) {
        this::hp = hp - d;
    }
}

Main {
    void exec() {
        Hero h = new Hero();          // __init__ runs
        h::takeDamage(30);
        CIO::println("hp =", h.hp);   // 70
    }
}
  • new ClassName() creates an object; __init__ is the constructor.
  • obj::method(...) calls methods, obj.field reads fields.
  • Obj::set("name", v) / Obj::get("name") access attributes dynamically.
  • A washed-away attribute refuses access: "property X was washed away".

The need assumption system

need declares a dependency; providers are collected automatically:

need value GREETING;          // a constant
need function greet;          // a method named greet
need Stream Greeter;          // a stream
need Class Hero;              // a class
// provider file (any included file):
const string GREETING = "Hello from utils/";

bio build bundles every provider reachable from the main entry recursively — a need without a provider is an error.

Arrays

BioLang arrays grow automatically:

int[] squares = new int[4];
for (int i = 0; i < 4; i = i + 1;) { squares[i] = i * i; }
CIO::println(squares);                    // [ 0 1 4 9 ]

ALL a = new Array(3);
a::set(0, 10);
a::push(40);                              // grows
CIO::println(a::join("-").res);           // join into a string

The Array class and the Vector class are written in BioLang itself; Arrays::count(), Arrays::forget(v) manage live instances.

Multiple return values

void triple(a int) { res a, a * 2, a * 3; }   // respond with several values

ALL t = triple(10);       // t.res is an array [10, 20, 30]
CIO::println(get t);

res a, b, c; responds with several values at once; they arrive as a raw array.

File IO

FIO is the file stream. Open a file, then read/write through the IO core methods:

// Writing:
FIO::open("notes.txt", "w");
FIO::println("line one");
FIO::close();

// Reading (text):
FIO::open("notes.txt");
string line = FIO::getln();
CIO::println("read:", line);
FIO::close();

// Reading (bytes):
FIO::open("data.bin");
int byte = FIO::read();           // 0-255, -1 at EOF

Text methods: print / println / get / getln. Byte methods: write / read. Also FIO::writeFile(path, content) and FIO::readFile(path) for whole-file operations.

SIO — string buffer

SIO keeps an in-memory string that IO methods read/write against — handy for building text:

SIO::println("Hello");
SIO::print("World");
CIO::println(SIO::content());     // Hello\nWorld
CIO::println(SIO::buf());         // whole buffer incl. consumed bytes
SIO::clear();

Tools: format, length, upper, lower, trim, contains, substring, replace.

Threads (cooperative)

Calc Worker {
    void factorial(n int) {
        int r = 1;
        for (int i = 2; i <= n; i = i + 1;) r = r * i;
        res r;
    }
}

Main {
    void exec() {
        ALL t = Threads::spawn("factorial", 10);
        ALL result = Threads::join(t);
        CIO::println("10! =", get result);    // 3628800
    }
}
  • Threads::spawn(name, args...) starts a bare method on a new thread.
  • Threads::join(t) waits and fetches its result.
  • Threads::active(), Threads::self(), Threads::yield().
  • Threads are cooperative — no preemption; a thread must yield or finish for others to run.

Taskm — task manager

Round-robin scheduling of tasks:

Taskm::interval(1);                  // rotate roughly every 1 ms
ALL t1 = Taskm::add("jobA", 5);
Taskm::run();                        // run until all tasks finish
CIO::println(get Threads::join(t1));

Building a project

A project = package.toml + src/ + utils/ + .biolang/deps/:

bio init myapp          # skeleton
# edit src/main.bio, add utils/, declare dependencies
bio build myapp -s      # standalone executable
bio run myapp           # or run interpreted

package.toml:

name = "myapp"
version = "0.1.0"
[dependencies]
libfoo = { version = "1.0.0", repo = "https://github.com/user/libfoo" }

Dependency resolution: the dependency's own repoBIOLANG_CONFIG file → ~/.biolang/config.toml.

Compiling and packaging

bio shell build hello.bio          # → bin/hello (standalone executable)
bio build myapp -s                 # project → standalone
bio build myapp -m                 # project → bin/myapp.img (app + platform CLI + libs)
bio build myapp -m dist.zip        # → .zip package
bio pack hello.img --entry hello bin/hello
bio run hello.img                  # run a package

See Packaging for the .img / .zip formats.

What's next

  • Advanced — smart references, binary libraries, memory, cross-platform.
  • Beginner if you skipped the basics.

Clone this wiki locally