types.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Gravity: Types</title>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
  7. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
  9. <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
  10. <link rel="stylesheet" href="stylesheets/styles.css">
  11. </head>
  12. <body>
  13. <!-- BEGIN NAVIGATION BAR -->
  14. <nav class="navbar navbar-default navbar-fixed-top">
  15. <div class="container">
  16. <div class="navbar-header">
  17. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
  18. <span class="sr-only">Toggle navigation</span>
  19. <span class="icon-bar"></span>
  20. <span class="icon-bar"></span>
  21. <span class="icon-bar"></span>
  22. </button>
  23. <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
  24. </div>
  25. <div id="navbar" class="navbar-collapse collapse">
  26. <ul class="nav navbar-nav navbar-right">
  27. <li class="active"><a href="index.html">Gravity</a></li>
  28. <li><a href="internals/index.html">Internals</a></li>
  29. <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
  30. </ul>
  31. </div><!--/.nav-collapse -->
  32. </div>
  33. </nav>
  34. <!-- END NAVIGATION BAR -->
  35. <div class="container body-container">
  36. <div class="main-content">
  37. <div class="row">
  38. <!-- BEGIN SIDEBAR -->
  39. <div class="col-sm-3 border-right section-left">
  40. <div saveheight="1" class="sidebar-nav">
  41. <h4>INTRODUCTION</h4>
  42. <ul>
  43. <li><a href="index.html"><span>Introduction</span></a></li>
  44. <li><a href="getting-started.html"><span>Getting Started</span></a></li>
  45. </ul>
  46. <h4>LANGUAGE GUIDE</h4>
  47. <ul>
  48. <li><a href="syntax.html"><span>Syntax</span></a></li>
  49. <li><a href="operators.html"><span>Operators</span></a></li>
  50. <li><a href="types.html" class="active"><span>Types</span></a></li>
  51. <li><a href="lists.html"><span>Lists</span></a></li>
  52. <li><a href="maps.html"><span>Maps</span></a></li>
  53. <li><a href="enum.html"><span>Enum</span></a></li>
  54. <li><a href="functions.html"><span>Functions</span></a></li>
  55. <li><a href="closures.html"><span>Closures</span></a></li>
  56. <li><a href="classes.html"><span>Classes</span></a></li>
  57. <li><a href="control-flow.html"><span>Control Flow</span></a></li>
  58. <li><a href="loops.html"><span>Loops</span></a></li>
  59. <li><a href="fibers.html"><span>Fibers</span></a></li>
  60. </ul>
  61. <h4>ADVANCED</h4>
  62. <ul>
  63. <li><a href="api.html"><span>Embedding API</span></a></li>
  64. <li><a href="system.html"><span>System class</span></a></li>
  65. <li><a href="unit-test.html"><span>Unit test</span></a></li>
  66. <li><a href="contributing.html"><span>Contributing</span></a></li>
  67. </ul>
  68. </div>
  69. </div>
  70. <!-- END SIDEBAR -->
  71. <div class="col-sm-9 border-left section-right">
  72. <h1 class="section-header">Values and Types</h1><hr>
  73. <p class="section-content">
  74. Gravity is a dynamically typed language so variables do not have a type, although they refer to a value that does have a type. In Gravity everything is an object (with methods you can call and instance variables you can use). Basic intrinsic types are of class Object, Int, Float, String, Bool, Null, <a href="classes.html">Class</a>, <a href="functions.html">Function</a>, Fiber, Instance, <a href="lists.html">List</a>, <a href="maps.html">Map</a> and Range.
  75. </p>
  76. <h4 class="section-h4">Object</h4>
  77. <p>Object is the root class of every object inside Gravity. Through the Object class, objects inherit a basic interface to the runtime system and the ability to behave as Gravity objects.</p>
  78. <h4 class="section-h4">Int</h4>
  79. <p>An Int represents a 64 bits (can optionally be compiled as 32bit) signed number:</p>
  80. <pre><code class="swift">
  81. var a = 123; // decimal
  82. var b = 0xFF; // hexadecimal
  83. var c = 0O7777; // octal
  84. var d = 0B0101; // binary
  85. </code></pre>
  86. <h4 class="section-h4">Float</h4>
  87. <p>A float represents a 32 bits (or better) floating point number:</p>
  88. <pre><code class="swift">
  89. var a = 3.1415; // float
  90. var b = 1.25e2; // scientific notation
  91. </code></pre>
  92. <h4 class="section-h4">String</h4>
  93. <p>Strings are an immutable sequence of characters. String literals can be surrounded in double or single quotes.</p>
  94. <pre><code class="swift">
  95. var a = "Hello World"; // double quotes
  96. var b = 'Hello World'; // single quotes
  97. // Strings have also a length property
  98. var n = b.length; // n is now 11
  99. </code></pre>
  100. <h4 class="section-h4">Bool</h4>
  101. <p>The bool data type can have only two values, they are the literals true and false. A Bool value expresses the validity of a condition (tells whether the condition is true or false).</p>
  102. <pre><code class="swift">
  103. var a = true;
  104. var b = false;
  105. </code></pre>
  106. <h4 class="section-h4">Null</h4>
  107. <p>It indicates the absence of a value. If you call a method that doesn’t return anything and get its returned value, you get null back. The null data type is also used to initialize uninitialized variables with a default value.</p>
  108. <pre><code class="swift">
  109. class Newton {
  110. var mass = 10;
  111. var acceleration;
  112. func force() {
  113. return mass * acceleration;
  114. }
  115. }
  116. func f2() {
  117. var sir = Newton();
  118. // acceleration instance variable has no default value
  119. // so it is automatically set to null
  120. return sir.force();
  121. }
  122. func f1() {
  123. var a;
  124. // a is uninitialized so it has a default null value
  125. return a;
  126. }
  127. </code></pre>
  128. <h4 class="section-h4">Range</h4>
  129. <p>A range is an object that represents a consecutive range of numbers. Syntax for this type has been directly inspired by Swift.</p>
  130. <pre><code class="swift">
  131. // a represents a range with values 1,2,3
  132. var a = 1...3;
  133. // b represents a range with values 1,2
  134. var b = 1..<3;
  135. // Ranges have also a conveniente count property
  136. var n1 = a.count; // n1 is now 3
  137. var n2 = b.count; // n2 is now 2
  138. </code></pre>
  139. </div>
  140. </div> <!-- /row -->
  141. </div> <!-- /main-content -->
  142. </div> <!-- /container -->
  143. <!-- BEGIN FOOTER -->
  144. <footer class="navbar-fixed-bottom">
  145. <div class="container footer">
  146. <p>
  147. </p>
  148. </div>
  149. </footer>
  150. <!-- END FOOTER -->
  151. <!-- Bootstrap JS -->
  152. <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
  153. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  154. <!-- Highlights JS -->
  155. <script src="scripts/highlight/highlight.min.js"></script>
  156. <script>hljs.initHighlightingOnLoad();</script>
  157. </body>
  158. </html>