lua-users home
lua-l archive

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


One of the problems I've been thinking about recently is type-checking in
Lua. The main reason is that I'm trying to persuade my employer to use Lua
for application programming. This might sound mad, but the context is that
we have a large (~1M lines) C code base with an API that does not support
good productivity (neither the API, nor the fact that it's in C), and it's
impractical to reimplement, so building a better API on top in Lua seems 
like a way to make application programming more productive, fun and the 
system more modular (as the C hidden by the Lua API can now be 
re-engineered).

The biggest disadvantage of Lua over C is the lack of static type-checking 
(there are few other disadvantages I could think of; we write database 
admin systems for hospitals, so the system spends most of its time waiting 
for the user and the rest waiting for the disk; the performance of the Lua 
code is not an issue).

Now, it's possible to typecheck assignments to globals easily: simply tag 
the object, and set appropriate getglobal and setglobal methods. Locals I 
can't think of a way of typechecking, but that problem will largely 
disappear in Lua 5, when you can have nested global tables. The one 
remaining problem is checking that the arguments to functions are correct. 
It's easy to do this simply by checking them at the top of the function, 
but this isn't nice to write. However, you can easily write something 
like:

function fun (typeList, func)
  return function (...)
           local len = getn (typeList)
           if len ~= getn (arg) then error...
           for i = 1, len do
             if tag (arg[i]) ~= typeList[i] then error...
           end
           return call (func, arg)
           end
end

which takes a function and generates the appropriate checking code, like 
this:

f = fun ({Int, String},
function (i, s)
  print s .. " " .. tostring (i) 
end)

so that it's a bit nicer to write.

Has anyone else tried something like this?

-- 
http://www.mupsych.org/~rrt/
L'art des vers est de transformer en beautés les faiblesses (Aragon)