updated and rebuilt docs

This commit is contained in:
leaf corcoran 2011-10-01 13:05:57 -07:00
parent ed89e8447a
commit c15089ee1c
2 changed files with 93 additions and 37 deletions

View File

@ -655,23 +655,25 @@ The function stub syntax is a shorthand for creating a new closure function
that bundles both the object and function. This new function calls the wrapped
function in the correct context of the object.
Its syntax is the same as calling an instance method with the `\` operator but
Its syntax is the same as calling an instance method with the <code>\\</code> operator but
with no argument list provided.
my_object = {
value: 1000
print: => print "the value:", @value
write: => print "the value:", @value
}
run_callback (func) ->
print "running callback..."
func!
-- this will not work, the function has to no reference to my_object
run_callback my_object.print
-- this will not work:
-- the function has to no reference to my_object
run_callback my_object.write
-- function stub syntax lets us bundle the object into a new function
run_callback my_object\print
-- function stub syntax
-- lets us bundle the object into a new function
run_callback my_object\write
## The Using Clause; Controlling Destructive Assignment

View File

@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Autogenerated from moonscript/docs/reference.md on Mon Aug 29 08:50:21 2011 -->
<!-- Autogenerated from moonscript/docs/reference.md on Sat Oct 1 12:41:57 2011 -->
<meta charset="UTF-8">
<title>MoonScript v0.2.0</title>
@ -212,6 +212,8 @@ p > code , li > code {
<li><a href="#with_statement">With Statement</a></li>
<li><a href="#function_stubs">Function Stubs</a></li>
<li><a href="#the_using_clause_controlling_destructive_assignment">The Using Clause; Controlling Destructive Assignment</a></li>
</ul>
<li><a href="#moonscript_api">MoonScript API</a></li>
@ -529,7 +531,7 @@ arguments if we know we will be using a lower indentation futher on.</p>
<span class="l_symbol">}</span></code></pre></td></tr></tbody></table>
<p>The same thing can be done with other block level statements like
<a href="#Conditionals">conditionals</a>. We can use indentation level to determine what
<a href="#conditionals">conditionals</a>. We can use indentation level to determine what
statement a value belongs to:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_keyword">if</span> <span class="l_atom">func</span> <span class="l_number">1</span>,<span class="l_number">2</span>,<span class="l_number">3</span>,
@ -991,11 +993,11 @@ need to return the results of the loop.</p>
<span class="l_keyword">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span> <span class="l_atom">my_function</span><span class="l_symbol">!</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_number">10</span>
<span class="l_atom">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_builtins">print</span>(<span class="l_atom">i</span>)
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_atom">i</span> - <span class="l_number">1</span>
<span class="l_keyword">end</span>
<span class="l_atom">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span>
<span class="l_atom">my_function</span>()
<span class="l_keyword">end</span></code></pre></td></tr></tbody></table>
@ -1264,7 +1266,7 @@ properties and methods from another class.</p>
<span class="l_table_key">size</span><span class="l_bold_symbol">:</span> <span class="l_number">10</span>
<span class="l_table_key">add_item</span><span class="l_bold_symbol">:</span> (<span class="l_atom">name</span>) <span class="l_fn_symbol">=&gt;</span>
<span class="l_keyword">if</span> <span class="l_symbol">#</span><span class="l_self_var">@items</span> <span class="l_symbol">&gt;</span> <span class="l_atom">size</span> <span class="l_keyword">then</span> <span class="l_atom">error</span> <span class="l_string">"backpack is full"</span>
<span class="l_atom">super</span> <span class="l_atom">name</span>
<span class="l_keyword">super</span> <span class="l_atom">name</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">BackPack</span>
<span class="l_atom">BackPack</span> <span class="l_symbol">=</span> (<span class="l_fn_symbol">function</span>(<span class="l_atom">_parent_0</span>)
<span class="l_keyword">local</span> <span class="l_atom">_base_0</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
@ -1336,7 +1338,7 @@ will not be assigned locally.</p>
module:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_comment">-- my_module.moon
</span><span class="l_atom">module</span> <span class="l_string">"my_module"</span>, <span class="l_atom">package</span><span class="l_bold_symbol">.</span><span class="l_atom">seeall</span>
</span><span class="l_builtins">module</span> <span class="l_string">"my_module"</span>, <span class="l_builtins">package.seeall</span>
<span class="l_keyword">export</span> <span class="l_atom">print_result</span>
<span class="l_atom">length</span> <span class="l_symbol">=</span> (<span class="l_atom">x</span>, <span class="l_atom">y</span>) <span class="l_fn_symbol">-&gt;</span> <span class="l_atom">math</span><span class="l_bold_symbol">.</span><span class="l_atom">sqrt</span> <span class="l_atom">x</span><span class="l_symbol">*</span><span class="l_atom">x</span> + <span class="l_atom">y</span><span class="l_symbol">*</span><span class="l_atom">y</span>
@ -1345,12 +1347,12 @@ module:</p>
<span class="l_builtins">print</span> <span class="l_string">"Length is "</span>, <span class="l_atom">length</span> <span class="l_atom">x</span>, <span class="l_atom">y</span>
<span class="l_comment">-- main.moon
</span><span class="l_atom">require</span> <span class="l_string">"my_module"</span>
</span><span class="l_builtins">require</span> <span class="l_string">"my_module"</span>
<span class="l_atom">my_module</span><span class="l_bold_symbol">.</span><span class="l_atom">print_result</span> <span class="l_number">4</span>, <span class="l_number">5</span> <span class="l_comment">-- prints the result
</span>
<span class="l_builtins">print</span> <span class="l_atom">my_module</span><span class="l_bold_symbol">.</span><span class="l_atom">length</span> <span class="l_number">6</span>, <span class="l_number">7</span> <span class="l_comment">-- errors, `length` not visible
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_atom">module</span>(<span class="l_string">"my_module"</span>, <span class="l_atom">package</span><span class="l_symbol">.</span><span class="l_atom">seeall</span>)
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">module</span>(<span class="l_string">"my_module"</span>, <span class="l_builtins">package.seeall</span>)
<span class="l_keyword">local</span> <span class="l_atom">length</span>
<span class="l_atom">length</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>)
<span class="l_keyword">return</span> <span class="l_atom">math</span><span class="l_symbol">.</span><span class="l_atom">sqrt</span>(<span class="l_atom">x</span> * <span class="l_atom">x</span> + <span class="l_atom">y</span> * <span class="l_atom">y</span>)
@ -1358,7 +1360,7 @@ module:</p>
<span class="l_atom">print_result</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>)
<span class="l_keyword">return</span> <span class="l_builtins">print</span>(<span class="l_string">"Length is "</span>, <span class="l_atom">length</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>))
<span class="l_keyword">end</span>
<span class="l_atom">require</span>(<span class="l_string">"my_module"</span>)
<span class="l_builtins">require</span>(<span class="l_string">"my_module"</span>)
<span class="l_atom">my_module</span><span class="l_symbol">.</span><span class="l_atom">print_result</span>(<span class="l_number">4</span>, <span class="l_number">5</span>)
<span class="l_builtins">print</span>(<span class="l_atom">my_module</span><span class="l_symbol">.</span><span class="l_atom">length</span>(<span class="l_number">6</span>, <span class="l_number">7</span>))</code></pre></td></tr></tbody></table>
@ -1387,7 +1389,7 @@ to bind it to that table:</p>
<span class="l_keyword">import</span> <span class="l_symbol">\</span><span class="l_atom">add</span> <span class="l_keyword">from</span> <span class="l_atom">my_module</span>
<span class="l_builtins">print</span> <span class="l_atom">add</span>(<span class="l_number">22</span>) <span class="l_comment">-- equivalent to calling my_module:get(22)
<span class="l_builtins">print</span> <span class="l_atom">add</span>(<span class="l_number">22</span>) <span class="l_comment">-- equivalent to calling my_module\get 22
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">my_module</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
<span class="l_atom">state</span> <span class="l_symbol">=</span> <span class="l_number">100</span>,
<span class="l_atom">add</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">self</span>, <span class="l_atom">value</span>)
@ -1407,8 +1409,11 @@ unnecessary noise. A common solution to this is to pass a table in as an
argument which contains a collection of keys and values to overwrite. The
downside to this is that the constructor of this object must support this form.</p>
<p>The <code>with</code> block helps to alleviate this. It lets us use a bare function and
index syntax in order to work with the object:</p>
<p>The <code>with</code> block helps to alleviate this. Within a <code>with</code> block we can use a
special statements that begin with either <code>.</code> or <code>\` which represent
those operations applied to the object we are using</code>with<code>on.</code></p>
<p>For example, we work with a newly created object:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_keyword">with</span> <span class="l_proper">Person</span><span class="l_symbol">!</span>
<span class="l_bold_symbol">.</span><span class="l_atom">name</span> <span class="l_symbol">=</span> <span class="l_string">"Oswald"</span>
@ -1464,6 +1469,55 @@ it has been giving access to.</p>
<span class="l_atom">sister</span>
<span class="l_symbol">}</span>)</code></pre></td></tr></tbody></table>
<h2><a name="function_stubs"></a>Function Stubs</h2>
<p>It is common to pass a function from an object around as a value, for example,
passing an instance method into a function as a callback. If the function
expects the object it is operating on as the first argument then you must
somehow bundle that object with the function so it can be called properly.</p>
<p>The function stub syntax is a shorthand for creating a new closure function
that bundles both the object and function. This new function calls the wrapped
function in the correct context of the object.</p>
<p>Its syntax is the same as calling an instance method with the <code>\</code> operator but
with no argument list provided.</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">my_object</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">{</span>
<span class="l_table_key">value</span><span class="l_bold_symbol">:</span> <span class="l_number">1000</span>
<span class="l_table_key">write</span><span class="l_bold_symbol">:</span> <span class="l_fn_symbol">=&gt;</span> <span class="l_builtins">print</span> <span class="l_string">"the value:"</span>, <span class="l_self_var">@value</span>
<span class="l_fn_symbol">}</span>
<span class="l_atom">run_callback</span> (<span class="l_atom">func</span>) <span class="l_fn_symbol">-&gt;</span>
<span class="l_builtins">print</span> <span class="l_string">"running callback..."</span>
<span class="l_atom">func</span><span class="l_symbol">!</span>
<span class="l_comment">-- this will not work:
</span><span class="l_comment">-- the function has to no reference to my_object
</span><span class="l_atom">run_callback</span> <span class="l_atom">my_object</span><span class="l_bold_symbol">.</span><span class="l_atom">write</span>
<span class="l_comment">-- function stub syntax
</span><span class="l_comment">-- lets us bundle the object into a new function
</span><span class="l_atom">run_callback</span> <span class="l_atom">my_object</span><span class="l_symbol">\</span><span class="l_atom">write</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">my_object</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
<span class="l_atom">value</span> <span class="l_symbol">=</span> <span class="l_number">1000</span>,
<span class="l_atom">write</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">self</span>)
<span class="l_keyword">return</span> <span class="l_builtins">print</span>(<span class="l_string">"the value:"</span>, <span class="l_atom">self</span><span class="l_symbol">.</span><span class="l_atom">value</span>)
<span class="l_keyword">end</span>
<span class="l_symbol">}</span>
<span class="l_atom">run_callback</span>(<span class="l_fn_symbol">function</span>(<span class="l_atom">func</span>)
<span class="l_builtins">print</span>(<span class="l_string">"running callback..."</span>)
<span class="l_keyword">return</span> <span class="l_atom">func</span>()
<span class="l_keyword">end</span>)
<span class="l_atom">run_callback</span>(<span class="l_atom">my_object</span><span class="l_symbol">.</span><span class="l_atom">write</span>)
<span class="l_atom">run_callback</span>((<span class="l_fn_symbol">function</span>()
<span class="l_keyword">local</span> <span class="l_atom">_base_0</span> <span class="l_symbol">=</span> <span class="l_atom">my_object</span>
<span class="l_keyword">local</span> <span class="l_atom">_fn_0</span> <span class="l_symbol">=</span> <span class="l_atom">_base_0</span><span class="l_symbol">.</span><span class="l_atom">write</span>
<span class="l_keyword">return</span> <span class="l_fn_symbol">function</span>(<span class="l_symbol">.</span><span class="l_symbol">.</span><span class="l_symbol">.</span>)
<span class="l_keyword">return</span> <span class="l_atom">_fn_0</span>(<span class="l_atom">_base_0</span>, <span class="l_symbol">.</span><span class="l_symbol">.</span><span class="l_symbol">.</span>)
<span class="l_keyword">end</span>
<span class="l_keyword">end</span>)())</code></pre></td></tr></tbody></table>
<h2><a name="the_using_clause_controlling_destructive_assignment"></a>The Using Clause; Controlling Destructive Assignment</h2>
<p>While lexical scoping can be a great help in reducing the complexity of the
@ -1487,7 +1541,7 @@ the following snippet:</p>
<span class="l_keyword">local</span> <span class="l_atom">my_func</span>
<span class="l_atom">my_func</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>()
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_number">10</span>
<span class="l_atom">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_builtins">print</span>(<span class="l_atom">i</span>)
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_atom">i</span> - <span class="l_number">1</span>
<span class="l_keyword">end</span>
@ -1553,8 +1607,8 @@ accessed, they just cant be modified:</p>
<p>Upon installing MoonScript, a <code>moonscript</code> module is made available. The best
use of this module is making your Luas require function MoonScript aware.</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">require</span> <span class="l_string">"moonscript"</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_atom">require</span>(<span class="l_string">"moonscript"</span>)</code></pre></td></tr></tbody></table>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_builtins">require</span> <span class="l_string">"moonscript"</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">require</span>(<span class="l_string">"moonscript"</span>)</code></pre></td></tr></tbody></table>
<p>After <code>moonscript</code> is required, Luas package loader is updated to search for
<code>.moon</code> files on any subsequent calls to <code>require</code>. The search path for <code>.moon</code>
@ -1615,8 +1669,8 @@ Lua code from MoonScript code.</p>
<p>Here is a quick example of how you would compile a MoonScript string to a Lua
String:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">require</span> <span class="l_string">"moonscript.parse"</span>
<span class="l_atom">require</span> <span class="l_string">"moonscript.compile"</span>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_builtins">require</span> <span class="l_string">"moonscript.parse"</span>
<span class="l_builtins">require</span> <span class="l_string">"moonscript.compile"</span>
<span class="l_keyword">import</span> <span class="l_atom">parse</span>, <span class="l_atom">compile</span> <span class="l_keyword">from</span> <span class="l_atom">moonscript</span>
@ -1632,8 +1686,8 @@ String:</p>
<span class="l_comment">-- our code is ready
</span><span class="l_builtins">print</span> <span class="l_atom">lua_code</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_atom">require</span>(<span class="l_string">"moonscript.parse"</span>)
<span class="l_atom">require</span>(<span class="l_string">"moonscript.compile"</span>)
</code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">require</span>(<span class="l_string">"moonscript.parse"</span>)
<span class="l_builtins">require</span>(<span class="l_string">"moonscript.compile"</span>)
<span class="l_keyword">local</span> <span class="l_atom">parse</span>, <span class="l_atom">compile</span> <span class="l_symbol">=</span> <span class="l_atom">moonscript</span><span class="l_symbol">.</span><span class="l_atom">parse</span>, <span class="l_atom">moonscript</span><span class="l_symbol">.</span><span class="l_atom">compile</span>
<span class="l_keyword">local</span> <span class="l_atom">moon_code</span> <span class="l_symbol">=</span> <span class="l_string">[[(-&gt; print "hello world")!]]</span>
<span class="l_keyword">local</span> <span class="l_atom">tree</span>, <span class="l_atom">err</span> <span class="l_symbol">=</span> <span class="l_atom">parse</span><span class="l_symbol">.</span><span class="l_atom">string</span>(<span class="l_atom">moon_code</span>)
@ -1720,7 +1774,7 @@ THE SOFTWARE.</p>
</div>
<div class="footer">Generated on Mon Aug 29 08:50:21 2011; MoonScript v0.2.0-dev</div>
<div class="footer">Generated on Sat Oct 1 12:41:57 2011; MoonScript v0.2.0-dev</div>