Skip to main content

Function Overloading

Zynx supports function overloading — multiple functions can share the same name as long as they differ in their parameter count or parameter types.

Overloading by Parameter Count

fn add(a: i32) -> i32 {
return a;
}

fn add(a: i32, b: i32) -> i32 {
return a + b;
}

fn main() {
let x = add(5); // calls add(i32)
let y = add(5, 10); // calls add(i32, i32)
}

Overloading by Parameter Type

fn describe(a: i32) {
// handles integers
}

fn describe(a: str) {
// handles strings
}

fn main() {
describe(42); // calls describe(i32)
describe("hello"); // calls describe(str)
}

Overload Resolution

When a call matches multiple overloads, the compiler selects the best candidate:

  1. All functions with the matching name are collected.
  2. Candidates are filtered by argument count.
  3. Each candidate is scored based on how well argument types match parameter types — exact matches are preferred over implicit conversions.
  4. The candidate with the lowest conversion cost wins.
  5. If no unique best candidate exists, a compile error is reported.

Overloading with Named Arguments

Overloaded functions work with keyword arguments. The same rules apply — positional arguments first, keyword arguments in parameter order:

fn add(a: i32) -> i32 {
return a;
}

fn add(a: i32, b: i32) -> i32 {
return a + b;
}

fn main() {
let x = add(a: 1); // calls add(i32)
let y = add(a: 1, b: 2); // calls add(i32, i32)
}

Overloading Across Modules

Exported overloaded functions can be called from other modules:

// math.zx
export
fn add(a: i32) -> i32 {
return a;
}

export
fn add(a: i32, b: i32) -> i32 {
return a + b;
}
// main.zx
import math;

fn main() {
let x = math.add(1);
let y = math.add(1, 2);
}

Symbol Mangling

Under the hood, each overload gets a unique mangled name based on the Zynx symbol mangling scheme. For example:

  • add(i32) mangles to _ZN3addEi
  • add(i32, i32) mangles to _ZN3addEii

This allows overloaded functions to coexist in the generated backend IR and final linked output.

See also