// Build with: build/flecha build examples/structures.fl -o /tmp/structures
// The program returns 42.
Box Box::init(i32 x, i32 y) override {
private i32 Box::hidden(&const self) { return self->y; }
i32 Box::value(&const self) { return self->x + self->hidden(); }
void Box::setY(&self, i32 value) { self->y = value; }
unsafe i32 Box::rawValue(*const self) { return self->x + self->y; }
Box box = Box::init(2, 40); // Explicit arguments for this constructor.
// An unsafe tear forbids copying and moving this type. The raw target below
// remains alive until cleanup; unsafe code must uphold that requirement.
unsafe void Counter::tear(&self) override {
if self->total != null { *self->total = *self->total + 1; }
unsafe Counter make_counter(*i32 total) {
return build Counter { self.total = total; };
NamedBox named = NamedBox::init();
NamedBox copy = named; // Independently owns a copy of the string field.
if copy.name != "answer" { return 1; }
*Box ptr = &raw copy.box;
if ptr->rawValue() != 42 { return 2; }
Counter counter = make_counter(&raw cleanups);
// Fresh factory return constructs directly in counter's storage.
// tear runs automatically at this scope's end.
if cleanups != 1 { return 3; }