Skip to content

FFI & Import

FFI & Import

Import forms

Import a module by dotted path

import std.math
import std.string as string

Access functions via the module name or alias:

import std.math as math
func main() -> i32:
if (math.PI > 3.14):
return 0
return 1

Import specific symbols

from std.math import sqrt, abs
from std.string import from_int, concat as cat

Import with include all

import std.math include all

Ambiguous names from multiple include all imports are rejected at use site.

Relative imports

from . import utils
from .. import core
from .utils import helper as helper_alias

Import with alias

import std.io as io

Module sources

SourceExample
Standard libraryimport std.math, import std.io
Project modulesimport src.build.compiler
Relative modulesfrom . import utils

There is no file-path string import (import "path/to/file.tg") in the current compiler.

extern func

extern func declares an external C function by symbol name:

extern func printf(fmt: str) -> i32
func main() -> i32:
printf("Hello, Thagore!\n")
return 0

All parameters and the return type must have explicit type annotations. The return type is required (use void for functions that return nothing):

extern func exit(code: i32) -> void
extern func malloc(size: i32) -> ptr
extern func free(p: ptr) -> void

FFI type mapping

Thagore typeC equivalent
i32int / int32_t
i64int64_t
f64double
strconst char*
ptrvoid*
voidvoid

The symbol name in extern func must exactly match the C symbol name resolved at link time.

Math FFI example

extern func sqrt(x: f64) -> f64
extern func pow(base: f64, exp: f64) -> f64
func hypotenuse(a: f64, b: f64) -> f64:
return sqrt(pow(a, 2.0) + pow(b, 2.0))
func main() -> i32:
return 0