Files
wren/values.html
Bob Nystrom 84a3201099 Regenerate
2018-07-13 09:03:56 -07:00

227 lines
9.2 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Values &ndash; Wren</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href='//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel='stylesheet' type='text/css'>
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
</head>
<body id="top">
<header>
<div class="page">
<div class="main-column">
<h1><a href="./">wren</a></h1>
<h2>a classy little scripting language</h2>
</div>
</div>
</header>
<div class="page">
<nav class="big">
<ul>
<li><a href="getting-started.html">Getting Started</a></li>
<li><a href="contributing.html">Contributing</a></li>
</ul>
<section>
<h2>language guide</h2>
<ul>
<li><a href="syntax.html">Syntax</a></li>
<li><a href="values.html">Values</a></li>
<li><a href="lists.html">Lists</a></li>
<li><a href="maps.html">Maps</a></li>
<li><a href="method-calls.html">Method Calls</a></li>
<li><a href="control-flow.html">Control Flow</a></li>
<li><a href="variables.html">Variables</a></li>
<li><a href="functions.html">Functions</a></li>
<li><a href="classes.html">Classes</a></li>
<li><a href="concurrency.html">Concurrency</a></li>
<li><a href="error-handling.html">Error Handling</a></li>
<li><a href="modularity.html">Modularity</a></li>
</ul>
</section>
<section>
<h2>reference</h2>
<ul>
<li><a href="modules">Modules</a></li>
<li><a href="embedding">Embedding</a></li>
<li><a href="performance.html">Performance</a></li>
<li><a href="qa.html">Q &amp; A</a></li>
</ul>
</section>
</nav>
<nav class="small">
<table>
<tr>
<td><a href="getting-started.html">Getting Started</a></td>
<td><a href="contributing.html">Contributing</a></td>
</tr>
<tr>
<td colspan="2"><h2>language guide</h2></td>
<td><h2>reference</h2></td>
</tr>
<tr>
<td>
<ul>
<li><a href="syntax.html">Syntax</a></li>
<li><a href="values.html">Values</a></li>
<li><a href="lists.html">Lists</a></li>
<li><a href="maps.html">Maps</a></li>
<li><a href="method-calls.html">Method Calls</a></li>
<li><a href="control-flow.html">Control Flow</a></li>
</ul>
</td>
<td>
<ul>
<li><a href="variables.html">Variables</a></li>
<li><a href="functions.html">Functions</a></li>
<li><a href="classes.html">Classes</a></li>
<li><a href="concurrency.html">Concurrency</a></li>
<li><a href="error-handling.html">Error Handling</a></li>
<li><a href="modularity.html">Modularity</a></li>
</ul>
</td>
<td>
<ul>
<li><a href="modules">Modules</a></li>
<li><a href="embedding">Embedding</a></li>
<li><a href="performance.html">Performance</a></li>
<li><a href="qa.html">Q &amp; A</a></li>
</ul>
</td>
</tr>
</table>
</nav>
<main>
<h1>Values</h1>
<p>Values are the built-in atomic object types that all other objects are composed
of. They can be created through <em>literals</em>, expressions that evaluate to a
value. All values are <em>immutable</em>&mdash;once created, they do not change. The
number <code>3</code> is always the number <code>3</code>. The string <code>"frozen"</code> can never have its
character array modified in place. </p>
<h2>Booleans <a href="#booleans" name="booleans" class="header-anchor">#</a></h2>
<p>A boolean value represents truth or falsehood. There are two boolean literals,
<code>true</code> and <code>false</code>. Their class is <a href="modules/core/bool.html">Bool</a>. </p>
<h2>Numbers <a href="#numbers" name="numbers" class="header-anchor">#</a></h2>
<p>Like other scripting languages, Wren has a single numeric type:
double-precision floating point. Number literals look like you expect coming
from other languages: </p>
<div class="codehilite"><pre><span></span>0
1234
-5678
3.14159
1.0
-12.34
</pre></div>
<p>Numbers are instances of the <a href="modules/core/num.html">Num</a> class. </p>
<h2>Strings <a href="#strings" name="strings" class="header-anchor">#</a></h2>
<p>A string is an array of bytes. Typically, they store characters encoded in
UTF-8, but you can put any byte values in there, even zero or invalid UTF-8
sequences. (You might have some trouble <em>printing</em> the latter to your terminal,
though.) </p>
<p>String literals are surrounded in double quotes: </p>
<div class="codehilite"><pre><span></span>&quot;hi there&quot;
</pre></div>
<p>A handful of escape characters are supported: </p>
<div class="codehilite"><pre><span></span>&quot;\0&quot; // The NUL byte: 0.
&quot;\&quot;&quot; // A double quote character.
&quot;\\&quot; // A backslash.
&quot;\%&quot; // A percent sign.
&quot;\a&quot; // Alarm beep. (Who uses this?)
&quot;\b&quot; // Backspace.
&quot;\f&quot; // Formfeed.
&quot;\n&quot; // Newline.
&quot;\r&quot; // Carriage return.
&quot;\t&quot; // Tab.
&quot;\v&quot; // Vertical tab.
</pre></div>
<p>A <code>\u</code> followed by four hex digits can be used to specify a Unicode code point: </p>
<div class="codehilite"><pre><span></span>System.print(&quot;\u0041\u0b83\u00DE&quot;) //&gt; AஃÞ
</pre></div>
<p>A capital <code>\U</code> followed by <em>eight</em> hex digits allows Unicode code points outside
of the basic multilingual plane, like all-important emoji: </p>
<div class="codehilite"><pre><span></span>System.print(&quot;\U0001F64A\U0001F680&quot;) //&gt; 🙊🚀
</pre></div>
<p>A <code>\x</code> followed by two hex digits specifies a single unencoded byte: </p>
<div class="codehilite"><pre><span></span>System.print(&quot;\x48\x69\x2e&quot;) //&gt; Hi.
</pre></div>
<p>Strings are instances of class <a href="modules/core/string.html">String</a>. </p>
<h3>Interpolation <a href="#interpolation" name="interpolation" class="header-anchor">#</a></h3>
<p>String literals also allow <em>interpolation</em>. If you have a percent sign (<code>%</code>)
followed by a parenthesized expression, the expression is evaluated. The
resulting object&rsquo;s <code>toString</code> method is called and the result is inserted in the
string: </p>
<div class="codehilite"><pre><span></span>System.print(&quot;Math %(3 + 4 * 5) is fun!&quot;) //&gt; Math 23 is fun!
</pre></div>
<p>Arbitrarily complex expressions are allowed inside the parentheses: </p>
<div class="codehilite"><pre><span></span>System.print(&quot;wow %((1..3).map {|n| n * n}.join())&quot;) //&gt; wow 149
</pre></div>
<p>An interpolated expression can even contain a string literal which in turn has
its own nested interpolations, but doing that gets unreadable pretty quickly. </p>
<h2>Ranges <a href="#ranges" name="ranges" class="header-anchor">#</a></h2>
<p>A range is a little object that represents a consecutive range of numbers. They
don&rsquo;t have their own dedicated literal syntax. Instead, the number class
implements the <code>..</code> and <code>...</code> <a href="method-calls.html#operators">operators</a> to create them: </p>
<div class="codehilite"><pre><span></span>3..8
</pre></div>
<p>This creates a range from three to eight, including eight itself. If you want a
half-inclusive range, use <code>...</code>: </p>
<div class="codehilite"><pre><span></span>4...6
</pre></div>
<p>This creates a range from four to six <em>not</em> including six itself. Ranges are
commonly used for <a href="control-flow.html#for-statements">iterating</a> over a
sequences of numbers, but are useful in other places too. You can pass them to
a <a href="lists.html">list</a>&lsquo;s subscript operator to return a subset of the list, for
example: </p>
<div class="codehilite"><pre><span></span>var list = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]
var slice = list[1..3]
System.print(slice) //&gt; [b, c, d]
</pre></div>
<p>Their class is <a href="modules/core/range.html">Range</a>. </p>
<h2>Null <a href="#null" name="null" class="header-anchor">#</a></h2>
<p>Wren has a special value <code>null</code>, which is the only instance of the class
<a href="modules/core/null.html">Null</a>. (Note the difference in case.) It functions a bit like <code>void</code> in some
languages: it indicates the absence of a value. If you call a method that
doesn&rsquo;t return anything and get its returned value, you get <code>null</code> back. </p>
<p><a class="right" href="lists.html">Lists &rarr;</a>
<a href="syntax.html">&larr; Syntax</a> </p>
</main>
</div>
<footer>
<div class="page">
<div class="main-column">
<p>Wren lives
<a href="https://github.com/munificent/wren">on GitHub</a>
&mdash; Made with &#x2764; by
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
<a href="https://github.com/munificent/wren/blob/master/AUTHORS">friends</a>.
</p>
<div class="main-column">
</div>
</footer>
</body>
</html>