// SPDX-License-Identifier: MIT import .helpers; namespace demo; // A local alias for an imported type. Source spelling remains available. type Token = helpers::Token; // A generic envelope with an imported token. // // ## Fields // - `value` keeps its generic type parameter. // - `token` uses [Token](item:helpers::Token) from another module. struct Envelope { // The wrapped value. value: T, // Provenance supplied by the caller. token: Token, } // A result with optional payload, expressed as a Goose enum. enum Outcome { // Nothing was produced. Empty, // The successful payload. Value { // The result value. data: T, // Identifies its origin. token: Token, }, } // Return a value unchanged. Works with [Envelope](item:demo::Envelope). // // **Example** with a real imported declaration: // ```goose,run // import .helpers; // fn identity(value: T) -> T { value } // fn main() { // let token = identity(helpers::Token { code: 7 }); // assert(token.code == 7); // } // ``` // // Raw HTML stays text: . fn identity(value: T) -> T { value } // Accept and return the imported token through a local alias. fn pass_token(token: Token) -> Token { token } // Integer overload. Adds one. fn score(value: i64) -> i64 { value + 1 } // Floating overload. Adds one half. fn score(value: f64) -> f64 { value + 0.5 } // A defaulted argument; the LSP deliberately renders its value as `...`. fn with_default(value: i64, extra: i64 = 2) -> i64 { value + extra } // An untyped generic parameter and an unspecified source result annotation. fn echo(value) { value } // A declared documentation bound, retained without inventing extra constraints. fn apply(value: i64) { F(value); } // Native export metadata is distinct from package visibility. export fn exported_plus(value: i64) -> i64 { value + 1 } // An inferred global type, as reported by the compiler. let answer = 42; // A mutable global binding. var counter: i64 = 0; // Executes representative generic, imported, default and overloaded paths. fn ::main() { let envelope = demo::Envelope { value: demo::identity(7), token: demo::Token { code: 42 } }; assert(envelope.value == 7); assert(demo::score(2) == 3); assert(demo::score(2.0) == 2.5); assert(demo::with_default(5) == 7); assert(helpers::twice(demo::echo(3)) == 6); fn local_only(value: i64) -> i64 { value } assert(local_only(1) == 1); }