Saltar al contenido

Doc Comments

En esta página

Los doc comments son comentarios especiales que el editor muestra al pasar el cursor sobre un símbolo. Zolo admite dos estilos: /// para líneas individuales y /** */ para bloques largos.

/// es el estilo preferido para funciones cortas y miembros de tipos. Cada línea prefijada con /// se concatena y se renderiza como markdown en el hover:

Doc comments /// en funciones, bloque /** */ en is_prime; anotaciones en campos de struct, variantes de enum y métodos de trait.

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()}")

Los doc comments funcionan en cualquier declaración de nivel superior: funciones libres, structs, enums, traits y sus respectivos miembros. Los comentarios internos (//) en campos y variantes también son visibles en el hover — úsalos para describir el propósito de cada campo sin necesitar un doc comment completo.

El bloque /** */ es útil cuando la descripción es lo suficientemente larga como para merecer formato en múltiples párrafos.

Consulta también

Buscar en Zolo

9 resultados

enespt-br