~ chicken-core (chicken-5) /manual/Module (chicken locative)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken locative)56A ''locative'' is an object that points to an element of a containing object,7much like a ''pointer'' in low-level, imperative programming languages like ''C''. The element can8be accessed and changed indirectly, by performing access or change operations9on the locative. The container object can be computed by calling the10{{locative->object}} procedure.1112Locatives may be passed to foreign procedures that expect pointer arguments.1314The following procedures are provided by the {{(chicken locative)}}15module.1617=== make-locative1819<procedure>(make-locative OBJ [INDEX])</procedure>2021Creates a locative that refers to the element of the non-immediate object22{{OBJ}} at position {{INDEX}}. {{OBJ}} may be a vector, pair, string, blob,23SRFI-4 number-vector, or record structure. {{INDEX}} should be a fixnum.24{{INDEX}} defaults to 0.252627=== make-weak-locative2829<procedure>(make-weak-locative OBJ [INDEX])</procedure>3031Creates a ''weak'' locative. Even though the locative refers to an element of a container object,32the container object will still be reclaimed by garbage collection if no other references33to it exist.343536=== locative?3738<procedure>(locative? X)</procedure>3940Returns {{#t}} if {{X}} is a locative, or {{#f}} otherwise.414243=== locative-ref4445<procedure>(locative-ref LOC)</procedure>4647Returns the element to which the locative {{LOC}} refers. If the containing48object has been reclaimed by garbage collection, an error is signalled.4950 (locative-ref (make-locative "abc" 1)) ==> #\b5152=== locative-set!5354<procedure>(locative-set! LOC X)</procedure><br>55<procedure>(set! (locative-ref LOC) X)</procedure>5657Changes the element to which the locative {{LOC}} refers to {{X}}.58If the containing59object has been reclaimed by garbage collection, an error is signalled.606162=== locative->object6364<procedure>(locative->object LOC)</procedure>6566Returns the object that contains the element referred to by {{LOC}} or67{{#f}} if the container has been reclaimed by garbage collection.6869 (locative->object (make-locative "abc" 1)) ==> "abc"7071---72Previous: [[Module (chicken load)]]7374Next: [[Module (chicken memory)]]