lua-users home
lua-l archive

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

LuaSub gives '-sjacket' for making runtime type checks, and it works like this:


local function mydear( t :string_or_table, i :int, j :[int], n : [number] )
	    : string, table, [int]
		...function body with returns...

't' is either string or table (checked by 'assert.string_or_table()')
'i' is integer ('')
'j' is an optional integer (nil or fulfilling '')
'n' is ...

The point is that any 'xxx' can be used to forward to '' as an assertion for such a type.

'assert', in itself, is a callable table, having some default assertions s.a. '.table', '.number', '.int' and so on.

One can also do more, like ': range(-1,3)' but that's not relevant.

This does _not_ do static checking, but could be the first step towards such analysis. I believe Fabien has some experience in this as well, and a similar notation.

The ': string, table, [int]' are of course the return value assertions.

For real world tests, try out luaSub at:


Alex Davies kirjoitti 28.2.2008 kello 19:49:

I'm sorry to say but you'll be disappointed. Lua currently has few keywords, and there is great hesitation before adding a useful one. See the recent multidozen reply continue thread for an example. To add a keyword that does nothing, and will (almost definitely) never do anything, would be very counter-lua.

So why not just use metalua? You could easily add a keyword which acts as a single line comment in that. Or if you prefer regular Lua, why not just use a comment? Your editor might even allow colouring --typedecl differently to regular comments, with enough tweaking.

- Alex

----- Original Message ----- From: "Norman Ramsey" < >
To: <>
Sent: Wednesday, February 27, 2008 4:05 AM
Subject: another hat in the ring regarding Lua 5.2: static typing

I dream of one day being able to do static type inference
on my Lua programs, thereby catching innumerable silly errors.
I worked on this problem one summer with Inna Zakharevich, and
it's not clear that we will ever get anywhere, but I would
dearly love it if Lua 5.2 contained some syntactic hooks
that could be used to write down type definitions and
to write down the types of some variables.  Since we have
no idea what we want, I'd simply ask for a syntax

typedecl string-literal

which the current compiler would ignore.

I realize the same effect could be achieved at present by simple

function typedecl() end

but (a) that would have a small run-time cost and (b) I'm really
asking the Lua team to pick a keyword and reserve it for future use.