Names and visibility¶
A Cloth identifier starts with an XID_Start character or _, followed
by zero or more XID_Continue characters. A lone _ is the discard or
placeholder token. Cloth uses the Unicode 18.0.0 UAX #31 profile R1-2 and
compares identifiers case-sensitively in NFC (R4), with no normalization
exclusions. For example, both Unicode spellings of café resolve to the
same declaration. Diagnostics retain the original source spelling.
See Unicode Standard Annex #31.
By default, top-level declarations are private to their file and members
are private to their declaring type, even when the type is public.
Use pub on an individual declaration or a group of declarations.
A using declaration imports public names from another file. The dotted
module path selects a .co or .clib file. The compiler searches the
entry file’s directory first, then the configured standard library.
Inside a function or method, parameters and local bindings take precedence
over members and file-level names.
Public declaration blocks¶
At file scope or inside a class or interface, write pub { ... } to
make each direct declaration public. You can mix blocks with individual
modifiers in the same file.
fn<int> next() = 0;
pub {
fn<int> next(int value) = value + 1;
typedef<class> Counter {
def<int> value = 0;
pub {
Counter(int initial) : () { self.value = initial; }
mt<int> read() = self.value;
}
}
}
pub fn<int> answer(Counter counter) = next(counter.read());
A caller can import Counter, answer, and the one-argument
overload of next. The zero-argument overload is private to the file.
The member block exposes the constructor and read. A caller can
write answer(Counter::new(41)) to get 42. The field value is
private to the class. See the
module and interface example.
Declarations keep their order and belong to the enclosing file or type.
Each child takes its usual terminator. The block ends at its closing
brace. Empty and nested public blocks are valid, and a child can also
carry its own pub. Put every using import before the first
file-level declaration or public block, including an empty block.
Importing a module¶
Put imports before declarations. Select one or more names after the
colon, or use * to bring in all the file’s public declarations.
using cloth.io.path.Path: Path, pathOf;
using cloth.math.Math: *;
fn<Path> manifest() = pathOf("module.k");
fn<float<64>> circleConstant() = TAU();
The module cloth.io.path.Path maps to cloth/io/path/Path.co under
the standard library root. If both .co and .clib match the same
module in the selected directory, the import is ambiguous. Listing a name
twice in one import reports C2010. An importing file’s own imports do
not re-export those names.
Standard library prelude¶
When a build selects a standard library, cloth/lang/Prelude.co supplies
default names to every source file. Its header imports select public names
from other modules. Public declarations in the prelude itself are also visible.
Local declarations and explicit imports take precedence over prelude names.
With a library that omits the prelude, a file can still select names with
ordinary using imports.
The shipped prelude selects Number, so a bound such as T: Number
works throughout the program. To use console I/O, import the functions in
each file that calls them, for example using cloth.Console : println;.
See the prelude declaration.
Member visibility¶
Use a dotted name such as Outer.Inner for a visible nested type. A
public declaration must also expose types that its callers can name.
A public function with a private result type reports C3117.
A positional struct literal initializes fields in declaration order. Each field must be accessible where the literal appears. A struct’s own method can initialize its private fields. A free function in the same file can use a positional literal only if all the fields are public.
Mark a state declaration or attachment pub so callers outside the class
can name that state in a test or transition. For initial states, payloads,
and the visibility of a separate state definition, see Named states.
A class method that implements a public interface requirement must itself
be public. If the interface declares the method without a body,
the class can supply the first body with either pub mt or
pub override mt. A class method that replaces an inherited interface
body introduced by = default must use override.
A derived class can satisfy an interface it adds with a matching public
method inherited from its base.