mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 14:48:40 +01:00
130 lines
6.8 KiB
HTML
130 lines
6.8 KiB
HTML
<!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='//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="expressions.html">Expressions</a></li>
|
||
<li><a href="variables.html">Variables</a></li>
|
||
<li><a href="control-flow.html">Control Flow</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 & 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">"Slow Loris"</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 "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="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'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">// "123"</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'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">"outer"</span>
|
||
<span class="p">{</span>
|
||
<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">"inner"</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 "inner".</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 "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="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">"hi"</span>
|
||
<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span class="s2">"again"</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">"before"</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">"after"</span><span class="p">)</span> <span class="c1">// Prints "after".</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> — Made with ❤ by <a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a>.</p>
|
||
<div class="main-column">
|
||
</div>
|
||
</footer>
|
||
</body>
|
||
</html> |