Hacker News new | ask | show | jobs
by bmitc 744 days ago
Do you know if Lua supports type annotations or gradual typing? I use those heavily when using dynamically typed languages.
2 comments

It does not. The most viable solution is transpilation, teal is the most recent candidate.

You can have a hack of sorts using a schema and tables as in

local schema = { a = "number", b="string" } -- key = "type" local data, link = {a=4, b="default value"}, {} -- link -> data, link itself is always empty local tbl = setmetatable(link, { __index = data , __newindex = function (t,k,v) if schema[k] and type(v)==schema[k] then data[k] = v return true end error"type does not match schema" end })

tbl.b = 5 -- this will error because its the wrong type

There are certain solutions you can do with functions, lsp is also really good at telling you when you went wrong.

No, Lua does not, but check out Roblox's Luau which is a gradually typed extension of Lua.
I'd recommend teal because luau is not really lua.