Home

Awesome

The jakt programming language

jakt is a memory-safe systems programming language.

It currently transpiles to C++.

NOTE: The language is under heavy development.

Usage

jakt file.jakt
clang++ -std=c++20 -Iruntime -Wno-user-defined-literals output.cpp

Goals

  1. Memory safety
  2. Code readability
  3. Developer productivity
  4. Executable performance
  5. Fun!

Memory safety

The following strategies are employed to achieve memory safety:

In jakt, there are three pointer types:

Null pointers are not possible in safe mode, but pointers can be wrapped in Optional, i.e Optional<T> or T? for short.

Note that weak pointers must always be wrapped in Optional. There is no weak T, only weak T?.

Math safety

For cases where silent integer overflow is desired, there are explicit functions that provide this functionality.

Code readability

Far more time is spent reading code than writing it. For that reason, jakt puts a high emphasis on readability.

Some of the features that encourage more readable programs:

Function calls

When calling a function, you must specify the name of each argument as you're passing it:

rect.set_size(width: 640, height: 480)

There are two exceptions to this:

Structures and classes

There are two main ways to declare a structure in Jakt: struct and class.

struct

Basic syntax:

struct Point {
    x: i64
    y: i64
}

Structs in Jakt have value semantics:

let a = Point(x: 10, y: 5)
let b = a
// "b" is a deep copy of "a", they do not refer to the same Point

jakt generates a default constructor for structs. It takes all fields by name. For the Point struct above, it looks like this:

Point(x: i64, y: i64)

Struct members are public by default.

class

Same basic syntax as struct:

class Size {
    width: i64
    height: i64

    public function area(this) => width * height
}

Classes in Jakt have reference semantics:

Class members are private by default.

Member functions

Both structs and classes can have member functions.

There are three kinds of member functions:

Static member functions don't require an object to call. They have no this parameter.

class Foo {
    function func() => println("Hello!")
}

// Foo::func() can be called without an object.
Foo::func()

Non-mutating member functions require an object to be called, but cannot mutate the object. The first parameter is this.

class Foo {
    function func(this) => println("Hello!")
}

// Foo::func() can only be called on an instance of Foo.
let x = Foo()
x.func()

Mutating member functions require an object to be called, and may modify the object. The first parameter is mutable this.

class Foo {
    x: i64

    function set(mutable this, anonymous x: i64) {
        this.x = x
    }
}

// Foo::set() can only be called on a mutable Foo:
let mutable foo = Foo(x: 3)
foo.set(9)

Arrays

Dynamic arrays are provided via a built-in Array<T> type. They can grow and shrink at runtime.

Array is memory safe:

Declaring arrays

// Function that takes an Array<i64> and returns an Array<String>
function foo(numbers: [i64]) -> [String] {
    ...
}

Shorthand for creating arrays

// Array<i64> with 256 elements, all initialized to 0.
let values = [0; 256]

// Array<String> with 3 elements: "foo", "bar" and "baz".
let values = ["foo", "bar", "baz"]

Dictionaries

function main() {
    let dict = ["a": 1, "b": 2]

    println("{}", dict["a"]!)
}

Tuples

function main() {
    let x = ("a", 2, true)

    println("{}", x.1)
}

Enums and Pattern Matching

enum MyOptional<T> {
    Some: T
    None
}

function value_or_default<T>(anonymous x: MyOptional<T>, default: T) -> T {
    return match x {
        Some(value) => value
        None => default
    }
}

enum Foo {
    StructLikeThingy {
        field_a: i32
        field_b: i32
    }
}

function look_at_foo(anonymous x: Foo) -> i32 {
    match x {
        StructLikeThingy(field_a: a, field_b: b) => {
            return a + b
        }
    }
}

Generics

Jakt supports both generic structures and generic functions.

function id<T>(anonymous x: T) -> T {
    return x
}

function main() {
    let y = id(3)

    println("{}", y + 1000)
}
struct Foo<T> {
    x: T
}

function main() {
    let f = Foo(x: 100)

    println("{}", f.x)
}

Type casts

There are four built-in casting operators in jakt.

Casts for all types

Casts specific to numeric types

Namespaces

(Not yet implemented)

namespace Foo {
    function bar() => 3
}

function main() {
    println("{}", Foo::bar())
}

Traits

(Not yet implemented)

To make generics a bit more powerful and expressive, you can add additional information to them:

trait Hashable {
    function hash(self) -> i128
}

class Foo implements Hashable {
    function hash(self) => 42
}

type i64 implements Hashable {
    function hash(self) => 100
}

The intention is that generics use traits to limit what is passed into a generic parameter, and also to grant that variable more capabilities in the body. It's not really intended to do vtable types of things (for that, just use a subclass)

Safety analysis

(Not yet implemented)

To keep things safe, there are a few kinds of analysis we'd like to do (non-exhaustive):

Error handling

Functions that can fail with an error instead of returning normally are marked with the throws keyword:

function task_that_might_fail() throws -> usize {
    if problem {
        throw Error::from_errno(EPROBLEM)
    }
    ...
    return result
}

function task_that_cannot_fail() -> usize {
    ...
    return result
}

Unlike languages like C++ and Java, errors don't unwind the call stack automatically. Instead, they bubble up to the nearest caller.

If nothing else is specified, calling a function that throws from within a function that throws will implicitly bubble errors.

Syntax for catching errors

If you want to catch errors locally instead of letting them bubble up to the caller, use a try/catch construct like this:

try {
    task_that_might_fail()
} catch error {
    println("Caught error: {}", error)
}

There's also a shorter form:

try task_that_might_fail() catch error {
    println("Caught error: {}", error)
}

Rethrowing errors

(Not yet implemented)

Inline C++

For better interoperability with existing C++ code, as well as situations where the capabilities of jakt within unsafe blocks are not powerful enough, the possibility of embedding inline C++ code into the program exists in the form of cpp blocks:

let mutable x = 0
unsafe {
    cpp {
        "x = (i64)&x;"
    }
}
println("{}", x)