Files
wren/variables.html
2019-02-06 02:52:27 +00:00

184 lines
8.9 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Variables &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">
<a href="./"><img src="./wren.svg" class="logo"></a>
<ul>
<li><a href="getting-started.html">Getting Started</a></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="blog">Blog</a></li>
</ul>
<section>
<h2>guides</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>guides</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>
<h2>Variables</h2>
<p>Variables are named slots for storing values. You define a new variable in Wren
using a <code>var</code> statement, like so: </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
</pre></div>
<p>This creates a new variable <code>a</code> in the current scope and initializes it with
the result of the expression following the <code>=</code>. Once a variable has been
defined, it can be accessed by name as you would expect. </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="n">animal</span> <span class="o">=</span> <span class="s">&quot;Slow Loris&quot;</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="n">animal</span><span class="p">)</span> <span class="output">Slow Loris</span>
</pre></div>
<h2>Scope <a href="#scope" name="scope" class="header-anchor">#</a></h2>
<p>Wren has true block scope: a variable exists from the point where it is defined
until the end of the <a href="syntax.html#blocks">block</a> where that definition appears. </p>
<div class="codehilite"><pre><span class="p">{</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span><span class="p">)</span> <span class="error">&quot;a&quot; doesn&#39;t exist yet.</span>
<span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="mi">123</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span><span class="p">)</span> <span class="output">123</span>
<span class="p">}</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span><span class="p">)</span> <span class="error">&quot;a&quot; doesn&#39;t exist anymore.</span>
</pre></div>
<p>Variables defined at the top level of a script are <em>top-level</em> and are visible
to the <a href="modules.html">module</a> system. All other variables are <em>local</em>.
Declaring a variable in an inner scope with the same name as an outer one is
called <em>shadowing</em> and is not an error (although it&rsquo;s not something you likely
intend to do much). </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="s">&quot;outer&quot;</span>
<span class="p">{</span>
<span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="s">&quot;inner&quot;</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span><span class="p">)</span> <span class="output">inner</span>
<span class="p">}</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span><span class="p">)</span> <span class="output">outer</span>
</pre></div>
<p>Declaring a variable with the same name in the <em>same</em> scope <em>is</em> an error. </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="s">&quot;hi&quot;</span>
<span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="s">&quot;again&quot;</span> <span class="error">&quot;a&quot; is already declared.</span>
</pre></div>
<h2>Assignment <a href="#assignment" name="assignment" class="header-anchor">#</a></h2>
<p>After a variable has been declared, you can assign to it using <code>=</code>: </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="mi">123</span>
<span class="err">a</span> <span class="o">=</span> <span class="mi">234</span>
</pre></div>
<p>An assignment walks up the scope stack to find where the named variable is
declared. It&rsquo;s an error to assign to a variable that isn&rsquo;t defined. Wren
doesn&rsquo;t roll with implicit variable definition. </p>
<p>When used in a larger expression, an assignment expression evaluates to the
assigned value. </p>
<div class="codehilite"><pre><span class="k">var</span> <span class="err">a</span> <span class="o">=</span> <span class="s">&quot;before&quot;</span>
<span class="vg">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="err">a</span> <span class="o">=</span> <span class="s">&quot;after&quot;</span><span class="p">)</span> <span class="output">after</span>
</pre></div>
<p>If the left-hand side is some more complex expression than a bare variable name,
then it isn&rsquo;t an assignment. Instead, it&rsquo;s calling a <a href="method-calls.html#setters">setter method</a>. </p>
<p><br><hr>
<a class="right" href="functions.html">Functions &rarr;</a>
<a href="control-flow.html">&larr; Control Flow</a> </p>
</main>
</div>
<footer>
<div class="page">
<div class="main-column">
<p>Wren lives
<a href="https://github.com/wren-lang/wren">on GitHub</a>
&mdash; Made with &#x2764; by
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
<a href="https://github.com/wren-lang/wren/blob/master/AUTHORS">friends</a>.
</p>
<div class="main-column">
</div>
</footer>
</body>
</html>