Functions
Functions are fundamental building blocks in Conduit. They are declared with the func keyword and can be passed as first-class values.
Function Declarations
A function declaration specifies parameter names, their types, and an optional return type:
func add(a: Int, b: Int) -> Int {
return a + b
}If a function does not return a value, the -> ReturnType clause is omitted (or specified as -> Void):
func greet(name: String) {
print("Welcome, " + name)
}Trailing parameters can declare default values (= expr):
func greet_with(name: String, prefix: String = "Hello") -> String {
return "{prefix}, {name}!"
}First-Class Functions & Callbacks
In Conduit, functions are first-class values. They can be stored in variables, passed as arguments to other functions, and returned from higher-order functions.
Function Type Signatures
Function types are written as (ParamTypes) -> ReturnType:
func apply_binary(op: (Int, Int) -> Int, x: Int, y: Int) -> Int {
return op(x, y)
}Here, op is any function taking two Int parameters and returning an Int.
Passing Functions
You can pass a declared function directly by name without parentheses:
res := apply_binary(add, 10, 20)
print(res) // Prints 30Anonymous Functions & Closures
You can create anonymous functions inline using func expressions:
double := func(x: Int) -> Int {
return x * 2
}
// Omitting `-> Type` declares a Void closure
notify := func(msg: String) {
print(msg)
}Closures capture enclosing local variables from their lexical scope by value snapshot at creation time and execute inside the module where they were defined. Reassigning a var in the outer scope after creating the closure does not alter the captured value inside the closure.
Control-flow rules inside closures are scoped to the closure itself:
returnand error propagation (?) apply to the closure's own return type, not an enclosing function.breakandcontinueinside a closure may only target loops declared within that same closure.
Generic Functions (func name<T>(...))
Functions can declare type parameters before their parameter list. Call sites may either let Conduit infer type arguments from call arguments or supply them explicitly with <T, ...>:
func first_or_default<T>(items: [T], fallback: T) -> T {
return items.get(0) ?? fallback
}
// Type arguments inferred from arguments:
n := first_or_default([10, 20], 0)
// Explicit type arguments when calling with empty collections:
empty_val := first_or_default<Int>([], 42)Generic functions compose naturally with closures to build reusable collection combinators such as find<T>, any<T>, all<T>, and fold<T, Acc>, and can be exported across module boundaries. Conflicting inferred types for the same type parameter (such as same(1, "two") for func same<T>(a: T, b: T)) are rejected at compile time.
Runnable Example
func add(a: Int, b: Int) -> Int {
return a + b
}
func apply_binary(op: (Int, Int) -> Int, x: Int, y: Int) -> Int {
return op(x, y)
}
func main() -> Void {
print(apply_binary(add, 10, 20))
double := func(x: Int) -> Int {
return x * 2
}
print(double(21))
}Running the Example
conduit run functions.cdtOutput:
30
42Common Errors & Diagnostics
Passing Incompatible Function Signature
If a callback signature does not match the expected parameter or return types:
func is_positive(n: Int) -> Bool {
return n > 0
}
res := apply_binary(is_positive, 10, 20) // Error!Diagnostic:
Type error: Type mismatch: expected '(Int, Int) -> Int', found '(Int) -> Bool'