FAQs.xhtml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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>Frequently Asked Questions</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="Low-level-functions.xhtml" title="Deprecated low-level functions"/>
  10. <link rel="next" href="Framework.xhtml" title="The Kawa language framework"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="chapter" title="Frequently Asked Questions" epub:type="chapter" id="FAQs">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title">Frequently Asked Questions</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <span id="importing-class-names"/>
  23. <h4 id="idm139667869230288">What is the equivalent of Java import?</h4>
  24. <p>To provide a short name for a class instead of the complete fully-qualified
  25. name use either <code class="literal">define-alias</code> (or <code class="literal">define-private-alias</code>)
  26. or the <code class="literal">import</code>-<code class="literal">class</code> combination.
  27. For example, to be able to write <code class="literal">ArrayList</code> instead
  28. of <code class="literal">java.util.ArrayList</code> do either:
  29. </p>
  30. <pre class="screen">(import (class java.util ArrayList))
  31. </pre>
  32. <p>or
  33. </p>
  34. <pre class="screen">(define-alias ArrayList java.util.ArrayList)
  35. </pre>
  36. <p>Using <code class="literal">import</code> is recommended:
  37. It handles errors better,
  38. and it allows you to define multiple aliases conveniently:
  39. </p>
  40. <pre class="screen">(import (class java.util Map HashMap))
  41. </pre>
  42. <p>Both forms allow renaming. For example if you want to refer
  43. to <code class="literal">java.lang.StringBuilder</code> as <code class="literal">StrBuf</code> do:
  44. </p>
  45. <pre class="screen">(import (class java.lang (StringBuilder StrBuf)))
  46. </pre>
  47. <p>or:
  48. </p>
  49. <pre class="screen">(define-alias StrBuf java.lang.StringBuilder)
  50. </pre>
  51. <p>The name(s) defined by <code class="literal">import</code> are by default private.
  52. A name defined using <code class="literal">define-alias</code> is by default exported;
  53. to avoid that use <code class="literal">define-private-alias</code> instead.
  54. </p>
  55. <p>You can also use <code class="literal">define-namespace</code> to introduce an abbreviation or
  56. renaming of a class name, but as a matter of style <code class="literal">define-alias</code>
  57. is preferred.
  58. </p>
  59. <p>There is no direct equivalent to Java’s <code class="literal">import PackageOrTypeName.*</code>
  60. (type-import-on-demand) declaration, but you can alias a package:
  61. </p>
  62. <pre class="screen">(define-alias jutil java.util)
  63. (define mylist :: jutil:List (jutil:ArrayList))
  64. </pre>
  65. <p>To import a static member, giving it a shortened name
  66. (like Java’s static-import-on-demand declaration), you can use
  67. <code class="literal">define-alias</code>. For example:
  68. </p>
  69. <pre class="screen">(define-alias console java.lang.System:console)
  70. </pre>
  71. <p>For static fields only (not methods or member classes) you can
  72. use an <code class="literal">import</code> form, either:
  73. </p>
  74. <pre class="screen">(import (only (java lang System) out))
  75. </pre>
  76. <p>or:
  77. </p>
  78. <pre class="screen">(import (only java.lang.System out))
  79. </pre>
  80. <p>This works because Kawa can treat any class as a “library”;
  81. in which case it considers all public static fields as exported bindings.
  82. </p>
  83. <h4 id="idm139667869213824">How do I refer to a Java member (nested) class?</h4>
  84. <p>Consider the Java SE member class <code class="literal">javax.swing.text.AbstractDocument.Content</code>.
  85. Using the Java syntax doesn’t work in Kawa.
  86. Inside you should use Kawa’s colon operator:
  87. </p>
  88. <pre class="screen">javax.swing.text.AbstractDocument:Content
  89. </pre>
  90. <p>Alternatively, you can use the internal JVM class name:
  91. </p>
  92. <pre class="screen">javax.swing.text.AbstractDocument$Content
  93. </pre>
  94. <h4 id="idm139667869211024">Why does Kawa’s REPL use display rather than write?</h4>
  95. <p>The read-eval-print-loop of most Scheme implementations prints the
  96. evaluation result using <code class="literal">write</code>, while Kawa uses <code class="literal">display</code> by default.
  97. </p>
  98. <p>First note that it is easy to override the default with the
  99. <code class="literal">--output-format</code> command-line option:
  100. </p>
  101. <pre class="screen">$kawa --output-format readable-scheme
  102. #|kawa:1|# "abc"
  103. "abc"
  104. </pre>
  105. <p>The reason <code class="literal">display</code> is the default is because of a vision of the REPL
  106. console as more than just printing out Scheme objects in
  107. textual form for use by a programmer.
  108. Some examples:
  109. </p>
  110. <div class="itemizedlist" epub:type="list">
  111. <ul class="itemizedlist" style="list-style-type: disc; ">
  112. <li class="listitem" epub:type="list-item">
  113. <p>A math program can display equations and graphs as the
  114. output of an expression.
  115. </p>
  116. </li>
  117. <li class="listitem" epub:type="list-item">
  118. <p>An expression can evaluate to a "picture" which would
  119. be <a class="ulink" href="http://per.bothner.com/blog/2007/ReplPane/" target="_top">displayed inline</a>.
  120. </p>
  121. </li>
  122. <li class="listitem" epub:type="list-item">
  123. <p>An HTML/XML obj can be insert into the output in visual
  124. form if the console understands HTML. (There is a prototype
  125. for this that works by using the JavaFX WebView as the display.)
  126. </p>
  127. </li>
  128. <li class="listitem" epub:type="list-item">
  129. <p>The plan for "Kawa-shell" functionality is to have expressions
  130. that evaluate to process objects, which would be lazy strings.
  131. This string would be the data from standard output. Thus the
  132. effect of displaying a process object would be to print out
  133. the standard output - just like a regular shell. Users would
  134. find it confusing/annoying if shell output used quotes.
  135. </p>
  136. </li>
  137. </ul>
  138. </div>
  139. <p>This "repl-as-pad" model doesn’t work as well if the repl
  140. uses <code class="literal">write</code> rather than <code class="literal">display</code>.
  141. </p>
  142. </section>
  143. <footer>
  144. <div class="navfooter">
  145. <p>
  146. Up: <a accesskey="u" href="pt01.xhtml">Part . Reference Documentation</a></p>
  147. <p>
  148. Previous: <a accesskey="p" href="Miscellaneous.xhtml">Miscellaneous topics</a></p>
  149. <p>
  150. Next: <a accesskey="n" href="Framework.xhtml">The Kawa language framework</a></p>
  151. </div>
  152. </footer>
  153. </body>
  154. </html>