You can associate arbitrary properties with any procedure.
Each property is a (key
, value
)-pair. Usually the
key
is a symbol, but it can be any object.
The preferred way to set a property is using an option-pair
in a lambda-expression
.
For example,
to set the setter
property of a procedure
to my-set-car
do the following:
(define my-car (lambda (arg) setter: my-set-car (primitive-car arg)))
The system uses certain internal properties:
'name
refers to the name used when a procedure is printed;
'emacs-interactive
is used to implement Emacs interactive
specification;
'setter
is used to associate a setter
procedure.
Procedure: procedure-property
proc
key
[default
]
Get the property value corresponding to the given
key
. Ifproc
has no property with the givenkey
, returndefault
(which defaults to#f
) instead.
Procedure: set-procedure-property!
proc
key
value
Associate the given
value
with thekey
property ofproc
.
To change the print name of the standard +
procedure (probably
not a good idea!), you could do:
(set-procedure-property! + 'name 'PLUS)
Note this only changes the name property used for printing:
+ ⇒ #<procedure PLUS> (+ 2 3) ⇒ 5 (PLUS 3 4) ⇒ ERROR
As a matter of style, it is cleaner to use the define-procedure
form, as it is a more declarative interface.
Syntax: define-procedure
name
[propname:
propvalue
] ...
method
...
Defines
name
as a compound procedure consisting of the specifiedmethod
s, with the associated properties. Applyingname
select the "best"method
, and applies that. See the following section on generic procedures.For example, the standard
vector-ref
procedure specifies one method, as well as thesetter
property:(define-procedure vector-ref setter: vector-set! (lambda (vector::vector k ::int) (invoke vector 'get k)))
You can also specify properties in the lambda body:
(define (vector-ref vector::vector k ::int) setter: vector-set! (invoke vector 'get k))
name
The name of a procedure (as a symbol), which is used when the procedure is printed.
setter
Set the setter procedure associated with the procedure.
validate-apply
validate-xapply
Used during the validation phase of the compiler.
compile-apply
Used during the bytecode-generation phase of the compiler: If we see a call to a known function with this property, we can emit custom bytecode for the call.