Pular para o conteúdo

1. Propriedade, movimentos e empréstimos verificados

Um valor proprietário tem uma responsabilidade de limpeza. Movê-lo transfere essa responsabilidade e torna o lugar anterior indisponível até nova inicialização. Cópias existem apenas quando a semântica permite cópias independentes.

Um empréstimo verificado &T ou &const T não assume propriedade. Seu tempo de vida mantém o proprietário vivo e impede acessos conflitantes. Uma slice verificada acrescenta comprimento, mantendo índices verificados. Mantenha empréstimos curtos, retorne dados próprios quando precisarem sobreviver à origem e deixe os diagnósticos exporem propriedade ambígua.

Estude referências verificadas, slices verificadas e o modelo de memória.

Exemplo testado

Acompanhe um valor por reempréstimo mutável, handle movido e handles compartilhados somente leitura.

Arquivo: curriculum/main-evidence/single/checked_references.fl

curriculum/main-evidence/single/checked_references.fl
// Build: build/flecha build examples/checked_references.fl -o build/checked_references
// Prints 6 and exits with 6.
void double_value(&u8 value) {
*value = *value * 2;
}
u8 read_value(&const u8 value) {
return *value;
}
i32 main() {
u8 value = 3;
ref := &value;
double_value(ref); // A call reborrows; ref remains usable.
stdoutln(read_value(ref));
// The last use of ref has passed, so value is accessible directly.
u8 other = 10;
moving := &other;
moved := moving; // moving cannot be read until reassigned.
*moved = 11;
moving = &other;
*moving = 12;
constref u8 fixed = &other; // const &u8: fixed binding, writable target.
*fixed = 13;
&const u8 shared = &other;
copy := shared; // Shared handles may be copied.
if *copy != *shared { return 1; }
*u8 pointer = none;
slot := &pointer; // &*u8: a checked reference to a raw-pointer variable.
*slot = none; // Safe access to the pointer slot; no pointee access.
return value as i32;
}

O bloco acima é o arquivo completo executado pelos comandos abaixo.

Cada binding representa uma operação distinta de propriedade ou empréstimo. A sequência mostra quando a mutação é permitida, quando um handle é movido e quando apenas leituras compartilhadas continuam válidas.

Comandos

Comandos
flecha check curriculum/main-evidence/single/checked_references.fl
flecha build curriculum/main-evidence/single/checked_references.fl -o build/checked_references
build/checked_references

Saída esperada

Saída esperada
6
status de saída: 6

Referência relacionada: /examples/checked_references/.