FFI & Import
FFI & Import
Import forms
Import a module by dotted path
import std.mathimport std.string as stringAccess functions via the module name or alias:
import std.math as math
func main() -> i32: if (math.PI > 3.14): return 0 return 1Import specific symbols
from std.math import sqrt, absfrom std.string import from_int, concat as catImport with include all
import std.math include allAmbiguous names from multiple include all imports are rejected at use site.
Relative imports
from . import utilsfrom .. import corefrom .utils import helper as helper_aliasImport with alias
import std.io as ioModule sources
| Source | Example |
|---|---|
| Standard library | import std.math, import std.io |
| Project modules | import src.build.compiler |
| Relative modules | from . 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 0All 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) -> voidextern func malloc(size: i32) -> ptrextern func free(p: ptr) -> voidFFI type mapping
| Thagore type | C equivalent |
|---|---|
i32 | int / int32_t |
i64 | int64_t |
f64 | double |
str | const char* |
ptr | void* |
void | void |
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) -> f64extern 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