Extensions
extend adds methods to a type after its declaration, or adds an explicit trait implementation for
an existing type. Use extensions to keep a type’s core definition separate from optional behavior.
Adding Methods
Section titled “Adding Methods”Extension members follow ordinary member visibility. Write public on methods that other modules
should call.
extend does not currently support init or deinit.
Adding Trait Implementations
Section titled “Adding Trait Implementations”extend Type: Trait { ... } adds a trait implementation:
A type does not satisfy a trait just because it happens to have methods with matching signatures.
The implementation must be declared on the type or in extend Type: Trait.
Generic Extensions
Section titled “Generic Extensions”Extensions can be generic:
Use @where to make an extension available only for selected type arguments:
Type-Shape Matching
Section titled “Type-Shape Matching”@where can match concrete type shapes. _ is an anonymous placeholder, accepting any type
argument in that position:
Use : for trait bounds. Use == or != for concrete type-shape matching:
The extension target itself can also use anonymous placeholders:
Extensions target surface forms directly, including generic patterns such as extend <T> T& and
extend <T> T[]^.
Module Visibility
Section titled “Module Visibility”Extensions can be used across module boundaries, but they are not globally active. A file must import the module that defines the extension before extension members participate in lookup.
Use public extend when an extension should be available to modules that import it. An extension
without public is only visible inside the module that defines it.
Jiang does not impose a global orphan rule on extensions: a module may extend a public external or builtin type. Lookup only considers extensions visible from the use site. A concrete target is more specific than a generic target; if multiple callable extensions with the same specificity match, the call is ambiguous rather than being selected by declaration order.
public import can re-export public extensions from another module: