Files
bux-lang/examples/lifetime_elision.bux
T
dimgigov 53b43b0f79 feat: lifetime elision, tooling CI, registry, and LSP locals
Ship the QUALITY_PLAN stretch from ownership through ecosystem: C.1
lifetime elision (bootstrap + selfhost), bux fmt/test/doc CI hooks,
stdlib goldens, package registry (bux search/add), and LSP 0.4
position-sensitive locals with inferred let types. Full-tree format
pass plus Map/Set remove double-free fix.
2026-07-19 16:35:08 +03:00

52 lines
1.2 KiB
Plaintext

// lifetime_elision.bux — C.1: elided lifetimes for common &[Checked] APIs
// No 'a annotations needed when there is a single input reference.
import Std::Io::{PrintLine, PrintInt};
import Std::Test::{Test_AssertEqInt, Test_Pass};
// Elided: param and return share one lifetime automatically
@[Checked]
func Identity(p: &int) -> &int {
return p;
}
// Explicit lifetime for documentation / multi-ref (same lifetime both sides)
@[Checked]
func IdentityNamed<'a>(p: &'a int) -> &'a int {
return p;
}
// Via intermediate let binding — lifetime is propagated
@[Checked]
func ViaLet(p: &int) -> &int {
let r: &int = p;
return r;
}
// self-style first param: elision prefers the first input for the return
@[Checked]
func FirstOf(self: &int, _other: int) -> &int {
return self;
}
@[Checked]
func Main() -> int {
var x: int = 10;
var y: int = 20;
let a: &int = Identity(&x);
Test_AssertEqInt(*a, 10);
let b: &int = IdentityNamed(&y);
Test_AssertEqInt(*b, 20);
let c: &int = ViaLet(&x);
Test_AssertEqInt(*c, 10);
let d: &int = FirstOf(&y, 0);
Test_AssertEqInt(*d, 20);
Test_Pass("lifetime_elision");
PrintLine("lifetime_elision: ok");
return 0;
}