Module-classes.xhtml 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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>Modules and how they are compiled to classes</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="Importing.xhtml" title="Importing from a library"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Modules and how they are compiled to classes" epub:type="subchapter" id="Module-classes">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Modules and how they are compiled to classes</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Modules provide a way to organize Scheme into
  23. reusable parts with explicitly defined interfaces to the rest
  24. of the program.
  25. A <em class="firstterm">module</em> is a set of definitions that the module <em class="firstterm">exports</em>,
  26. as well as some <em class="firstterm">actions</em> (expressions evaluated for their side effect).
  27. The top-level forms in a Scheme source file compile a module;
  28. the source file is the <em class="firstterm">module source</em>.
  29. When Kawa compiles the module source, the result is the
  30. <em class="firstterm">module class</em>. Each exported definition is translated to
  31. a public field in the module class.
  32. </p>
  33. <section class="sect2" title="Name visibility" epub:type="division" id="idm139667871606752">
  34. <div class="titlepage">
  35. <div>
  36. <div>
  37. <h3 class="title">Name visibility</h3>
  38. </div>
  39. </div>
  40. </div>
  41. <p>The definitions that a module exports are accessible to other modules.
  42. These are the "public" definitions, to use Java terminology.
  43. By default, all the identifiers declared at the top-level of a module
  44. are exported, except those defined using <code class="literal">define-private</code>.
  45. (If compiling with the <code class="literal">--main</code> flag,
  46. then by default no identifiers are exported.)
  47. However, a major purpose of using modules is to control the set of
  48. names exported. One reason is to reduce the chance of accidental
  49. name conflicts between separately developed modules. An even more
  50. important reason is to enforce an interface: Client modules should
  51. only use the names that are part of a documented interface, and should
  52. not use internal implementation procedures (since those may change).
  53. </p>
  54. <p>If there is a <code class="literal">module-export</code> (or <code class="literal">export</code>)
  55. declaration in the module, then only those names listed are exported.
  56. There can be more than one <code class="literal">module-export</code>, and they can be
  57. anywhere in the Scheme file. The recommended style has
  58. a single <code class="literal">module-export</code> near the beginning of the file.
  59. </p>
  60. <span id="meta-export-declaration"/>
  61. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871600048" class="indexterm"/> <code class="function">module-export</code> <em class="replaceable"><code><a class="link" href="Module-classes.xhtml#meta-export-spec"><em class="replaceable"><code>export-spec</code></em></a></code></em><em class="replaceable"><code><sup>*</sup></code></em></p>
  62. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871596304" class="indexterm"/> <code class="function">export</code> <em class="replaceable"><code><a class="link" href="Module-classes.xhtml#meta-export-spec"><em class="replaceable"><code>export-spec</code></em></a></code></em><em class="replaceable"><code><sup>*</sup></code></em></p>
  63. <div class="blockquote">
  64. <blockquote class="blockquote">
  65. <p>The forms <code class="literal">export</code> and <code class="literal">module-export</code> are equivalent.
  66. (The older Kawa name is <code class="literal">module-export</code>;
  67. <code class="literal">export</code> comes from R7RS.)
  68. Either form specifies a list of identifiers which
  69. can be made visible to other libraries or programs.
  70. </p>
  71. <div class="literallayout">
  72. <p><a id="idm139667871590240" class="indexterm"/><span id="meta-export-spec"/><em class="replaceable"><code>export-spec</code></em> <code class="literal">::=</code> <em class="replaceable"><code>identifier</code></em><br/>
  73.   | <code class="literal"><span class="bold"><strong>(rename</strong></span></code> <a class="link" href="Lexical-syntax.xhtml#meta-identifier"><em class="replaceable"><code>identifier</code></em></a><sub>1</sub> <a class="link" href="Lexical-syntax.xhtml#meta-identifier"><em class="replaceable"><code>identifier</code></em></a><sub>2</sub><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  74. </p>
  75. </div>
  76. <p>In the former variant, an <em class="replaceable"><code>identifier</code></em> names a single binding
  77. defined within or imported into the library, where the
  78. external name for the export is the same as the name of
  79. the binding within the library.
  80. A <code class="literal">rename</code> spec exports the
  81. binding defined within or imported into the library and
  82. named by <em class="replaceable"><code>identifier</code></em><sub>1</sub>,
  83. using <em class="replaceable"><code>identifier</code></em><sub>2</sub> as the external name.
  84. </p>
  85. <p>Note that it is an error if there is no definition for <em class="replaceable"><code>identifier</code></em>
  86. (or <em class="replaceable"><code>identifier</code></em><sub>1</sub>)
  87. in the current module, or if it is defined using <code class="literal">define-private</code>.
  88. </p>
  89. <p>As a matter of style, <code class="literal">export</code> or <code class="literal">module-export</code> should
  90. appear after <code class="literal">module-name</code> but <span class="emphasis"><em>before</em></span> other commands
  91. (including <code class="literal">import</code> or <code class="literal">require</code>).
  92. (This is a requirement if there are any cycles.)
  93. </p>
  94. </blockquote>
  95. </div>
  96. <p>In this module, <code class="literal">fact</code> is public and <code class="literal">worker</code> is private:
  97. </p>
  98. <pre class="screen">(module-export fact)
  99. (define (worker x) ...)
  100. (define (fact x) ...)
  101. </pre>
  102. <p>Alternatively, you can write:
  103. </p>
  104. <pre class="screen">(define-private (worker x) ...)
  105. (define (fact x) ...)
  106. </pre>
  107. </section>
  108. <section class="sect2" title="R7RS explicit library modules" epub:type="division" id="idm139667871573376">
  109. <div class="titlepage">
  110. <div>
  111. <div>
  112. <h3 class="title">R7RS explicit library modules</h3>
  113. </div>
  114. </div>
  115. </div>
  116. <p>A R7RS <code class="literal">define-library</code> form is another way to create a module.
  117. The R7RS term <em class="firstterm">library</em> is roughly the same as a Kawa module.
  118. In Kawa, each source file is a <a class="link" href="Bodies.xhtml#implicit-library"><em class="firstterm">implicit module</em></a>,
  119. which may contain zero or more explicit sub-modules (in
  120. the form of <code class="literal">define-library</code>) optionally followed by
  121. the definitions and expressions of the implicit (file-level) module.
  122. </p>
  123. <span id="meta-library-definition"/>
  124. <a id="idm139667871569200" class="indexterm"/>
  125. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871568160" class="indexterm"/> <code class="function">define-library</code> <em class="replaceable"><code><a class="link" href="Module-classes.xhtml#meta-library-name"><em class="replaceable"><code>library-name</code></em></a></code></em> <em class="replaceable"><code><a class="link" href="Module-classes.xhtml#meta-library-declaration"><em class="replaceable"><code>library-declaration</code></em></a></code></em><em class="replaceable"><code><sup>*</sup></code></em></p>
  126. <div class="literallayout">
  127. <p><a id="idm139667871563456" class="indexterm"/><span id="meta-library-name"/><em class="replaceable"><code>library-name</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code> <a class="link" href="Module-classes.xhtml#meta-library-name-parts"><em class="replaceable"><code>library-name-parts</code></em></a> <code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  128. <a id="idm139667871559088" class="indexterm"/><span id="meta-library-name-parts"/><em class="replaceable"><code>library-name-parts</code></em> <code class="literal">::=</code> <em class="replaceable"><code>identifier</code></em><sup>+</sup><br/>
  129. </p>
  130. </div>
  131. <p>A <em class="replaceable"><code>library-name</code></em> is a list whose members are identifiers and
  132. exact non-negative integers. It is used to identify the library
  133. uniquely when importing from other programs or
  134. libraries. Libraries whose first identifier is <code class="literal">scheme</code> are
  135. reserved for use by the R7RS report and future versions of that
  136. report. Libraries whose first identifier is <code class="literal">srfi</code> are reserved
  137. for libraries implementing <a class="ulink" href="http://srfi.schemer.org/" target="_top">Scheme Requests for Implementation</a>.
  138. It is inadvisable, but not an error, for identifiers
  139. in library names to contain any of the characters <code class="literal">|</code> <code class="literal">\</code> <code class="literal">?</code>
  140. <code class="literal">*</code> <code class="literal">&lt;</code> <code class="literal">"</code> <code class="literal">:</code> <code class="literal">&gt;</code> <code class="literal">+</code> <code class="literal">[</code> <code class="literal">]</code>
  141. <code class="literal">/</code> <code class="literal">.</code> or control characters after escapes are
  142. expanded.
  143. </p>
  144. <p>See <a class="link" href="Module-classes.xhtml#module-name">module-name</a> for how a <em class="replaceable"><code>library-name</code></em> is
  145. mapped to a class name.
  146. </p>
  147. <div class="literallayout">
  148. <p><a id="idm139667871546528" class="indexterm"/><span id="meta-library-declaration"/><em class="replaceable"><code>library-declaration</code></em> <code class="literal">::=</code><br/>
  149.   <a class="link" href="Module-classes.xhtml#meta-export-declaration"><em class="replaceable"><code>export-declaration</code></em></a><br/>
  150.   | <a class="link" href="Importing.xhtml#meta-import-declaration"><em class="replaceable"><code>import-declaration</code></em></a><br/>
  151.   | <code class="literal"><span class="bold"><strong>(begin</strong></span></code> <a class="link" href="Bodies.xhtml#meta-statement"><em class="replaceable"><code>statement</code></em></a><sup>*</sup> <code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  152.   | <code class="literal"><span class="bold"><strong>(include</strong></span></code> <em class="replaceable"><code>filename</code></em><sup>+</sup><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  153.   | <code class="literal"><span class="bold"><strong>(include-ci</strong></span></code> <em class="replaceable"><code>filename</code></em><sup>+</sup><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  154.   | <code class="literal"><span class="bold"><strong>(include-library-declarations</strong></span></code> <em class="replaceable"><code>filename</code></em><sup>+</sup><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  155.   | <code class="literal"><span class="bold"><strong>(cond-expand</strong></span></code> <a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-cond-expand-clause"><em class="replaceable"><code>cond-expand-clause</code></em></a><sup>*</sup> [<code class="literal"><span class="bold"><strong>(else</strong></span></code> command-or-definition*<code class="literal"><span class="bold"><strong>)</strong></span></code>]<code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  156.   | <a class="link" href="Bodies.xhtml#meta-statement"><em class="replaceable"><code>statement</code></em></a><br/>
  157. </p>
  158. </div>
  159. <p>The <code class="literal">begin</code>, <code class="literal">include</code>, and <code class="literal">include-ci</code> declarations are
  160. used to specify the body of the library. They have the
  161. same syntax and semantics as the corresponding expression types.
  162. This form of <code class="literal">begin</code> is analogous to, but not the
  163. same as regular <code class="literal">begin</code>.
  164. A plain <em class="replaceable"><code>statement</code></em> (which is allowed as a Kawa extension)
  165. is also part of the body of the library,
  166. as if it were wrapped in a <code class="literal">begin</code>).
  167. </p>
  168. <p>The <code class="literal">include-library-declarations</code> declaration is similar
  169. to <code class="literal">include</code> except that the contents of the file are
  170. spliced directly into the current library definition. This
  171. can be used, for example, to share the same <code class="literal">export</code> declaration
  172. among multiple libraries as a simple form of library interface.
  173. </p>
  174. <p>The <code class="literal">cond-expand</code> declaration has the same syntax and semantics
  175. as the <code class="literal">cond-expand</code> expression type, except that
  176. it expands to spliced-in library declarations rather than
  177. expressions enclosed in <code class="literal">begin</code>.
  178. </p>
  179. <p>When a library is loaded, its expressions are executed in
  180. textual order. If a library’s definitions are referenced in
  181. the expanded form of a program or library body, then that
  182. library must be loaded before the expanded program or
  183. library body is evaluated. This rule applies transitively. If
  184. a library is imported by more than one program or library,
  185. it may possibly be loaded additional times.
  186. </p>
  187. <p>Similarly, during the expansion of a library <code class="literal">(foo)</code>, if any
  188. syntax keywords imported from another library <code class="literal">(bar)</code> are
  189. needed to expand the library, then the library <code class="literal">(bar)</code> must
  190. be expanded and its syntax definitions evaluated before the
  191. expansion of <code class="literal">(foo)</code>.
  192. </p>
  193. <p>Regardless of the number of times that a library is loaded,
  194. each program or library that imports bindings from a library must
  195. do so from a single loading of that library, regardless
  196. of the number of import declarations in which it
  197. appears. That is, <code class="literal">(import (only (foo) a</code>)) followed by
  198. <code class="literal">(import (only (foo) b))</code> has the same effect as
  199. <code class="literal">(import (only (foo) a b))</code>.
  200. </p>
  201. </section>
  202. <section class="sect2" title="How a module becomes a class" epub:type="division" id="idm139667871515088">
  203. <div class="titlepage">
  204. <div>
  205. <div>
  206. <h3 class="title">How a module becomes a class</h3>
  207. </div>
  208. </div>
  209. </div>
  210. <p>If you want to just use a Scheme module as a module (i.e. <code class="literal">load</code>
  211. or <code class="literal">require</code> it), you don’t care how it gets translated
  212. into a module class. However, Kawa gives you some control over how this
  213. is done, and you can use a Scheme module to define a class which
  214. you can use with other Java classes. This style of class definition
  215. is an alternative to <code class="literal">define-class</code>,
  216. which lets you define classes and instances fairly conveniently.
  217. </p>
  218. <p>The default name of the module class is the main part of the
  219. filename of the Scheme source file (with directories and extensions
  220. stripped off). That can be overridden by the <code class="literal">-T</code> Kawa
  221. command-line flag. The package-prefix specified by the <code class="literal">-P</code>
  222. flag is prepended to give the fully-qualified class name.
  223. </p>
  224. <span id="module-name"/>
  225. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871509680" class="indexterm"/> <code class="function">module-name</code> <em class="replaceable"><code>name</code></em></p>
  226. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871506848" class="indexterm"/> <code class="function">module-name</code> <em class="replaceable"><code>&lt;name&gt;</code></em></p>
  227. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871504016" class="indexterm"/> <code class="function">module-name</code> <em class="replaceable"><code><a class="link" href="Module-classes.xhtml#meta-library-name"><em class="replaceable"><code>library-name</code></em></a></code></em></p>
  228. <div class="blockquote">
  229. <blockquote class="blockquote">
  230. <p>Sets the name of the generated class, overriding the default.
  231. If there is no ‘<code class="literal">.</code>’ in the <em class="replaceable"><code>name</code></em>, the package-prefix
  232. (specified by the <code class="literal">-P</code> Kawa command-line flag) is prepended.
  233. </p>
  234. <p>If the form <em class="replaceable"><code>library-name</code></em> is used,
  235. then the class name is the result of taking
  236. each <em class="replaceable"><code>identifier</code></em> in the <em class="replaceable"><code>library-name-parts</code></em>,
  237. <a class="link" href="Mangling.xhtml" title="Mapping Scheme names to Java names">mangling</a> if needed, and concatenating them
  238. separated by periods.
  239. For example <code class="literal">(org example doc-utils)</code> becomes
  240. <code class="literal">org.example.doc-utils</code>. (You can’t reference the class name
  241. <code class="literal">doc-utils</code> directly in Java, but the JVM has no problems with it.
  242. In Java you can use reflection to access classes with such names.)
  243. </p>
  244. <p>As a matter of style, <code class="literal">module-name</code> should be the first
  245. command in a file (after possible comments). It must appear
  246. before a <code class="literal">require</code> or <code class="literal">import</code>, in case of cycles.
  247. </p>
  248. </blockquote>
  249. </div>
  250. <p>By default, the base class of the generated module class is unspecified;
  251. you cannot count on it being more specific than <code class="literal">Object</code>.
  252. However, you can override it with <code class="literal">module-extends</code>.
  253. </p>
  254. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871491312" class="indexterm"/> <code class="function">module-extends</code> <em class="replaceable"><code>class</code></em></p>
  255. <div class="blockquote">
  256. <blockquote class="blockquote">
  257. <p>Specifies that the class generated from the immediately surrounding
  258. module should extend (be a sub-class of) the class <code class="literal"><em class="replaceable"><code>class</code></em></code>.
  259. </p>
  260. </blockquote>
  261. </div>
  262. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871487296" class="indexterm"/> <code class="function">module-implements</code> <em class="replaceable"><code>interface</code></em> <em class="replaceable"><code>...</code></em></p>
  263. <div class="blockquote">
  264. <blockquote class="blockquote">
  265. <p>Specifies that the class generated from the immediately surrounding
  266. module should implement the interfaces listed.
  267. </p>
  268. </blockquote>
  269. </div>
  270. <p>Note that the compiler does <span class="emphasis"><em>not</em></span> currently check that all the
  271. abstract methods requires by the base class or implemented interfaces
  272. are actually provided, and have the correct signatures. This will
  273. hopefully be fixed, but for now, if you are forgot a method, you will
  274. probably get a verifier error
  275. </p>
  276. <p>For each top-level exported definition the compiler creates a
  277. corresponding public field with a similar (mangled) name.
  278. By default, there is some indirection: The value of the Scheme variable
  279. is not that of the field itself. Instead, the field is a
  280. <code class="literal">gnu.mapping.Location</code> object, and the value Scheme variable is
  281. defined to be the value stored in the <code class="literal">Location</code>.
  282. Howewer, if you specify an explicit type, then the field will
  283. have the specified type, instead of being a <code class="literal">Location</code>.
  284. The indirection using <code class="literal">Location</code> is also avoided if you use
  285. <code class="literal">define-constant</code>.
  286. </p>
  287. <p>If the Scheme definition defines a procedure (which is not re-assigned
  288. in the module), then the compiler assumes the variable as bound as a
  289. constant procedure. The compiler generates one or more methods
  290. corresponding to the body of the Scheme procedure. It also generates
  291. a public field with the same name; the value of the field is an
  292. instance of a subclass of <code class="literal">&lt;gnu.mapping.Procedure&gt;</code> which when
  293. applied will execute the correct method (depending on the actual arguments).
  294. The field is used when the procedure used as a value (such as being passed
  295. as an argument to <code class="literal">map</code>), but when the compiler is able to do so,
  296. it will generate code to call the correct method directly.
  297. </p>
  298. <p>You can control the signature of the generated method by declaring
  299. the parameter types and the return type of the method. See the
  300. applet (see <a class="link" href="Compiling.xhtml#Applet-compilation" title="Compiling to an applet">Applet compilation</a>) example for how this can be done.
  301. If the procedures has optional parameters, then the compiler will
  302. generate multiple methods, one for each argument list length.
  303. (In rare cases the default expression may be such that this is
  304. not possible, in which case an "variable argument list" method
  305. is generated instead. This only happens when there is a nested
  306. scope <span class="emphasis"><em>inside</em></span> the default expression, which is very contrived.)
  307. If there are <code class="literal">#!keyword</code> or <code class="literal">#!rest</code> arguments, the compiler
  308. generate a "variable argument list" method. This is a method whose
  309. last parameter is either an array or a <code class="literal">&lt;list&gt;</code>, and whose
  310. name has <code class="literal">$V</code> appended to indicate the last parameter is a list.
  311. </p>
  312. <p>Top-leval macros (defined using either <code class="literal">define-syntax</code>
  313. or <code class="literal">defmacro</code>) create a field whose type is currently a sub-class of
  314. <code class="literal">kawa.lang.Syntax</code>; this allows importing modules to detect
  315. that the field is a macro and apply the macro at compile time.
  316. </p>
  317. <p>Unfortunately, the Java class verifier does not allow fields to have
  318. arbitrary names. Therefore, the name of a field that represents a
  319. Scheme variable is "mangled" (see <a class="link" href="Mangling.xhtml" title="Mapping Scheme names to Java names">Mangling</a>) into an acceptable Java name.
  320. The implementation can recover the original name of a field <code class="literal">X</code>
  321. as <code class="literal">((gnu.mapping.Named) X).getName()</code> because all the standard
  322. compiler-generated field types implement the <code class="literal">Named</code> interface.
  323. </p>
  324. <span id="dual-purpose-class"/>
  325. </section>
  326. <section class="sect2" title="Same class for module and defined class" epub:type="division" id="idm139667871467792">
  327. <div class="titlepage">
  328. <div>
  329. <div>
  330. <h3 class="title">Same class for module and defined class</h3>
  331. </div>
  332. </div>
  333. </div>
  334. <p>You can declare a class using <code class="literal">define-simple-class</code>
  335. with the same name as the module class, for example the
  336. following in a file named <code class="literal">foo.scm</code>:
  337. </p>
  338. <pre class="screen">(define-simple-class foo ...)
  339. </pre>
  340. <p>In this case the defined class will serve dual-purpose as the module class.
  341. </p>
  342. <p>To avoid confusion, in this case you must not specify
  343. <code class="literal">module-extends</code>, <code class="literal">module-implements</code>, or <code class="literal">(module-static #t)</code>.
  344. Also, the defined class should not have public static members.
  345. In that case it works out pretty well: public static members
  346. represent bindings exported by the module; other non-private members
  347. “belong” to the defined class.
  348. </p>
  349. <p>In this case <code class="literal">(module-static 'init-run)</code> is implied.
  350. </p>
  351. <span id="static-or-non-modules"/>
  352. </section>
  353. <section class="sect2" title="Static vs non-static modules" epub:type="division" id="idm139667871461264">
  354. <div class="titlepage">
  355. <div>
  356. <div>
  357. <h3 class="title">Static vs non-static modules</h3>
  358. </div>
  359. </div>
  360. </div>
  361. <p>There are two kinds of module class:
  362. A <em class="firstterm">static module</em> is a class (or gets compiled to a class)
  363. all of whose public fields are static, and that does not have a
  364. public constructor. A JVM can only have a single global instance of
  365. a static module.
  366. An <em class="firstterm">instance module</em> has a public default constructor,
  367. and usually has at least one non-static public field.
  368. There can be multiple instances
  369. of an instance module; each instance is called a <em class="firstterm">module instance</em>.
  370. However, only a single instance of a module can be <em class="firstterm">registered</em>
  371. in an environment, so in most cases there is only a single
  372. instance of instance modules. Registering an instance in an environment
  373. means creating a binding mapping a magic name (derived from the class name)
  374. to the instance.
  375. </p>
  376. <p>In fact, any Java class class that has the properties of either
  377. an instance module or a static module, is a module, and can be
  378. loaded or imported as such; the class need not have written
  379. using Scheme.
  380. </p>
  381. <p>You can control whether a module is compiled to a static or
  382. a non-static class using either a command-line flag to the compiler,
  383. or using the <code class="literal">module-static</code> special form.
  384. </p>
  385. <div class="variablelist" epub:type="list">
  386. <dl class="variablelist">
  387. <dt class="term"><code class="literal">--module-static</code>
  388. </dt>
  389. <dd>
  390. <p>Generate a static module
  391. (as if <code class="literal">(module-static #t)</code> were specified).
  392. This is (now) the default.
  393. </p>
  394. </dd>
  395. <dt class="term"><code class="literal">--module-nonstatic</code>
  396. </dt>
  397. <dt class="term"><code class="literal">--no-module-static</code>
  398. </dt>
  399. <dd>
  400. <p>Generate a non-static module
  401. (as if <code class="literal">(module-static #f)</code> were specified).
  402. This used to be the default.
  403. </p>
  404. </dd>
  405. <dt class="term"><code class="literal">--module-static-run</code>
  406. </dt>
  407. <dd>
  408. <p>Generate a static module
  409. (as if <code class="literal">(module-static 'init-run)</code> were specified).
  410. </p>
  411. </dd>
  412. </dl>
  413. </div>
  414. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871450240" class="indexterm"/> <code class="function">module-static</code> <em class="replaceable"><code>name</code></em> <em class="replaceable"><code>...</code></em></p>
  415. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871447024" class="indexterm"/> <code class="function">module-static</code> <em class="replaceable"><code>#t</code></em></p>
  416. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871444224" class="indexterm"/> <code class="function">module-static</code> <em class="replaceable"><code>#f</code></em></p>
  417. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871441424" class="indexterm"/> <code class="function">module-static</code> <em class="replaceable"><code>'init-run</code></em></p>
  418. <div class="blockquote">
  419. <blockquote class="blockquote">
  420. <p>Control whether the generated fields and methods are static.
  421. If <code class="literal">#t</code> or <code class="literal">'init-run</code> is specified, then the module will be a
  422. static module, <span class="emphasis"><em>all</em></span> definitions will be static.
  423. If <code class="literal">'init-run</code> is specified, in addition the module body
  424. is evaluated in the class’s static initializer.
  425. (Otherwise, it is run the first time it is <code class="literal">require</code>’d.)
  426. Otherwise, the module is an instance module. If there is a non-empty
  427. list of <em class="replaceable"><code>name</code></em>s then the module is an instance module, but the <em class="replaceable"><code>name</code></em>s
  428. that are explicitly listed will be compiled to static fields and methods.
  429. If <code class="literal">#f</code> is specified, then all exported names will
  430. be compiled to non-static (instance) fields and methods.
  431. </p>
  432. <p>By default, if no <code class="literal">module-static</code> is specified:
  433. </p>
  434. <div class="orderedlist" epub:type="list">
  435. <ol class="orderedlist" type="1">
  436. <li class="listitem" epub:type="list-item">
  437. <p>If there is a <code class="literal">module-extends</code> or <code class="literal">module-implements</code>
  438. declaration, or one of the <code class="literal">--applet</code> or <code class="literal">--servlet</code>
  439. command-line flags was specified, then <code class="literal">(module-static #f)</code> is implied.
  440. </p>
  441. </li>
  442. <li class="listitem" epub:type="list-item">
  443. <p>If one of the command-line flags
  444. <code class="literal">--no-module-static</code>, <code class="literal">--module-nonstatic</code>,
  445. <code class="literal">--module-static</code>, or <code class="literal">--module-static-run</code> was specified,
  446. then the default is <code class="literal">#f</code>, <code class="literal">#f</code>, <code class="literal">#t</code>, or <code class="literal">'init-run</code>,
  447. respectively.
  448. </p>
  449. </li>
  450. <li class="listitem" epub:type="list-item">
  451. <p>If the module class is <a class="link" href="Module-classes.xhtml#dual-purpose-class">dual-purpose</a>
  452. then <code class="literal">(module-static 'init-run)</code> is implied.
  453. </p>
  454. </li>
  455. <li class="listitem" epub:type="list-item">
  456. <p>Otherwise the default is <code class="literal">(module-static #t)</code>.
  457. (It used to be <code class="literal">(module-static #f)</code> in older Kawa versions.)
  458. </p>
  459. </li>
  460. </ol>
  461. </div>
  462. <p>The default is <code class="literal">(module-static #t)</code>. It usually produces more efficient
  463. code, and is recommended if a module contains only procedure or macro
  464. definitions. However, a static module means that all environments in a JVM
  465. share the same bindings, which you may not want if you use
  466. multiple top-level environments.
  467. </p>
  468. </blockquote>
  469. </div>
  470. <p>The top-level actions of a module will get compiled to a <code class="literal">run</code>
  471. method. If there is an explicit <code class="literal">method-extends</code>, then the
  472. module class will also automatically implement <code class="literal">java.lang.Runnable</code>.
  473. (Otherwise, the class does not implement <code class="literal">Runnable</code>, since in that
  474. case the <code class="literal">run</code> method return an <code class="literal">Object</code> rather than <code class="literal">void</code>.
  475. This will likely change.)
  476. </p>
  477. </section>
  478. <section class="sect2" title="Module options" epub:type="division" id="idm139667871418480">
  479. <div class="titlepage">
  480. <div>
  481. <div>
  482. <h3 class="title">Module options</h3>
  483. </div>
  484. </div>
  485. </div>
  486. <p>Certain compilation options can be be specified <span class="emphasis"><em>either</em></span>
  487. on the command-line when compiling, or in the module itself.
  488. </p>
  489. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871416464" class="indexterm"/> <code class="function">module-compile-options</code> [<em class="replaceable"><code>key</code></em><em class="replaceable"><code><span class="bold"><strong>:</strong></span></code></em> <em class="replaceable"><code>value</code></em>] <em class="replaceable"><code>...</code></em></p>
  490. <div class="blockquote">
  491. <blockquote class="blockquote">
  492. <p>This sets the value of the <code class="literal">key</code> option to <code class="literal">value</code>
  493. for the current module (source file). It takes effect as
  494. soon it is seen during the first macro-expansion pass,
  495. and is active thereafter (unless overridden by <code class="literal">with-compile-options</code>).
  496. </p>
  497. <p>The <em class="replaceable"><code>key:</code></em> is one of the supported option names
  498. (The ending colon makes it a Kawa keyword). Valid
  499. option keys are:
  500. </p>
  501. <div class="itemizedlist" epub:type="list">
  502. <ul style="list-style-type: disc; " class="itemizedlist">
  503. <li class="listitem" epub:type="list-item">
  504. <p><code class="literal"><span class="bold"><strong>main:</strong></span></code> - Generate an application, with a main method.
  505. </p>
  506. </li>
  507. </ul>
  508. </div>
  509. <div class="itemizedlist" epub:type="list">
  510. <ul style="list-style-type: disc; " class="itemizedlist">
  511. <li class="listitem" epub:type="list-item">
  512. <p><code class="literal"><span class="bold"><strong>full-tailcalls:</strong></span></code> - Use a calling convention that supports proper tail recursion.
  513. </p>
  514. </li>
  515. </ul>
  516. </div>
  517. <div class="itemizedlist" epub:type="list">
  518. <ul style="list-style-type: disc; " class="itemizedlist">
  519. <li class="listitem" epub:type="list-item">
  520. <p><code class="literal"><span class="bold"><strong>warn-undefined-variable:</strong></span></code> - Warn if no compiler-visible binding for a variable.
  521. </p>
  522. </li>
  523. <li class="listitem" epub:type="list-item">
  524. <p><code class="literal"><span class="bold"><strong>warn-unknown-member:</strong></span></code> - Warn if referencing an unknown method or field.
  525. </p>
  526. </li>
  527. <li class="listitem" epub:type="list-item">
  528. <p><code class="literal"><span class="bold"><strong>warn-invoke-unknown-method:</strong></span></code> - Warn if invoke calls an unknown method (subsumed by warn-unknown-member).
  529. </p>
  530. </li>
  531. <li class="listitem" epub:type="list-item">
  532. <p><code class="literal"><span class="bold"><strong>warn-unused:</strong></span></code> - Warn if a variable is usused or code never executed.
  533. </p>
  534. </li>
  535. <li class="listitem" epub:type="list-item">
  536. <p><code class="literal"><span class="bold"><strong>warn-unreachable:</strong></span></code> - Warn if this code can never be executed.
  537. </p>
  538. </li>
  539. <li class="listitem" epub:type="list-item">
  540. <p><code class="literal"><span class="bold"><strong>warn-void-used:</strong></span></code> - Warn if an expression depends on the value of a void sub-expression (one that never returns a value).
  541. </p>
  542. </li>
  543. <li class="listitem" epub:type="list-item">
  544. <p><code class="literal"><span class="bold"><strong>warn-as-error:</strong></span></code> - Treat a compilation warning as if it were an error.
  545. </p>
  546. </li>
  547. </ul>
  548. </div>
  549. <p>The <em class="replaceable"><code>value</code></em> must be a literal value: either a boolean
  550. (<code class="literal">#t</code> or <code class="literal">#f</code>), a number, or a string,
  551. depending on the <em class="replaceable"><code>key</code></em>.
  552. (All the options so far are boolean options.)
  553. </p>
  554. <pre class="screen">(module-compile-options warn-undefined-variable: #t)
  555. ;; This causes a warning message that y is unknown.
  556. (define (func x) (list x y))
  557. </pre>
  558. </blockquote>
  559. </div>
  560. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667871394864" class="indexterm"/> <code class="function">with-compile-options</code> [<em class="replaceable"><code>key:</code></em> <em class="replaceable"><code>value</code></em>] <em class="replaceable"><code>...</code></em> <em class="replaceable"><code>body</code></em></p>
  561. <div class="blockquote">
  562. <blockquote class="blockquote">
  563. <p>Similar to <code class="literal">module-compile-options</code>, but the option
  564. is only active within <em class="replaceable"><code>body</code></em>.
  565. </p>
  566. <p>The module option key <code class="literal">main:</code> has no effect when applied
  567. to a particular body via the <code class="literal">with-compile-options</code> syntax.
  568. </p>
  569. <pre class="screen">(define (func x)
  570. (with-compile-options warn-invoke-unknown-method: #f
  571. (invoke x 'size)))
  572. </pre>
  573. </blockquote>
  574. </div>
  575. </section>
  576. </section>
  577. <footer>
  578. <div class="navfooter">
  579. <ul>
  580. <li>
  581. <b class="toc">
  582. <a href="Module-classes.xhtml#idm139667871606752">Name visibility</a>
  583. </b>
  584. </li>
  585. <li>
  586. <b class="toc">
  587. <a href="Module-classes.xhtml#idm139667871573376">R7RS explicit library modules</a>
  588. </b>
  589. </li>
  590. <li>
  591. <b class="toc">
  592. <a href="Module-classes.xhtml#idm139667871515088">How a module becomes a class</a>
  593. </b>
  594. </li>
  595. <li>
  596. <b class="toc">
  597. <a href="Module-classes.xhtml#idm139667871467792">Same class for module and defined class</a>
  598. </b>
  599. </li>
  600. <li>
  601. <b class="toc">
  602. <a href="Module-classes.xhtml#idm139667871461264">Static vs non-static modules</a>
  603. </b>
  604. </li>
  605. <li>
  606. <b class="toc">
  607. <a href="Module-classes.xhtml#idm139667871418480">Module options</a>
  608. </b>
  609. </li>
  610. </ul>
  611. <p>
  612. Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  613. <p>
  614. Previous: <a accesskey="p" href="Annotations.xhtml">Annotations of declarations</a></p>
  615. <p>
  616. Next: <a accesskey="n" href="Importing.xhtml">Importing from a library</a></p>
  617. </div>
  618. </footer>
  619. </body>
  620. </html>