generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Lua Fallbacks

Previous Thread :: Next Thread 
AvengersCreed is not online. AvengersCreed
Joined: 08 Jan 2011
Total Posts: 484
22 Mar 2011 08:31 PM
Of special interest – and actually the main reason for introducing fallbacks – are the fallbacks for table access: in the statement x=a[i], if a[i] is nil, then a fallback is called (if set) and its return value used as the value of a[i]. This simple new feature allowed the programmer to implement different semantics for table access. In particular, one could implement several kinds of inheritance, the simplest one being single inheritance by delegation:

function Index (a,i)
if i == "parent" then - to avoid loop
return nil
end
local p = a.parent
if type(p) == "table" then
return p[i] - may trigger Index again
else
return nil
end
end

setfallback("index", Index)
This code follows a chain of "parents" upwards, until a table has the required field or the chain ends. With the "index" fallback set as above, the code below prints red even though b does not have a color field:

a=Window{x=100, y=200, color="red"}
b=Window{x=300, y=400, parent=a}
print(b.color)
There is nothing magical or "hard-coded" about delegation through a "parent" field. This is a programmer choice. She could use a different name for the "parent" field, or implement more complicated multiple inheritance by allowing the "parent" field to be itself a table of parents that are tried in order, or anything else.

Different fallbacks were called for the expression a[i] when a is not a table. There was a "gettable" fallback, triggered to get the value of a[i], as in x=a[i]; and a "settable" fallback, triggered to set the value of a[i], as in a[i]=x.

There are many possibilities for exploiting these table fallbacks; cross-language inheritance is a very powerful one: When a is a userdata value (a pointer in the host C program), table fallbacks can give the programmer transparent access to values in data structures residing in the host program.

Our decision not to hard-code any of those possible behaviors led to one of the main design concepts of Lua: meta-mechanisms. Instead of littering the language with lots of features, we provided ways so that the user could program the features herself, in the way she wants them, and only for those features she needs.

The fallback meta-mechanism allowed Lua to support object-oriented programming, in the sense that (several kinds of) inheritance (and also operator overloading) could be implemented. We even added a piece of syntactical sugar for defining and using "methods": functions can be defined as a:f(x,y,z) and a hidden parameter called self is added to a.f, in the sense that a call to a:f(10,20,30) is equivalent to a.f(a,10,20,30).

In May 1996 we released Lua 2.4. A main feature of this new version was an external compiler, called luac. This program pre-compiles Lua code and saves bytecode and string tables to a binary file. The format of this file was chosen to be easily loaded and portable across different platforms. With luac, programs can avoid parsing and code generation at run-time, which can be costly, specially for large, static programs such as graphical metafiles.

Our first paper about Lua [10] already anticipated the possibility of an external compiler, but we only needed it after Lua became widely used at TeCGraf and large graphical metafiles were written in Lua, as the output of graphical editors.

Besides faster loading, luac also allows off-line syntax checking and the protection of source code from user changes. However, pre-compiling does not imply faster execution because Lua chunks are always compiled into bytecodes before being executed – luac simply allows those bytecodes to be saved in a file for later execution.

luac is implemented in "client mode", that is, it uses the modules which implement Lua simply as a (polite) client, even though it does include the private header files to have access to the internal data structures that need to be saved. One advantage of this policy is that it helped to structure the implementation of Lua's core into clearly separated modules. In particular, it is now easy to remove the parsing modules (lexer, parser, and code generator), which represent 40% of the core code in Lua 4.0, leaving just the tiny module that loads pre-compiled chunks. This can be useful for tiny implementations of Lua to be embedded in small devices such as mobile devices or robots (Crazy Ivan, a robot that won RoboCup in 2000 and 2001 in Denmark, has a "brain" implemented in Lua).
Report Abuse
xSIXx is not online. xSIXx
Joined: 06 Aug 2010
Total Posts: 9202
22 Mar 2011 08:32 PM
nice copy and paste, I googled the exact same post.
Report Abuse
AvengersCreed is not online. AvengersCreed
Joined: 08 Jan 2011
Total Posts: 484
22 Mar 2011 08:33 PM
Why thank you you can tell its not mine :3
Report Abuse
lucas668 is not online. lucas668
Joined: 18 Jun 2008
Total Posts: 6183
22 Mar 2011 08:33 PM
Me thinks this be an OTer.
Report Abuse
Emess is not online. Emess
Joined: 01 Apr 2010
Total Posts: 13331
22 Mar 2011 08:34 PM
Cool story bro.
Report Abuse
AJtn12 is not online. AJtn12
Joined: 22 May 2009
Total Posts: 5527
22 Mar 2011 10:03 PM
He isn't an OT'er, he's an anti-OT'er. O_O
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image