Use cases · C interop
C interop and embedding
Tern compiles to C, so calling C is a declaration, not a binding layer, and anything that can call C can call Tern.
curl -fsSL https://ternlang.dev/install.sh | sh
tern new myapp && cd myappCall C
extern "zlib.h" link "z":
fn crc32(crc: int, data: str, n: int) int
fn main():
text = "hello"
print(crc32(0, text, len(text))) # 907060870The header’s prototypes are checked by the C compiler, and a call to crc32 compiles to a call to crc32. Bundle your own .c files with source "impl.c".
Call Tern
export fn word_count(text: str) int:
n = 0
for w in text.split(" "):
if w.trim() != "":
n += 1
return ntern build --lib lib.tn # liblib.a + lib.h
tern build --shared lib.tn # a shared library for Python's ctypes and friendsThe C interop guide covers arrays, callbacks, opaque handles, runtime errors from C and calls from several threads.
Questions
Does Tern need a binding generator for C?
No. List the functions you want in an extern block with Tern types; the header is included and the C compiler checks the signatures.
Can Python call Tern?
Yes. Build with --shared and load the library with ctypes.
More about Tern
Tern (ternlang) is an early release: the language will change, and so will these numbers. Benchmarks run on an Apple M4 Pro; the full tables, losses included, are on the home page.