Skip to content

Doc Comments

Doc comments are special comments that the editor displays when hovering over a symbol. Zolo supports two styles: /// for individual lines and /** */ for long blocks.

/// is the preferred style for short functions and type members. Each line prefixed with /// is concatenated and rendered as markdown in the hover:

Doc comments /// on functions, /** */ block on is_prime; annotations on struct fields, enum variants, and trait methods.

02-doc-comments.zolo
Playground
// ============================================================

// Doc Comments in Zolo

// ============================================================

// Doc comments (///) are special: they appear in the editor's

// hover when you mouse over the symbol.


// -- Line doc comments (///) ----------------------------------


/// Computes the area of a circle given its radius.

fn circle_area(radius: float) -> float {
    return 3.14159 * radius * radius
}

/// Computes the factorial of an integer.

/// Returns 1 for n <= 1.

fn factorial(n: int) -> int {
    if n <= 1 { return 1 }
    return n * factorial(n - 1)
}

print("Area: {circle_area(5.0)}")
print("5! = {factorial(5)}")

// -- Block doc comments (/** */) ------------------------------


/**
 * Checks whether a number is prime.
 * Tests divisibility from 2 up to the square root of n.
 */
fn is_prime(n: int) -> bool {
    if n < 2 { return false }
    var i = 2
    while i * i <= n {
        if n % i == 0 { return false }
        i += 1
    }
    return true
}

print("7 prime? {is_prime(7)}")
print("10 prime? {is_prime(10)}")

// -- Doc comments on structs ----------------------------------


/// Represents a 2D point on the Cartesian plane.

struct Point {
    // Horizontal coordinate

    x: float,
    // Vertical coordinate

    y: float,
}

impl Point {
    // Creates a new point at the origin (0, 0)

    fn origin() -> Point {
        return Point { x: 0.0, y: 0.0 }
    }

    // Computes the distance from this point to the origin

    fn distance_to_origin(self) -> float {
        return (self.x ** 2.0 + self.y ** 2.0) ** 0.5
    }
}

let p = Point { x: 3.0, y: 4.0 }
print("Distance: {p.distance_to_origin()}")

// -- Doc comments on enums ------------------------------------


/// Represents the cardinal directions.

enum Direction {
    // North - upward

    North,
    // South - downward

    South,
    // East - to the right

    East,
    // West - to the left

    West,
}

let dir = Direction::North
match dir {
    Direction::North => print("Heading north"),
    Direction::South => print("Heading south"),
    Direction::East => print("Heading east"),
    Direction::West => print("Heading west"),
}

// -- Doc comments on traits -----------------------------------


/// Defines behavior for objects that can be displayed.

trait Displayable {
    /// Returns a textual representation of the object.

    fn display(self) -> str
}

impl Displayable for Point {
    fn display(self) -> str {
        return "({self.x}, {self.y})"
    }
}

let q = Point { x: 1.0, y: 2.0 }
print("Point: {q.display()}")

Doc comments work on any top-level declaration: free functions, structs, enums, traits, and their respective members. Inner comments (//) on fields and variants are also visible in the hover — use them to describe the purpose of each field without needing a full doc comment.

The /** */ block is useful when the description is long enough to warrant formatting across multiple paragraphs.

See also

enespt-br