-
Notifications
You must be signed in to change notification settings - Fork 1k
Simplified lookup of property values #940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e76d89a
3cd352d
909beeb
138b3f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -574,6 +574,9 @@ With two arguments, \var{id} and \var{key}, \var{lookup} returns the | |
value of \var{id}'s \var{key} property, or \scheme{#f} if \var{id} | ||
has no \var{key} property. | ||
|
||
However, in the case of properties, there is a second, usually easier | ||
way, to obtain property values, namely by calling the | ||
\scheme{property-value} procedure described below. | ||
|
||
%---------------------------------------------------------------------------- | ||
\entryheader | ||
|
@@ -709,6 +712,29 @@ Attaching a new property with the same name as an property already | |
attached to a binding shadows the existing property with the new | ||
property. | ||
|
||
%---------------------------------------------------------------------------- | ||
\entryheader | ||
\formdef{property-value}{\categoryprocedure}{(property-value \var{identifier} \var{key-identifier})} | ||
\formdef{property-value}{\categoryprocedure}{(property-value \var{identifier} \var{key-identifier} \var{default})} | ||
\returns see below | ||
\listlibraries | ||
\endentryheader | ||
|
||
The \scheme{property-value} procedure returns the value of the | ||
\var{key-identifier} property of \var{identifier}. | ||
Comment on lines
+723
to
+724
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The \scheme{property-value} procedure is used in macro transformers to look up the value of the |
||
If \var{identifier} has no \var{key-identifier} property, \var{default} | ||
is returned. | ||
If omitted, \var{default} defaults to \scheme{#f}. | ||
As the right-hand side of a \scheme{define-property} form is evaluated | ||
at expand-time, note that invoking \scheme{property-value} at relative | ||
run-time within the same library or top-level program won't see the | ||
property. | ||
This is the reason why the \scheme{get-property} macro wrapper around | ||
\scheme{property-value} is used below. | ||
Comment on lines
+728
to
+733
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this section. I tried to include the essence of the content in the revision above. |
||
|
||
If \var{identifier} or \var{key-identifier} have no visible binding, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. have => has |
||
an exception with condition type \scheme{&undefined} is raised. | ||
|
||
The following example defines a macro, \scheme{get-info}, that retrieves | ||
the \scheme{info} property of a binding, defines the variable \scheme{x}, | ||
attaches an \scheme{info} property to the binding of \scheme{x}, retrieves | ||
|
@@ -721,11 +747,10 @@ properties are shadowed as well as the outer binding of \scheme{x}. | |
(define info) | ||
(define-syntax get-info | ||
(lambda (x) | ||
(lambda (lookup) | ||
(syntax-case x () | ||
[(_ q) | ||
(let ([info-value (lookup #'q #'info)]) | ||
#`'#,(datum->syntax #'* info-value))])))) | ||
(syntax-case x () | ||
[(_ q) | ||
(let ([info-value (property-value #'q #'info)]) | ||
#`'#,(datum->syntax #'* info-value))]))) | ||
(define x "x-value") | ||
(define-property x info "x-info") | ||
(get-info x) ;=> "x-info" | ||
|
@@ -741,10 +766,9 @@ just that. | |
\schemedisplay | ||
(define-syntax get-property | ||
(lambda (x) | ||
(lambda (r) | ||
(syntax-case x () | ||
[(_ id key) | ||
#`'#,(datum->syntax #'* (r #'id #'key))])))) | ||
(syntax-case x () | ||
[(_ id key) | ||
#`'#,(datum->syntax #'* (property-value #'id #'key))]))) | ||
Comment on lines
+769
to
+771
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the original code, because it demonstrates how to use the lookup procedure. Add a note:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is related to my comment above; if we agree that new code probably wants to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine with me to have get-info use the lookup procedure and get-property use property-value. I want to have an example of each way. |
||
(get-property x info) ;=> "x-info" | ||
\endschemedisplay | ||
|
||
|
@@ -810,11 +834,10 @@ cannot be accessed or forged. | |
(do-drt #'rname #'(fname ...) #f)] | ||
[(_ rname pname (fname ...)) | ||
(for-all identifier? #'(rname pname fname ...)) | ||
(lambda (lookup) | ||
(let ([prtd (lookup #'pname #'drt-key)]) | ||
(unless prtd | ||
(syntax-error #'pname "unrecognized parent record type")) | ||
(do-drt #'rname #'(fname ...) prtd)))])))) | ||
(let ([prtd (property-value #'pname #'drt-key)]) | ||
(unless prtd | ||
(syntax-error #'pname "unrecognized parent record type")) | ||
(do-drt #'rname #'(fname ...) prtd)))]))) | ||
\endschemedisplay | ||
|
||
\schemedisplay | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For compile-time properties, the \scheme{property-value} procedure can be used instead of the lookup procedure in macro transformers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best wording probably depends on what the "recommended" way to retrieve property values should be. With
property-value
being available, I see no reason to use the older way for new code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid making a recommendation and simply present the options.