Coming from other languages
Notes for people who have written C, C++, Rust, Go, Python or TypeScript, on the places where Zevic behaves differently. If you are new to programming, start with the tutorial instead.
At a glance
| Topic | Zevic |
|---|---|
| Execution | Compiled to native code (LLVM IR, then clang) |
| Types | Static. The type and an initial value are required for every variable |
| Variables | let (immutable, contents cannot change) / mut (mutable) |
| Expressions | if, match and blocks return values. The last expression is the function’s result |
| Failure | No exceptions. Option / Result and ?. Bugs are a Panic |
| null | None (use Option) |
| Memory | Garbage collected (the Boehm GC is bundled) |
| Defining types | struct (value type) / class (reference type) / enum (can carry data) / interface |
| Inheritance | None. Implement an interface, with default methods |
| Generics | Yes. One function is generated per type (monomorphisation) |
| Concurrency | async / await, Async.Channel, OS threads (Thread.Spawn) |
| C interop | extern "C" / export "C". C sources build together |
| Numeric conversion | No implicit conversions (write .ToI32() and so on) |
| Overflow and division by zero | Panic |
What the syntax looks like
module Main
import Zevic.Console
struct Point {
X: I32
Y: I32
fn Init(x: I32, y: I32) { this.X = x; this.Y = y }
fn Dist2() -> I32 { this.X * this.X + this.Y * this.Y }
}
fn Main() {
let p: Point = Point.New(3, 4)
Console.Println("${p.Dist2()}")
}
- Conditions are wrapped in parentheses:
if (x > 0) { ... },while (...),for (x in xs),match (v) { ... } - Values are made with
Point.New(...). The compiler generatesNewfromInit(defining it yourself is an error) - String interpolation is
"${expr}" - Module functions are called with the module name, as in
Console.Println
Notes by language
From C / C++
- No headers are needed. Declare a C function as
extern "C" { fn puts(s: CString) -> CInt }and call it. Only C types are allowed in parameters and results, and you convert with"hello".ToC()orString.FromC(p) - C sources can be compiled together, as in
zevic main.zev native.c -o app - In the other direction, expose a Zevic function with
export "C" fn ...and call it from C.--emit-headeralso writes a C header - The GC manages memory, so there is no
freeto call (for memory allocated on the C side, see “The boundary with the GC” in the specification)
module Main
import Zevic.Console
extern "C" {
fn call_twice(x: CInt) -> CInt
}
export "C" fn zv_double(x: CInt) -> CInt {
(x.ToI32() * 2).ToCInt()
}
fn Main() {
Console.Println("${call_twice(21.ToCInt()).ToI32()}")
}
/* native.c */
extern int zv_double(int);
int call_twice(int x) { return zv_double(x); }
zevic main.zev native.c -o app && ./app
42
From Rust
match,enum(with data),Option/Resultand?feel the same. The difference is that?works only in a function that returnsResult<_, Error>, and the error type is fixed toError- There is no borrow checker and there are no lifetimes. There is a GC
- A
structis a value type and is copied; aclassis a reference type - What a
traitdoes is done by aninterface. There are noimplblocks: methods are written inside the type
From Go
- The GC,
deferandinterfacewill be familiar. Aninterfaceimplementation is stated in the type’s declaration, as in: Shape - Instead of a
(value, err)pair you returnResult<T, Error>and propagate with? - In place of goroutines there are
async { ... }withawait,Async.Channel<T>andThread.Spawn
From Python / JavaScript / TypeScript
- It is statically typed and compiled. Type annotations cannot be omitted
- Instead of
None/undefinedyou get anOption<T>and take it apart withmatch - There are no exceptions. A missing file or a string that is not a number comes back as a
Result - Integer types have fixed widths such as
I32andI64, and you convert explicitly, as in.ToI64(), when mixing them
Things that trip people up
- The
iinfor (i in 0..5)is anI64. To put it into anArray<I32>, convert it withi.ToI32() - You cannot
Push/Seton aletarray or Map. Make itmutto change it - Reading outside an array (
xs[10]) is a Panic. Usexs.Get(10)(anOption) to read safely - It is
Main, notmain, and function names conventionally start with a capital letter - The default optimisation level is
-O0. When measuring speed, usezevic -O2orzevpm build --release