lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mar 8, 2010, at 12:49 AM, Doug Rogers wrote:

> Wow, that's one crazy looking interface! Why bother with encode(t/s), write(file, t) or read(file) when you can use __call, __index and __newindex!

If you liked the above, you will positively love the following:

local DB = require( 'DB' )
local aDB = DB( 'sqlite3://localhost/test.db' )

for aContact in aDB( 'select * from contact' ) do
    print(, )

For reference, the plain LuaSQL equivalent:

require "luasql.postgres"
env = assert (luasql.postgres())
con = assert (env:connect("luasql-test"))
cur = assert (con:execute"SELECT name, email from people")
row = cur:fetch ({}, "a")
while row do
  print(string.format("Name: %s, E-mail: %s",,
  row = cur:fetch (row, "a")

Another inspirational example, a bare bone HTTP server:

local HTTP = require( 'HTTP' )
local TCPServer = require( 'TCPServer' )

local aServer = TCPServer( '', 1080 )

HTTP[ '/' ] = function() return 'Hello world' end

aServer( HTTP )

> It reminds me of the gyrations I went through when I discovered all the amusing ways to use C++ operator overloading. Lots of fun! Heck, in C++ some of it became part of std::.
> Other than the obvious fun, though, is there a benefit I've missed to having the implementation use __call, __index, and __newindex? The indexing doesn't make me think "writing/reading a file". Why not just use two named functions for the API? Brevity? Couldn't that also be achieved with local tread =
> Just curious.

I would blame Rici Lake's FuncTable for the initial inspiration:

Went downhill from there: how would it look like to not use verbs and instead limit oneself to Lua's basic table operators. In short, few verbs, many names. 

For your viewing delight, here is the result of all that handy work: