lua-users home
lua-l archive

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


I'm coming to agree with Fabien that any meta-extension should be used
to add _new_ things to Lua, not confuse the issue with curly braces ;)

The proposal of the day: type annotations for function 'prototypes':

Consider this hypothetical syntax:

function F(String s, Number t)
	...
end

The actual code that the compiler sees is this:

function F(s,t)
   _assert_type(s,"string","s")
   _assert_type(t,"number","t")
  ....
end

where _assert_type() is:

function _assert_type(value,typestr,parm)
local t = type(value)
 if t ~= typestr then
   local msg = ("Argument '%s' expects a type of '%s', got
'%s'"):format(parm,typestr,t)
   error(msg,2)
 end
end

This is doubly useful: first, you can decorate your function
prototypes with useful type annotations. Second, you can dynamically
enforce that contract.

This all worked fine with the latest version of the token-filter macro
library; I'll post the code and some examples somewhere appropriate.

steve d.

PS. On the subject of whether Lua is up to the processing task, I
created a test file with 10,000 function definitions like this.
Parsing with the type-annotations macros took little over a second.
This seems to reflect the sheer numbers of lexical replacements
involved; a similar file with no type annotations was parsed in about
120ms.