Files
wren/variables.html
2015-01-01 21:04:14 -08:00

131 lines
6.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Variables Wren</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href='http://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="index.html">wren</a></h1>
<h2>a classy little scripting language</h2>
</div>
</div>
</header>
<div class="page">
<nav>
<ul>
<li><a href="getting-started.html">Getting Started</a></li>
</ul>
<section>
<h2>language</h2>
<ul>
<li><a href="syntax.html">Syntax</a></li>
<li><a href="method-calls.html">Method Calls</a></li>
<li><a href="variables.html">Variables</a></li>
<li><a href="branching.html">Branching</a></li>
<li><a href="looping.html">Looping</a></li>
<li><a href="error-handling.html">Error Handling</a></li>
</ul>
</section>
<section>
<h2>types</h2>
<ul>
<li><a href="values.html">Values</a></li>
<li><a href="classes.html">Classes</a></li>
<li><a href="fibers.html">Fibers</a></li>
<li><a href="functions.html">Functions</a></li>
<li><a href="lists.html">Lists</a></li>
<li><a href="maps.html">Maps</a></li>
</ul>
</section>
<section>
<h2>reference</h2>
<ul>
<li><a href="core-library.html">Core Library</a></li>
<li><a href="embedding-api.html">Embedding API</a></li>
<li><a href="performance.html">Performance</a></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="qa.html">Q &amp; A</a></li>
</ul>
</section>
</nav>
<main>
<h1>Variables</h1>
<p>Variables are named slots for storing values. You can define a new variable in
Wren using a <code>var</code> statement, like so:</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="m">1</span> <span class="o">+</span> <span class="m">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="kd">var</span> <span class="n">animal</span> <span class="o">=</span> <span class="s2">&quot;Slow Loris&quot;</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">animal</span><span class="p">)</span> <span class="c1">// Prints &quot;Slow Loris&quot;.</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 block where that definition appears.</p>
<div class="codehilite"><pre><span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// ERROR! a doesn&#39;t exist yet.</span>
<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="m">123</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// &quot;123&quot;</span>
<span class="p">}</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// ERROR! a doesn&#39;t exist anymore.</span>
</pre></div>
<p>Variables defined at the top level of a script are <em>global</em>. 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's not
something you likely intend to do much).</p>
<div class="codehilite"><pre><span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;outer&quot;</span>
<span class="p">{</span>
<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;inner&quot;</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// Prints &quot;inner&quot;.</span>
<span class="p">}</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// Prints &quot;outer&quot;.</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="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;hi&quot;</span>
<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;again&quot;</span> <span class="c1">// ERROR!</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="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="m">123</span>
<span class="n">a</span> <span class="o">=</span> <span class="m">234</span>
</pre></div>
<p>An assignment walks up the scope stack to find where the named variable is
declared. It's an error to assign to a variable that isn't defined. Wren
doesn'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="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;before&quot;</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="n">a</span> <span class="o">=</span> <span class="s2">&quot;after&quot;</span><span class="p">)</span> <span class="c1">// Prints &quot;after&quot;.</span>
</pre></div>
</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>.</p>
<div class="main-column">
</div>
</footer>
</body>
</html>