burn / docs

Introduction

Migrating from the Go version

Go version Current Burn
type Person { ... } def type Person { ... }
class Human { fun create(...) ... } def struct Human(...) { ... static fun create(...) ... }
interpreter written in Go compiler, bvm and runtime written in Burn, native code for x86-64 and arm64
burn -exe file.bn embedded the source in a Go program burn build file.bn produces real machine code
every number was a float int and float are separate types
toString(x) needed for "a" + x "a" + x converts automatically
no break/continue both are supported
errors often reported on line 1 every error has an exact line and column

Classes

In the Go version classes were only namespaces for functions and the data lived in a separate type. Now a struct holds its fields and methods together:

def struct Human(name: string, age: int) {
    static fun create(name: string, age: int): Human {
        return new Human(name, age)
    }

    fun greet(): string {
        return "Hello, " + name + "!"
    }
}

var john = Human.create("John", 30)
print(john.greet())
print(Human.greet(john))

Human.greet(john) still works, so most existing call sites compile unchanged once the struct is written.

Classes to structs

Earlier versions of the rewrite had def class. Classes were replaced by structs, and def class is now a compile error ("classes are now structs"). To update a class:

Class Struct
def class Human with the fields string name and int age def struct Human(name: string, age: int)
fun init(name: string) { self.name = name } a constructor parameter name, plus a parameterless fun init() for any other setup
Human("John", 30) or Human { name: "John", age: 30 } new Human("John", 30)
def class Circle: Shape def struct Circle(radius: float) :: Shape
a class with only static functions def static struct Name { ... }
  • Fields that callers set when they create an object, usually the ones without a default, become constructor parameters. Other fields stay in the body and need a default unless their type has a zero value (int, float, bool, string, arrays, maps, optionals and enums).
  • An init with parameters becomes constructor parameters. Code that computed other fields from them moves to a field default (string id = name.lower()) or to fun init() without parameters, which runs after the fields are set.
  • Every place that creates an object needs new. The compiler reports each one with "Human is a struct, so its objects are created with new".
  • Interfaces are listed after :: instead of :.
  • A class that only had static fun methods and static values becomes a def static struct. Inside it the static keyword is optional, and callers keep writing Name.function().
  • def type records are unchanged: Pair(1, "x") and Pair { first: 1, second: "x" } still work.

Before:

def class Account {
    string owner
    int balance
    string id

    fun init(owner: string, balance: int) {
        self.owner = owner
        self.balance = balance
        self.id = owner.lower() + "-1"
    }
}

var a = Account("Ada", 10)

After:

def struct Account(owner: string, balance: int) {
    string id = ""

    fun init() {
        id = owner.lower() + "-1"
    }
}

var a = new Account("Ada", 10)

Numbers

Code that relied on every number being a float may need as int, round(...) or float literals such as 2.0. The compiler points at every place that needs a change.

Imports

import "std/date", import "date" and import "http" keep working. File imports are now resolved relative to the importing file.