lua-users home
lua-l archive

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


On Fri, Feb 29, 2008 at 9:04 AM, steve donovan <steve.j.donovan@gmail.com> wrote:
I was thinking about this general problem the other
day: how does one explcitly specify type in a dynamic language? The
idea was that types are objects, and one builds them up with
expressions:
   string_or_table = choice(String,Table)
   string_or_nil = choice(String,Nil)
   Location = choice({X=Double,Y=Double},{Double,Double})

I like to reuse regular _expression_ constructs for this. Your examples are written in metalua:
"string or table"
"string or nil" (although you can define "option(string)" with "newtype option(x) = x or nil"
"newtype location = {X=number, Y=number} or {number, number}" (or if you prefer: "newtype location = {X=number, Y=number} or table(2, number)").

Basically, you can redefine the meaning of all operators in a type declaration context, and by default, "and" and "or" are defined as type intersection and union.

A more interesting question is how far one can push static type
inference. I had a brief affair with Boo, which is a Python-like
compiled language where the only type annotations are usually in
method signatures. Cool, but boy the compiler was slow!

Static type checking/inference is a serious burden on code writing. For it to be worth it, you'd better have very strong guarantees of soundness on checked code. In all sound mixed static/dynamic systems I've met, soon enough dynamism creeps in all parts of your code base, and the static type system doesn't know anything useful anymore about your code.

Qi is rather interesting, as an almost practical mixed static/dynamic system, but it's based on sequent calculus, so you'd better be a maths geek if you want to like it.

A final remark about type-checking: I think there are many cases where it makes sense to check plain Lua code, not only extended Lua. It's doable if type declarations are stored either in separate files, or in comments, or in plain Lua data; I've got a couple of related stuff in my heap of unpublished/unfinished hacks...

-- Fabien.