[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A better way to access records.
From: |
Taylan Kammer |
Subject: |
Re: A better way to access records. |
Date: |
Fri, 30 Oct 2020 19:17:37 +0100 |
User-agent: |
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.1 |
On 30.10.2020 11:28, Brendan Tildesley wrote:
In the guix codebase, on many occasions there appear things like this:
(match-lambda
(($ <agetty-configuration> agetty tty term baud-rate auto-login
login-program login-pause? eight-bits? no-reset? remote? flow-control?
host no-issue? init-string no-clear? local-line extract-baud?
skip-login? no-newline? login-options chroot hangup? keep-baud? timeout
detect-case? wait-cr? no-hints? no-hostname? long-hostname?
erase-characters kill-characters chdir delay nice extra-options)
(list
.... >
Wouldn't be nice if we could just step inside a record type whenever we
pleased?
The above would be like this perhaps:
(let-from-record-type <agetty-configuration>
(list ...))
Usually in Scheme the concept of "lexical scope" is held in very high
regard, which means that for every identifier that is being referenced
in a piece of code, you should be able to see its verbatim definition or
binding (with 'define' or 'let') in the text, with the exception of
imports of course.
This has various advantages, like being intuitive, making name clashes
unlikely, making it easy for IDEs and other tools to find the definition
of a binding, and so on.
Of course, the disadvantage is the verbosity.
Digression: This is also the crux of the debate on whether it's a good
idea for a record definition syntax to implicitly bind procedures. For
instance would it be a blessing or a curse if I could just say
(define-record-type <rec> (make-rec foo bar) rec?)
and automatically have rec-foo, set-rec-foo!, rec-bar and set-rec-bar!
defined for me, even though none of those identifiers appear in the
definition of the record type... End digression.
As seen in your example, a record may have tons of fields. Binding them
all automatically would IMO be quite bad in some cases. In the list we
see very generic identifiers like 'term', 'host', 'timeout', 'chdir' and
'delay'. Binding these implicitly would be Very Bad(TM) because you
might have been using them for something else and happen to forget that
this record type contains them and as such the 'let-from-record-type'
overrides your bindings.
Worse yet: when the record gets more fields, your code might break
because one of the new fields happens to be an identifier that you were
using in your code!
Consider the following. Let's say the <agetty-configuration> does not
yet have a field called 'chdir' and nobody has any idea that one day it
will be added. I write the following code:
(let-from-record my-agetty-config
(let ((orig-dir (get-working-dir))
(tmpdir (make-tmp-dir))
(chdir tmpdir)
(do-something-with-agetty-config)
(chdir orig-dir)))
One day, 'chdir' is added to the agetty-configuration record type...
Well I assume you see the problem. :-)
In code where the bindings to be taken from the record are listed
explicitly, such a problem cannot occur.
- Taylan