Doc Comments
Nesta página
Doc comments são comentários especiais que o editor exibe ao passar o mouse
sobre um símbolo. Zolo suporta dois estilos: /// para linhas individuais e
/** */ para blocos longos.
/// é o estilo preferido para funções curtas e membros de tipos. Cada linha
prefixada com /// é concatenada e renderizada como markdown no hover:
Doc comments /// em funções, bloco /** */ em is_prime; anotações em campos de struct, variantes de enum e métodos de trait.
// ============================================================
// 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 funcionam em qualquer declaração de nível superior: funções
livres, structs, enums, traits e seus respectivos membros. Os comentários
internos (//) em campos e variantes também ficam visíveis no hover — use-os
para descrever o propósito de cada campo sem precisar de um doc comment
completo.
O bloco /** */ é útil quando a descrição é longa o suficiente para merecer
formatação em múltiplos parágrafos.
Veja também