Field-operations.xhtml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Accessing object fields</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Mangling.xhtml" title="Mapping Scheme names to Java names"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Accessing object fields" epub:type="subchapter" id="Field-operations">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Accessing object fields</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <section class="sect2" title="Accessing static fields and properties" epub:type="division" id="idm139667870907808">
  23. <div class="titlepage">
  24. <div>
  25. <div>
  26. <h3 class="title">Accessing static fields and properties</h3>
  27. </div>
  28. </div>
  29. </div>
  30. <p>The recommmended way to access fields
  31. uses the <a class="link" href="Colon-notation.xhtml" title="Property access using colon notation">colon notation</a>.
  32. For static fields and properties the following is recommended:
  33. </p>
  34. <div class="literallayout">
  35. <p><em class="replaceable"><code>class-expression</code></em><code class="literal"><span class="bold"><strong>:</strong></span></code><em class="replaceable"><code>field-name</code></em><br/>
  36. </p>
  37. </div>
  38. <p>For example:
  39. </p>
  40. <pre class="screen">java.lang.Integer:MAX_VALUE
  41. </pre>
  42. <p>A property with a <code class="literal">get</code> method is equivalent to a field.
  43. The following are all equivalent:
  44. </p>
  45. <pre class="screen">java.util.Currency:available-currencies
  46. java.util.Currency:availableCurrencies
  47. (java.util.Currency:getAvailableCurrencies)
  48. </pre>
  49. <p>Just like for a method call, the <em class="replaceable"><code>class-expression</code></em>
  50. can be a class in the current lexical scope,
  51. a fully-qualified class name, or more generally an
  52. expression that evaluates to a class.
  53. </p>
  54. </section>
  55. <section class="sect2" title="Accessing instance fields and properties" epub:type="division" id="idm139667870901152">
  56. <div class="titlepage">
  57. <div>
  58. <div>
  59. <h3 class="title">Accessing instance fields and properties</h3>
  60. </div>
  61. </div>
  62. </div>
  63. <p>The syntax is:
  64. </p>
  65. <div class="literallayout">
  66. <p><em class="replaceable"><code>instance</code></em><code class="literal"><span class="bold"><strong>:</strong></span></code><em class="replaceable"><code>field-name</code></em><br/>
  67. </p>
  68. </div>
  69. <p>The <em class="replaceable"><code>field-name</code></em> can of course be the name of an actual
  70. object field, but it can also be the name of a property with
  71. a zero-argument <code class="literal">get</code> method.
  72. For example, if <code class="literal">cal</code> is a <code class="literal">java.util-Calendar</code> instance,
  73. then the following are all equivalent:
  74. </p>
  75. <pre class="screen">cal:time-zone
  76. cal:timeZone
  77. (cal:getTimeZone)
  78. (cal:get-time-zone)
  79. </pre>
  80. <p>You can use colon notation to assign to a field:
  81. </p>
  82. <pre class="screen">(set! cal:time-zone TimeZone:default)
  83. </pre>
  84. <p>which is equivalent to:
  85. </p>
  86. <pre class="screen">(cal:setTimeZone (TimeZone:getDefault))
  87. </pre>
  88. <p>A Java array only has the <code class="literal">length</code> field, plus the <code class="literal">class</code> property:
  89. </p>
  90. <pre class="screen">(int[] 4 5 6):length ⇒ 3
  91. (int[] 4 5 6):class:name ⇒ "int[]"
  92. </pre>
  93. </section>
  94. <section class="sect2" title="Using field and static-field methods" epub:type="division" id="idm139667870892384">
  95. <div class="titlepage">
  96. <div>
  97. <div>
  98. <h3 class="title">Using field and static-field methods</h3>
  99. </div>
  100. </div>
  101. </div>
  102. <p>The following methods are useful in cases where colon notation
  103. is ambiguous, for example where there are both fields and methods
  104. with the same name.
  105. You might also prefer as a matter of style, to
  106. emphasise that a field is being accessed.
  107. </p>
  108. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870890656" class="indexterm"/> <code class="function">field</code> <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>fieldname</code></em></p>
  109. <div class="blockquote">
  110. <blockquote class="blockquote">
  111. <p>Get the instance field with the given <em class="replaceable"><code>fieldname</code></em> from the given
  112. <em class="replaceable"><code>Object</code></em>. Returns the value of the field, which must be accessible.
  113. This procedure has a <code class="literal">setter</code>, and so can be used as the first
  114. operand to <code class="literal">set!</code>.
  115. </p>
  116. <p>The field name is "mangled" (see <a class="link" href="Mangling.xhtml" title="Mapping Scheme names to Java names">Mangling</a>) into a valid Java name.
  117. If there is no accessible field whose name is <code class="literal">"<em class="replaceable"><code>fieldname</code></em>"</code>,
  118. we look for a no-argument method whose name is
  119. <code class="literal">"get<em class="replaceable"><code>Fieldname</code></em>"</code> (or <code class="literal">"is<em class="replaceable"><code>Fieldname</code></em>"</code> for a
  120. boolean property).
  121. </p>
  122. <p>If <em class="replaceable"><code>object</code></em> is a primitive Java array, then <em class="replaceable"><code>fieldname</code></em> can only
  123. be <code class="literal">'length</code>, and the result is the number of elements of the array.
  124. </p>
  125. </blockquote>
  126. </div>
  127. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870879248" class="indexterm"/> <code class="function">static-field</code> <em class="replaceable"><code>class</code></em> <em class="replaceable"><code>fieldname</code></em></p>
  128. <div class="blockquote">
  129. <blockquote class="blockquote">
  130. <p>Get the static field with the given <em class="replaceable"><code>fieldname</code></em> from the given
  131. <em class="replaceable"><code>class</code></em>. Returns the value of the field, which must be accessible.
  132. This procedure has a <code class="literal">setter</code>, and so can be used as the first
  133. operand to <code class="literal">set!</code>.
  134. </p>
  135. <p>If the <em class="replaceable"><code>fieldname</code></em> is the special name <code class="literal">class</code>,
  136. then it returns the <code class="literal">java.lang.Class</code> object corresponding to
  137. <em class="replaceable"><code>class</code></em> (which is usually a <code class="literal">gnu.bytecode.ClassType</code> object).
  138. </p>
  139. </blockquote>
  140. </div>
  141. <p>Examples:
  142. </p>
  143. <pre class="screen">(static-field java.lang.System 'err)
  144. ;; Copy the car field of b into a.
  145. (set! (field a 'car) (field b 'car))
  146. </pre>
  147. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870870112" class="indexterm"/> <code class="function">slot-ref</code> <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>fieldname</code></em></p>
  148. <div class="blockquote">
  149. <blockquote class="blockquote">
  150. <p>A synonym for <code class="literal">(field <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>fieldname</code></em>)</code>.
  151. </p>
  152. </blockquote>
  153. </div>
  154. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870865072" class="indexterm"/> <code class="function">slot-set!</code> <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>fieldname</code></em> <em class="replaceable"><code>value</code></em></p>
  155. <div class="blockquote">
  156. <blockquote class="blockquote">
  157. <p>A synonym for <code class="literal">(set! (field <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>fieldname</code></em>) <em class="replaceable"><code>value</code></em>)</code>.
  158. </p>
  159. </blockquote>
  160. </div>
  161. </section>
  162. <section class="sect2" title="Older colon-dot notation" epub:type="division" id="idm139667870859200">
  163. <div class="titlepage">
  164. <div>
  165. <div>
  166. <h3 class="title">Older colon-dot notation</h3>
  167. </div>
  168. </div>
  169. </div>
  170. <p>There is older syntax where following the colon
  171. there is field name a following the colon <span class="emphasis"><em>and</em></span> a period.
  172. </p>
  173. <p>To access an static field named <em class="replaceable"><code>field-name</code></em> use this syntax
  174. </p>
  175. <pre class="screen">(<em class="replaceable"><code>prefix</code></em>:.<em class="replaceable"><code>field-name</code></em> <em class="replaceable"><code>instance</code></em>)
  176. </pre>
  177. <p>The <em class="replaceable"><code>prefix</code></em> can be as discussed in See <a class="link" href="Method-operations.xhtml" title="Calling Java methods from Scheme">Method operations</a>.
  178. Here are 5 equivalent ways:
  179. </p>
  180. <pre class="screen">(java.lang.Integer:.MAX_VALUE)
  181. (&lt;java.lang.Integer&gt;:.MAX_VALUE)
  182. (define-namespace Int32 &lt;java.lang.Integer&gt;)
  183. (Int32:.MAX_VALUE)
  184. (define-namespace Integer "class:java.lang.Integer")
  185. (Integer:.MAX_VALUE)
  186. (define-alias j.l.Integer java.lang.Integer)
  187. (j.l.Integer:.MAX_VALUE)
  188. </pre>
  189. <p>You can set a static field using this syntax:
  190. </p>
  191. <pre class="screen">(set! (<em class="replaceable"><code>prefix</code></em>:.<em class="replaceable"><code>field-name</code></em>) <em class="replaceable"><code>new-value</code></em>)
  192. </pre>
  193. <p>The special field name <code class="literal">class</code> can be used to extract the
  194. <code class="literal">java.lang.Class</code> object for a class-type. For example:
  195. </p>
  196. <pre class="screen">(java.util.Vector:.class) ⇒ class java.util.Vector
  197. </pre>
  198. <p>To access a instance field named <em class="replaceable"><code>field-name</code></em> use the following syntax.
  199. Note the period before the <em class="replaceable"><code>field-name</code></em>.
  200. </p>
  201. <pre class="screen">(*:.<em class="replaceable"><code>field-name</code></em> <em class="replaceable"><code>instance</code></em>)
  202. </pre>
  203. <p>This syntax works with <code class="literal">set!</code> - to set the field use this syntax:
  204. </p>
  205. <pre class="screen">(set! (*:.<em class="replaceable"><code>field-name</code></em> <em class="replaceable"><code>instance</code></em>) <em class="replaceable"><code>new-value</code></em>)
  206. </pre>
  207. <p>Here is an example:
  208. </p>
  209. <pre class="screen">(define p (list 3 4 5))
  210. (*:.cdr p) ⇒ (4 5)
  211. (set! (*:.cdr p) (list 6 7))
  212. p ⇒ (3 6 7)
  213. </pre>
  214. <p>You can specify an explicit class:
  215. </p>
  216. <pre class="screen">(<em class="replaceable"><code>prefix</code></em>:.<em class="replaceable"><code>field-name</code></em> <em class="replaceable"><code>instance</code></em>)
  217. </pre>
  218. <p>If <em class="replaceable"><code>prefix</code></em> is bound to <code class="literal">&lt;<em class="replaceable"><code>class</code></em>&gt;</code>, then the above
  219. is equivalent to:
  220. </p>
  221. <pre class="screen">(*:.<em class="replaceable"><code>field-name</code></em> (as &lt;<em class="replaceable"><code>class</code></em>&gt; <em class="replaceable"><code>instance</code></em>))
  222. </pre>
  223. </section>
  224. </section>
  225. <footer>
  226. <div class="navfooter">
  227. <ul>
  228. <li>
  229. <b class="toc">
  230. <a href="Field-operations.xhtml#idm139667870907808">Accessing static fields and properties</a>
  231. </b>
  232. </li>
  233. <li>
  234. <b class="toc">
  235. <a href="Field-operations.xhtml#idm139667870901152">Accessing instance fields and properties</a>
  236. </b>
  237. </li>
  238. <li>
  239. <b class="toc">
  240. <a href="Field-operations.xhtml#idm139667870892384">Using field and static-field methods</a>
  241. </b>
  242. </li>
  243. <li>
  244. <b class="toc">
  245. <a href="Field-operations.xhtml#idm139667870859200">Older colon-dot notation</a>
  246. </b>
  247. </li>
  248. </ul>
  249. <p>
  250. Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  251. <p>
  252. Previous: <a accesskey="p" href="Allocating-objects.xhtml">Allocating objects</a></p>
  253. <p>
  254. Next: <a accesskey="n" href="Mangling.xhtml">Mapping Scheme names to Java names</a></p>
  255. </div>
  256. </footer>
  257. </body>
  258. </html>