[ACCEPTED]-Statically typed Lua-static-typing

Accepted answer
Score: 16

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

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

Score: 7

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 4 solution though.

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

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

Score: 6

This question is six years old... but here's 7 a new answer: http://terralang.org/

Like C, Terra is a simple, statically-typed, compiled 6 language with manual memory management. But 5 unlike C, it is designed from the beginning 4 to interoperate with Lua. Terra functions 3 are first-class Lua values created using 2 the terra keyword. When needed they are JIT-compiled 1 to machine code.

Score: 5

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

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

Score: 4

There is a new paper "Typed Lua: An 10 Optional Type System for Lua" from 9 PUC-Rio just published in Dyla'14. http://dl.acm.org/citation.cfm?id=2617553

It is 8 about "initial design of Typed Lua, an 7 optionally-typed extension of the Lua scripting 6 language". It's still in progress, and 5 the type system is still relatively simple. No 4 type inference/type checking tool provided.

Regarding 3 the metalua based typing system, Tidal Lock: optional 2 static type checking and inference for Lua 1 from Fabien. http://lua-users.org/lists/lua-l/2013-02/msg00403.html.

Score: 3

There is also Ravi https://github.com/dibyendumajumdar/ravi

Ravi Programming Language 4 is a derivative of Lua 5.3 with limited 3 optional static typing and LLVM and libgccjit 2 based JIT compilers

I really enjoy programing 1 in Terra (see above)

Score: 1

I recommend EmmyLua.

This is an Intellij/VSCode 6 plugin that supports typing documentation. I 5 found the overall documenting approaching 4 method to be very friendly. Also thanks 3 to its IDE support, EmmyLua also support 2 hinting.

Here is a little snippet for EmmyLua 1 doc:

--- @alias recipe_prototype any
--- @alias recipe_name string
--- @alias ingredient_name string

--- @class Coordinate
--- @field x number
--- @field y number

--- @class Entity
--- @field entity_number number unique identifier of entity
--- @field name string entity name
--- @field position Coordinate
--- @field direction any defines.direction.east/south/west/north

--- @class BlueprintSection
--- @field entities Entity[]
--- @field inlets number[] index of inlets in entities list
--- @field outlets number[] index of outlets in entities list

--- @type BlueprintSection
BlueprintSection = {}

--- @return BlueprintSection
function BlueprintSection.new()
    --- ...
end

--- @param other BlueprintSection
--- @param xoff number optional, x-offset of the other section, default to width of self
--- @param yoff number optional, y-offset of the other section, default to 0
--- @return BlueprintSection new self
function BlueprintSection:concat(other, xoff, yoff)
   -- ...
end

For more doc reference, check https://emmylua.github.io

More Related questions