Allocating-objects.xhtml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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>Allocating objects</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="Method-operations.xhtml" title="Calling Java methods from Scheme"/>
  10. <link rel="next" href="Field-operations.xhtml" title="Accessing object fields"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Allocating objects" epub:type="subchapter" id="Allocating-objects">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Allocating objects</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The recommended way to create an instance of a type <em class="replaceable"><code>T</code></em>
  23. is to “call” <em class="replaceable"><code>T</code></em> as if it were a function, with the
  24. arguments used to initialize the object.
  25. If <code class="literal">T</code> is a class and <code class="literal">T</code> has a matching constructor,
  26. then the arguments will used for constructor arguments:
  27. </p>
  28. <pre class="screen">(java.util.StringTokenizer "this/is/a/test" "/")
  29. </pre>
  30. <p>(You can think of the type <em class="replaceable"><code>T</code></em> as being
  31. coerced to an instance-constructor function.)
  32. </p>
  33. <p>If <code class="literal">T</code> is a container or collection type,
  34. then typically the arguments will be used to specify
  35. the child or component values.
  36. Many standard Scheme procedures fit this convention.
  37. For example in Kawa <code class="literal">list</code> and <code class="literal">vector</code> evaluate to
  38. types, rather than procedures as in standard Scheme,
  39. but because types can be used as constructor functions it just works:
  40. </p>
  41. <pre class="screen">(list 'a (+ 3 4) 'c) ⇒ (a 7 c)
  42. (vector 'a 'b 'c) ⇒ #(a b c)
  43. </pre>
  44. <p>Any class <code class="literal">T</code> that has a default constructor
  45. and an <code class="literal">add</code> method can be initialized this way.
  46. Examples are <code class="literal">java.util</code> collection classes,
  47. and <code class="literal">jawa.awt</code> and <code class="literal">javax.swing</code> containers.
  48. </p>
  49. <pre class="screen">(java.util.ArrayList 11 22 33) ⇒ [11, 22, 333]
  50. </pre>
  51. <p>The above expression is equivalent to:
  52. </p>
  53. <pre class="screen">(let ((tmp (java.util.ArrayList)))
  54. (tmp:add 11)
  55. (tmp:add 22)
  56. (tmp:add 33)
  57. tmp)
  58. </pre>
  59. <p>Allocating Java arrays (see <a class="link" href="Array-operations.xhtml#Creating-new-Java-arrays">Creating-new-Java-arrays</a>) uses a
  60. similar pattern:
  61. </p>
  62. <pre class="screen">(int[] 2 3 5 7 11)
  63. </pre>
  64. <p>Sometimes you want to set some named property to an initial value.
  65. You can do that using a keyword argument. For example:
  66. </p>
  67. <pre class="screen">(javax.swing.JButton text: "Do it!" tool-tip-text: "do it")
  68. </pre>
  69. <p>This is equivalent to using <em class="firstterm">setter methods</em>:
  70. </p>
  71. <pre class="screen">(let ((tmp (javax.swing.JButton)))
  72. (tmp:setText "Do it!")
  73. (tmp:setToolTipText "do it")
  74. tmp)
  75. </pre>
  76. <p>A keyword argument <code class="literal"><em class="replaceable"><code>key-name</code></em></code><code class="literal"><span class="bold"><strong>:</strong></span></code> can
  77. can translated to either a <code class="literal"><code class="literal"><span class="bold"><strong>set</strong></span></code><em class="replaceable"><code>KeyName</code></em><code class="literal"><span class="bold"><strong>:</strong></span></code></code>
  78. or a <code class="literal"><code class="literal"><span class="bold"><strong>add</strong></span></code><em class="replaceable"><code>KeyName</code></em><code class="literal"><span class="bold"><strong>:</strong></span></code></code> method.
  79. The latter makes it convenient to add listeners:
  80. </p>
  81. <pre class="screen">(javax.swing.JButton
  82. text: "Do it!"
  83. action-listener:
  84. (object (java.awt.event.ActionListener)
  85. ((actionPerformed e) (do-the-action))))
  86. </pre>
  87. <p>This is equivalent to:
  88. </p>
  89. <pre class="screen">(let ((tmp (javax.swing.JButton)))
  90. (tmp:setText "Do it!")
  91. (tmp:addActionListener
  92. (object (java.awt.event.ActionListener)
  93. ((actionPerformed e) (do-the-action))))
  94. tmp)
  95. </pre>
  96. <p>Making use of so-called “SAM-conversion” (see <a class="link" href="Anonymous-classes.xhtml#SAM-conversion">SAM-conversion</a>)
  97. makes it even more convenient:
  98. </p>
  99. <pre class="screen">(javax.swing.JButton
  100. text: "Do it!"
  101. action-listener:
  102. (lambda (e) (do-the-action)))
  103. </pre>
  104. <p>The general case allows for a mix of
  105. constructor arguments, property keywords, and child values:
  106. </p>
  107. <div class="literallayout">
  108. <p><em class="replaceable"><code>class-type</code></em> <a class="link" href="Allocating-objects.xhtml#meta-constructor-value"><em class="replaceable"><code>constructor-value</code></em></a>... <a class="link" href="Allocating-objects.xhtml#meta-property-initializer"><em class="replaceable"><code>property-initializer</code></em></a>... <a class="link" href="Allocating-objects.xhtml#meta-child-value"><em class="replaceable"><code>child-value</code></em></a>...<br/>
  109. <a id="idm139667870938720" class="indexterm"/><span id="meta-constructor-value"/><em class="replaceable"><code>constructor-value</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  110. <a id="idm139667870935920" class="indexterm"/><span id="meta-property-initializer"/><em class="replaceable"><code>property-initializer</code></em> <code class="literal">::=</code> <a class="link" href="Keywords.xhtml#meta-keyword"><em class="replaceable"><code>keyword</code></em></a> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  111. <a id="idm139667870932288" class="indexterm"/><span id="meta-child-value"/><em class="replaceable"><code>child-value</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  112. </p>
  113. </div>
  114. <p>First an object is constructed with the <em class="replaceable"><code>constructor-value</code></em> arguments
  115. (if any) passed to the object constructor;
  116. then named properties (if any) are used to initialize named properties;
  117. and then remaining arguments are used to add child values.
  118. </p>
  119. <p>There is an ambiguity if there is no <em class="replaceable"><code>property-initializer</code></em> -
  120. we can’t distinguish between a <em class="replaceable"><code>constructor-value</code></em>
  121. and a <em class="replaceable"><code>child-value</code></em>.
  122. In that case, if there is a matching constructor method, then all of the
  123. arguments are constructor arguments;
  124. otherwise, there must a default constructor, and all
  125. of the arguments are <em class="replaceable"><code>child-value</code></em> arguments.
  126. </p>
  127. <p>There is a trick you can you if you need both
  128. <em class="replaceable"><code>constructor-value</code></em> and <em class="replaceable"><code>child-value</code></em> arguments:
  129. separate them with an “empty keyword” <code class="literal">||:</code>.
  130. This matches a method named <code class="literal">add</code>, which means that
  131. the next argument effectively a <em class="replaceable"><code>child-value</code></em> - as do
  132. all the remaining arguments. Example:
  133. </p>
  134. <pre class="screen">(let ((vec #(1 2 3)))
  135. (java.util.ArrayList vec ||: 4 5 6))
  136. ⇒ [1, 2, 3, 4, 5, 6]
  137. </pre>
  138. <p>The compiler rewrites these allocations expression
  139. to generated efficient bytecode, assuming that the “function”
  140. being applied is a type known by the compiler.
  141. Most of the above expressions also work if the type is applied
  142. at run-time, in which case Kawa has to use slower reflection:
  143. </p>
  144. <pre class="screen">(define iarr int[])
  145. (apply iarr (list 3 4 5)) ⇒ [3 4 5]
  146. </pre>
  147. <p>However <code class="literal">add<em class="replaceable"><code>Xxx</code></em></code> methods and SAM-conversion
  148. are currently only recognized in the case of a class known at compile-time,
  149. not at run-time.
  150. </p>
  151. <p>Here is a working Swing demo illustrating many of these techniques:
  152. </p>
  153. <pre class="screen">(import (class javax.swing
  154. JButton Box JFrame))
  155. (define-simple-class HBox (Box)
  156. ((*init*) (invoke-special Box (this) '*init* 0)))
  157. (define value 0)
  158. (define txt
  159. (javax.swing.JLabel
  160. text: "0"))
  161. (define (set-value i)
  162. (set! value i)
  163. (set! txt:text (number-&gt;string i)))
  164. (define fr
  165. (JFrame
  166. title: "Hello!"
  167. (Box 1#|VERTICAL|# ||:
  168. (javax.swing.Box:createGlue)
  169. txt
  170. (javax.swing.Box:createGlue)
  171. (HBox
  172. (JButton ;; uses 1-argument constructor
  173. "Decrement" ;; constructor argument
  174. tool-tip-text: "decrement"
  175. action-listener: (lambda (e) (set-value (- value 1))))
  176. (javax.swing.Box:createGlue)
  177. (JButton ;; uses 0-argument constructor
  178. text: "Increment"
  179. tool-tip-text: "increment"
  180. action-listener: (lambda (e) (set-value (+ value 1))))))))
  181. (fr:setSize 200 100)
  182. (set! fr:visible #t)
  183. </pre>
  184. <p>If you prefer, you can use the older <code class="literal">make</code> special function:
  185. </p>
  186. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667870916928" class="indexterm"/> <code class="function">make</code> <em class="replaceable"><code>type</code></em> <em class="replaceable"><code>args</code></em> <em class="replaceable"><code>...</code></em></p>
  187. <div class="blockquote">
  188. <blockquote class="blockquote">
  189. <p>Constructs a new object instance of the specified <em class="replaceable"><code>type</code></em>,
  190. which must be either a <code class="literal">java.lang.Class</code> or a
  191. <code class="literal">&lt;gnu.bytecode.ClassType&gt;</code>.
  192. Equivalent to:
  193. </p>
  194. <pre class="screen"><em class="replaceable"><code>type</code></em> <em class="replaceable"><code>args</code></em> ...
  195. </pre>
  196. </blockquote>
  197. </div>
  198. <p>Another (semi-deprecated) function is to use the colon notation
  199. with the <code class="literal">new</code> pseudo-function.
  200. The following three are all equivalent:
  201. </p>
  202. <pre class="screen">(java.awt.Point:new x: 4 y: 3)
  203. (make java.awt.Point: x: 4 y: 3)
  204. (java.awt.Point x: 4 y: 3)
  205. </pre>
  206. </section>
  207. <footer>
  208. <div class="navfooter">
  209. <p>
  210. Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  211. <p>
  212. Previous: <a accesskey="p" href="Method-operations.xhtml">Calling Java methods from Scheme</a></p>
  213. <p>
  214. Next: <a accesskey="n" href="Field-operations.xhtml">Accessing object fields</a></p>
  215. </div>
  216. </footer>
  217. </body>
  218. </html>