lua-users home
lua-l archive

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

Jérôme VUARAND wrote:


Static typing enforce some level of code correcteness and that's the
aspect of it that I'm looking for. So the static typing system I'm
looking for should support user defined static types beyond lua
default ones, something like type inference à la Haskell.

I'd rather have a simple core language that does not have type
checking, because there will be times when you have to stand on
your head to override the checks :-)

What do you do about string data that may be interpreted as a

On a positive note, some run-time type checking on objects is
a good idea if it does not severely impact the performance of
the core language.

If you dive into the LuaSocket code, you'll see some
rudimentary run-time type checking on userdata which
is (in my opinion) quite elegant.

It even allows for objects to have states that restrict which
methods are valid on an object depending on its current state.

In udp.c, for example, you get:

int udp_open(lua_State *L)
    /* create classes */
    auxiliar_newclass(L, "udp{connected}", udp);
    auxiliar_newclass(L, "udp{unconnected}", udp);
    /* create class groups */
    auxiliar_add2group(L, "udp{connected}",   "udp{any}");
    auxiliar_add2group(L, "udp{unconnected}", "udp{any}");
    auxiliar_add2group(L, "udp{connected}",   "select{able}");
    auxiliar_add2group(L, "udp{unconnected}", "select{able}");
    /* define library functions */
    luaL_openlib(L, NULL, func, 0);
    return 0;

And then an operation such as send() only works if the udp object
is connected:

static int meth_send(lua_State *L) {
    p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1);

or setsockname() only works if the socket is unconnected:

static int meth_setsockname(lua_State *L) {
    p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);

but setoption() works on any state:

static int meth_setoption(lua_State *L) {
    p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);