views:

521

answers:

3
+5  Q: 

Strongly typed Lua

I am looking for a Lua front-end compiler that is strongly-type, but outputs standard Lua 5.1 byte-code (that has no notion of types at all). What I want is a decent amount of static, compile-time syntactic analysis and optional typing, to detect trivial errors sooner than run-time. The resulting byte-code would have to play nicely with existing Lua byte-code that was compiled with the standard LoadString().

To be clear -- any difference would only occur at byte-compilation time. At runtime, the byte code would have no idea that anything different/unusual happened to it during the compile phase.

What I have in mind sounds a lot like ActionScript; I wouldn't even mind an ActionScript compiler that outputs Lua byte code!

Has anyone heard of such an effort? I've seen some references to using MetaLua to do this, but honestly I am not bright enough to make heads of tails of their documentation

+3  A: 

There is no such thing. It may be possible to extend MetaLua to do this but nobody has done it, and AFAIK, there are no plans to do so. Lua is meant to be a dynamic language, if you want a statically typed language, use one.

What you are essentially looking for is something like Java or C#. In that case, you could use a project like Lua.NET to integrate existing Lua code with C#. There is also Kahlua for Java.

Zifre
I am not looking for LUA.NET. LUA.NET implies some sort of .NET runtime. The .NET runtime is nothing like the LUA ByteCode VM. What I am looking for would only apply strong-typing at byte-compile time, and then run the resulting code blissfully unaware that anything unusually happened to it at compile time.
Armentage
Unfortunately, I am already working with a large (and growing) LUA code base. Due to LUA's nature, changing any existing library code is pretty much impossible. An *optional* strong-typing front end would solve this problem, without necessarily changing the basic feel of the language.
Armentage
@Armentage, Lua is not an acronym, it is a proper noun. It is written "Lua", not "LUA". See http://www.lua.org/about.html#name for the official story.
RBerteig
@Armentage: If you want to pursue this in the fall I might be able to find a student who would be interested.
Norman Ramsey
We had an intern at my firm build something like this over the summer. Results were interesting... his mentor actually had him build his type-safe lua pre-processor in Haskell...
Armentage
+4  A: 

In the summer of 2005 or thereabouts, I worked with an incredibly smart undergraduate student on the problem of doing some compile-time type inference for Lua, possibly assisted by annotations. This problem turns out to be incredibly hard! (My student wrote a short technical note, but it's not really intended for general circulation.)

If I wanted to solve the problem you have posed, with the twin constraints that it allow significant static type checking and that it interoperate with standard bytecode-compiled Lua code, I would design a new language from scratch to satisfy these two constraints. It would be a substantial amount of work but significantly easier than trying to retrofit a type system to Lua.

Norman Ramsey
I hear what you're saying; I've been putting a lot of thought into this, and while it seems very simply to cover the most basic cases (i.e. catching local x:int = "hello" as an error) things get very difficult when you start worrying about tables or returning typed tuples from functions. But those guys at Adobe figured it out with Javascript!
Armentage
Table and the infinite number of ways they are used is definitely the problem. If you're willing to have lots of annotations things probably get simpler.
Norman Ramsey
Norman, knowledge should be shared. I for one would be very interested in learning what were the conclusions of your study!
+1  A: 

Please see this Metalua blog post.

-{ extension "types" }

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

This is looks like a run-time solution though.

Anyway, feel free to ask your question in Metalua mailing list. If you want to extend Lua syntax, Metalua is the first tool to look at.

P.S. Please never write Lua as all-caps!

Alexander Gladysh