Formattable

public protocol Formattable

Protocol for types that can render themselves as a String under a FormatOptions.

Print routines and string interpolation "\{expr}" and "\{expr:spec}" both ultimately bottom out in format. Implementors should honour every FormatOptions field that is meaningful for their domain (alignment and width are universal; radix only applies to integers, floatStyle only to floats) and silently ignore fields that aren't.

Examples

"\{name}"; // "Alice" (default formatting) "\{name:>10}"; // " Alice" (right-align, width 10) "\{n:08x}"; // "0000002a" (zero-pad, hex, width 8) "\{pi:.2}"; // "3.14" (precision 2) "\{value:?}"; // debug representation

Methods

func format(into: mutating StringBuilder, FormatOptions)

Writes this value's formatted representation directly into writer.

This is the kernel method — all formatting ultimately bottoms out here. The convenience format(options:) -> String in the protocol extension calls this under the hood.

public func formatted(FormatOptions) -> String

Returns this value rendered as a String.

Convenience wrapper: creates a StringBuilder, calls format(into:), and returns the built string. Uses a distinct name to avoid overload-resolution ambiguity with format(into:).

Defined in lang/std/text/format.ks