<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>t+1 &#187; lua</title>
	<atom:link href="http://blog.tplus1.com/index.php/category/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tplus1.com</link>
	<description>Programming, gardening, economics, life in Cleveland Heights</description>
	<lastBuildDate>Sat, 07 Jan 2012 21:12:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Lua metatable examples</title>
		<link>http://blog.tplus1.com/index.php/2007/10/03/lua-metatable-examples/</link>
		<comments>http://blog.tplus1.com/index.php/2007/10/03/lua-metatable-examples/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 21:21:42 +0000</pubDate>
		<dc:creator>matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[lua]]></category>

		<guid isPermaLink="false">http://blog.tplus1.com/index.php/2007/10/03/lua-metatable-examples/</guid>
		<description><![CDATA[Lua has exactly one data structure &#8212; tables.  And it uses those to implement everything else.
This is how inheritance works in lua:
t1 = {a = 3}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- t1 is a table with one name-value pair.
t2 = {}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-- t2 is an empty table.
setmetatable(t1, t2)&#160;&#160; -- t2 is t1&#039;s metatable.
t3 = {c = 5}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- t3 [...]]]></description>
			<content:encoded><![CDATA[<p>Lua has exactly one data structure &#8212; tables.  And it uses those to implement everything else.</p>
<p>This is how inheritance works in lua:</p>
<p><pre><code>t1 = {a = 3}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- t1 is a table with one name-value pair.
t2 = {}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- t2 is an empty table.
setmetatable(t1, t2)&nbsp;&nbsp; -- t2 is t1&#039;s metatable.
t3 = {c = 5}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- t3 is just another table like t1.

t2.__index = t3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- when a lookup fails in t1, t2 will look for a value
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- in t3.

print(t1.a)
print(t1.b)
print(t1.c)</code></pre></p>
<p>And the output is here:<br />
<pre><code>$ lua lua_fun.lua
3
nil
5</code></pre></p>
<p><a href="http://www.lua.org/pil/13.4.1.html">This page explains with more detail</a>.</p>
<p>When I first read this stuff, I wondered why I I couldn&#8217;t just make the metatable t2 be the place where t1 goes when a lookup fails, rather than require t3 to hold the defaults.  Then I realized that __index doesn&#8217;t necessarilly need to point to another table.  It could also hold a function, like this:</p>
<p><pre><code>-- Now, we&#039;ll change the way t2 handles failed lookups in t1 so that it always returns the key that was asked for.
t2.__index = function (t, k)
&nbsp;&nbsp;&nbsp;&nbsp;return k
end

print(t1.a)
print(t1.b)
print(t1.c)</code></pre></p>
<p>And now we get:<br />
<pre><code>3
b
c</code></pre></p>
<p>It is actually possible to make t2 be the metatable:<br />
<pre><code>-- What happens with this?
t2.__index = t2
t2.d = 6

print(t1.a)
print(t1.b)
print(t1.c)
print(t1.d)</code></pre></p>
<p>The results:<br />
<pre><code>3
nil
nil
6</code></pre></p>
<p>In conclusion, lua is neat.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tplus1.com/index.php/2007/10/03/lua-metatable-examples/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

