Próximo: , Anterior: , Acima: Estilo de código   [Conteúdo][Índice]


22.6.3 Tipos de dados e correspondência de padrão

A tendência no Lisp clássico é usar listas para representar tudo e, em seguida, pesquisá-las “à mão” usando car, cdr, cadr e co. Existem vários problemas com esse estilo, notadamente o fato de que é difícil de ler, propenso a erros e um obstáculo para os relatórios de erros de tipos adequados.

Guix code should define appropriate data types (for instance, using define-record-type*) rather than abuse lists. In addition, it should use pattern matching, via Guile’s (ice-9 match) module, especially when matching lists (veja Pattern Matching em GNU Guile Reference Manual); pattern matching for records is better done using match-record from (guix records), which, unlike match, verifies field names at macro-expansion time.

When defining a new record type, keep the record type descriptor (RTD) private (veja Records em GNU Guile Reference Manual, for more on records and RTDs). As an example, the (guix packages) module defines <package> as the RTD for package records but it does not export it; instead, it exports a type predicate, a constructor, and field accessors. Exporting RTDs would make it harder to change the application binary interface (because code in other modules might be matching fields by position) and would make it trivial for users to forge records of that type, bypassing any checks we may have in the official constructor (such as “field sanitizers”).