lua-users home
lua-l archive

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

There's some dynamic type checking offered with metalua with -{extension 'types'}. A description of a previous version can be found at

The type description language is fairly rich, with type unions and intersections, parametric and dependent types, intuitive syntax for structures, etc. Types are attached to variables, not values, i.e. if you re-assign a new value to a variable, the value's type will be re-checked. It can obviously be switched off, with -{stat: types.enabled = false}. It also has a namespace handling mechanism, so that you can define as many type declarations, functors etc. as you want without fear that it will interfere with "real" code (type definitions are in;f=src/lib/extension/types-runtime.mlua).

I've also started working on static type inference with a subset of Lua, but that's more for fun than with real hopes for production use: a static Lua would not be Lua anymore, it would rather feel like a weird cousin of Scala. The main interest of that calculus would rather be to experiment with some academic ideas, by making them much cheaper to implement.

Another track, which seems most promising to me, would be to design some super-lint: static code analysis that raises warnings on suspicious stuff, yet lets you compile them, advises you to protect this and that with dynamic checks, etc. The kind of tool taht would go in a pre-commit hook. I think that static type inference reached its optimal helpfulness with Haskell, we need to explore other approaches if we hope to ever do better. Almost-trivial checks like global vars usage and type-checking of native functions would be an easy first step.

You can play and add typing errors in;f=src/samples/types_test.mlua to see how they are caught:
-{ extension "types" }
-{ extension "clist" }

-- Uncomment this to turn typechecking code generation off:
-- -{stat: types.enabled=false}

function sum (x :: table(number)) :: number
local acc :: number = 0
for i=1, #x do
acc = acc + x[i]
return acc

x = { i for i=1,100 }
y = sum (x)
printf ("sum 1 .. %i = %i", #x, y)