lua-users home
lua-l archive

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



On Nov 11, 2008, at 8:05 PM, Sam Roberts wrote:

A simple property-based API is reasonable, too, and in a sense, the
control.value = 5 is more 'luaish", because it makes control look like
a table, the basic lua data structure.

This is also the API I have settled for over time: expose functionality using only Lua's metamethods (e.g. index, newindex, call, etc).

For example:

-- Create a TCP server, implemented in terms of meta:__call [1]
local aServer = TCPServer( '127.0.0.1', 1080 )

-- Assign an URL handler, implemented in terms of meta:__newindex [2]
HTTP[ '/hello(%a*)' ] = function( aName ) return ( 'Hello %s' ):format( aName or 'world' ) end

-- Run the server, implemented in terms of self:__call [3]

aServer( HTTP )

Cheers,

PA.

[1] http://dev.alt.textdrive.com/browser/HTTP/TCPServer.lua#L76
[2] http://dev.alt.textdrive.com/browser/HTTP/HTTP.lua#L565
[3] http://dev.alt.textdrive.com/browser/HTTP/TCPServer.lua#L126