Types.xhtml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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>Types</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="Standard-Types.xhtml" title="Standard Types"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="chapter" title="Types" epub:type="chapter" id="Types">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title">Types</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>A <em class="firstterm">type</em> is a set of values, plus an associated set of operations
  23. valid on those values.
  24. Types are useful for catching errors ("type-checking"), documenting
  25. the programmer’s intent, and to help the compiler generate better code.
  26. Types in some languages (such as C) appear in programs,
  27. but do not exist at run-time. In such languages, all type-checking
  28. is done at compile-time. Other languages (such as standard Scheme)
  29. do not have types as such, but they have <em class="firstterm">predicates</em>, which
  30. allow you to check if a value is a member of certain sets; also,
  31. the primitive functions will check at run-time if the arguments
  32. are members of the allowed sets. Other languages, including Java
  33. and Common Lisp, provide a combination: Types may be used as specifiers
  34. to guide the compiler, but also exist as actual run-time values.
  35. In Java, for each class, there is a corresponding <code class="literal">java.lang.Class</code>
  36. run-time object, as well as an associated type (the set of values
  37. of that class, plus its sub-classes, plus <code class="literal">null</code>).
  38. </p>
  39. <p>Kawa, like Java, has first-class types, that is types exist as
  40. objects you can pass around at run-time. For each Java type,
  41. there is a corresponding Kawa type (but not necessarily vice
  42. versa). It would be nice if we could represent run-time
  43. type values using <code class="literal">java.lang.Class</code> objects, but unfortunately
  44. that does not work very well. One reason is that we need
  45. to be able to refer to types and classes that do not exist yet,
  46. because we are in the processing of compiling them. Another
  47. reason is that we want to be able to distinguish between different
  48. types that are implemented using the same Java class.
  49. </p>
  50. <p>Various Kawa constructs require or allow a type to be specified.
  51. Those specifications consist of <em class="firstterm">type expressions</em>, and a type expression
  52. is evaluated to yield a type value. The current Kawa compiler
  53. is rather simple-minded, and in many places only allows simple
  54. types that the compiler can evaluate at compile-time.
  55. More specifically, it only allows simple <em class="firstterm">type names</em>
  56. that map to primitive Java types or Java classes.
  57. </p>
  58. <div class="literallayout">
  59. <p><a id="idm139667872008096" class="indexterm"/><span id="meta-type"/><em class="replaceable"><code>type</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/>
  60. <a id="idm139667872005296" class="indexterm"/><span id="meta-opt-type-specifier"/><em class="replaceable"><code>opt-type-specifier</code></em> <code class="literal">::=</code> [<code class="literal"><span class="bold"><strong>::</strong></span></code> <a class="link" href="Types.xhtml#meta-type"><em class="replaceable"><code>type</code></em></a>]<br/>
  61. </p>
  62. </div>
  63. </section>
  64. <footer>
  65. <div class="navfooter">
  66. <ul>
  67. <li>
  68. <b class="toc">
  69. <a href="Standard-Types.xhtml">Standard Types</a>
  70. </b>
  71. </li>
  72. <li>
  73. <b class="toc">
  74. <a href="Parameterized-Types.xhtml">Parameterized Types</a>
  75. </b>
  76. </li>
  77. <li>
  78. <b class="toc">
  79. <a href="Type-tests-and-conversions.xhtml">Type tests and conversions</a>
  80. </b>
  81. </li>
  82. </ul>
  83. <p>
  84. Up: <a accesskey="u" href="pt01.xhtml">Part . Reference Documentation</a></p>
  85. <p>
  86. Previous: <a accesskey="p" href="Input-Output.xhtml">Input, output, and file handling</a></p>
  87. <p>
  88. Next: <a accesskey="n" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  89. </div>
  90. </footer>
  91. </body>
  92. </html>