123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Gravity: Types</title>
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" />
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,700,800">
- <link rel="stylesheet" href="scripts/highlight/styles/github-gist.css">
- <link rel="stylesheet" href="stylesheets/styles.css">
- </head>
- <body>
- <!-- BEGIN NAVIGATION BAR -->
- <nav class="navbar navbar-default navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="http://gravity-lang.org"><img src="images/logo.png" width="32px" height="36px"></a>
- </div>
- <div id="navbar" class="navbar-collapse collapse">
- <ul class="nav navbar-nav navbar-right">
- <li class="active"><a href="index.html">Gravity</a></li>
- <li><a href="internals/index.html">Internals</a></li>
- <li><a href="https://github.com/marcobambini/gravity">GitHub</a></li>
- </ul>
- </div><!--/.nav-collapse -->
- </div>
- </nav>
- <!-- END NAVIGATION BAR -->
- <div class="container body-container">
- <div class="main-content">
- <div class="row">
-
- <!-- BEGIN SIDEBAR -->
- <div class="col-sm-3 border-right section-left">
- <div saveheight="1" class="sidebar-nav">
- <h4>INTRODUCTION</h4>
- <ul>
- <li><a href="index.html"><span>Introduction</span></a></li>
- <li><a href="getting-started.html"><span>Getting Started</span></a></li>
- </ul>
- <h4>LANGUAGE GUIDE</h4>
- <ul>
- <li><a href="syntax.html"><span>Syntax</span></a></li>
- <li><a href="operators.html"><span>Operators</span></a></li>
- <li><a href="types.html" class="active"><span>Types</span></a></li>
- <li><a href="lists.html"><span>Lists</span></a></li>
- <li><a href="maps.html"><span>Maps</span></a></li>
- <li><a href="enum.html"><span>Enum</span></a></li>
- <li><a href="functions.html"><span>Functions</span></a></li>
- <li><a href="closures.html"><span>Closures</span></a></li>
- <li><a href="classes.html"><span>Classes</span></a></li>
- <li><a href="control-flow.html"><span>Control Flow</span></a></li>
- <li><a href="loops.html"><span>Loops</span></a></li>
- <li><a href="fibers.html"><span>Fibers</span></a></li>
- </ul>
- <h4>ADVANCED</h4>
- <ul>
- <li><a href="api.html"><span>Embedding API</span></a></li>
- <li><a href="system.html"><span>System class</span></a></li>
- <li><a href="unit-test.html"><span>Unit test</span></a></li>
- <li><a href="contributing.html"><span>Contributing</span></a></li>
- </ul>
- </div>
- </div>
- <!-- END SIDEBAR -->
- <div class="col-sm-9 border-left section-right">
- <h1 class="section-header">Values and Types</h1><hr>
-
- <p class="section-content">
- 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.
- </p>
-
- <h4 class="section-h4">Object</h4>
- <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>
-
- <h4 class="section-h4">Int</h4>
- <p>An Int represents a 64 bits (can optionally be compiled as 32bit) signed number:</p>
- <pre><code class="swift">
- var a = 123; // decimal
- var b = 0xFF; // hexadecimal
- var c = 0O7777; // octal
- var d = 0B0101; // binary
- var e = Int.random(1, 10) // Returns a random integer between 1 and 10 inclusive
- </code></pre>
-
- <h4 class="section-h4">Float</h4>
- <p>A float represents a 32 bits (or better) floating point number:</p>
- <pre><code class="swift">
- var a = 3.1415; // float
- var b = 1.25e2; // scientific notation
- </code></pre>
-
- <h4 class="section-h4">String</h4>
- <p>Strings are an immutable sequence of characters. String literals can be surrounded in double or single quotes.</p>
- <pre><code class="swift">
- var a = "Hello World"; // double quotes
- var b = 'Hello World'; // single quotes
-
- // Strings have also a length property
- var n = b.length; // n is now 11
- // Strings also have some built in methods
- n = b.index("Wor") // n is now 6.
- n = b.count("l") // n is now 3.
- n = b.count("llo") // n is now 1.
- n = "A".repeat(10) // n is now "AAAAAAAAAA"
- // upper() and lower() operate on the whole string when 0 arguments are passed
- n = b.upper() // n is now "HELLO WORLD"
- n = b.lower() // n is now "hello world"
- // upper() and lower() can both take multiple integer arguments
- n = b.upper(1, -1) // n is now "HEllo WorlD"
- // split() is the opposite of a list join
- var string = "Roses are Red, Violets are Blue"
- var list = string.split(", ")
- list[0] // "Roses are Red"
- list[1] // "Violets are Blue"
- // You are also able to edit strings by character...
- b[0] = "Z" // b is now "Zello World"
- b[1] = "abc" // b is now "Zabco World"
- b[-7] = "QWERTY" // b is now "ZabcQWERTYd"
- // and retrieve those characters
- n = a[6] // n is now "W"
- n = a[-7] // n is now "o"
- n = a[0...4] // n is now "Hello"
- n = a[-5...-1] // n is now "World"
- n = a[-5...10] // n is now "World"
- n = a[-1...-5] // n is now "dlroW"
- </code></pre>
-
- <h4 class="section-h4">Bool</h4>
- <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>
- <pre><code class="swift">
- var a = true;
- var b = false;
- </code></pre>
-
- <h4 class="section-h4">Null</h4>
- <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>
- <pre><code class="swift">
- class Newton {
- var mass = 10;
- var acceleration;
- func force() {
- return mass * acceleration;
- }
- }
-
- func f2() {
- var sir = Newton();
- // acceleration instance variable has no default value
- // so it is automatically set to null
- return sir.force();
- }
-
- func f1() {
- var a;
- // a is uninitialized so it has a default null value
- return a;
- }
- </code></pre>
-
- <h4 class="section-h4">Range</h4>
- <p>A range is an object that represents a consecutive range of numbers. Syntax for this type has been directly inspired by Swift.</p>
- <pre><code class="swift">
- // a represents a range with values 1,2,3
- var a = 1...3;
-
- // b represents a range with values 1,2
- var b = 1..<3;
-
- // Ranges have also a conveniente count property
- var n1 = a.count; // n1 is now 3
- var n2 = b.count; // n2 is now 2
- </code></pre>
-
- </div>
- </div> <!-- /row -->
- </div> <!-- /main-content -->
- </div> <!-- /container -->
- <!-- BEGIN FOOTER -->
- <footer class="navbar-fixed-bottom">
- <div class="container footer">
- <p>
- </p>
- </div>
- </footer>
- <!-- END FOOTER -->
-
- <!-- Bootstrap JS -->
- <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
-
- <!-- Highlights JS -->
- <script src="scripts/highlight/highlight.min.js"></script>
- <script>hljs.initHighlightingOnLoad();</script>
-
- </body>
- </html>
|