Arguably the most notable of compiled-to-Lua languages is Moonscript. However, there are some parts in that language that I don't like.
- Whitespace significant. Subjective of course, some of you might like it.
- Parenthesis-less function calls. Makes some code more "DSL-like". Personally I don't care for it; IMHO it makes the code feel "naked" and can complicate the parser. I'm fine with Lua's own rules for omitting parenthesis.
- Removal of local variable declaration and replacing it with a `using` statement. It's a rather stupid alternative when Lua's own local is fine.
- Implicit returns in nearly all blocks. I prefer being explicit
Code: Select all
-- Local function declaration
local parse_args(defaults, args)
-- local variable declaration
res := {}
for k, v in pairs(defaults) do
res[k] = if v != nil then v end
end
return res
end
-- Local variable declaration
Person, Person.prototype := {}, nil
function Person.new(opts)
-- Piping operator sends the result of first expression to the next
'Created a new Person object!' |> print
tbl := parse_args({
name = '',
age = 0,
gender = nil,
}, opts)
return setmetatable(Person.prototype, tbl)
end
Person.prototype = {
-- Shorthand function declaration with self and return
:get_name = @name,
-- Longhand function declaration with self
:set_name(first_name, last_name)
@name = first_name .. last_name
end,
-- Shortcut for getters and setters. Useful when you don't need additional logic
get age,
set age,
get gender,
set gender,
:say_hello()
-- Embedded expressions in strings.
"Hi, my name is ${@name}" |> print
end,
-- Longhand function declaration with self parameter manually added
say_goodbye(self)
'Goodbye!' |> print
end,
:__index(key) = Person.prototype[key],
}
-- TEST
john := Person.new({
name = 'John Doe',
age = 24,
gender = 'male',
})
john:say_hello()
-- with expression.
jane := with Person.new() do
:set_name('Jane', 'Doe')
.age = 12 -- assignments are possible
:set_age(24)
:set_gender('female')
end
jane:say_hello()
john:say_goodbye()
jane:say_goodbye()
-- destructuring assignment
thing := {'hello', 'world'}
{a, b} := thing
print(a, b) -- "hello" "world"
pos := {x = 1, y = 3}
{.x, .y} := pos
print(x, y) -- 1 3
person := {name = "John Doe", occupation = "Programmer"}
{.name, job = .occupation} := person
print(name, job) -- "John Doe" "Programmer"
deep := {a = {b = {c = {d = {e = "duckbutt"}}}}}
{.a.b.c.d.e} = deep
print(e) -- "duckbutt"
import 'braces' from '__future__'
-- equivalent to
{.braces} = require '__future__'
-- vvv random additions with questionable usefulness vvv
-- TOML-inspired table syntax (?)
foobar := {
hello = 'Hello World!'
[package]
version = '1.2.3',
license = 'MIT',
[abc]
1, 2, 3,
content = 'What is love?'
[abc.def]
4, 5, 6,
content = 'Baby don\'t hurt me'
[abc.def.ghi]
7, 8, 9,
content = 'Don\'t hurt me, no more'
}
Edit: I just realized this looks very similar to my other post "Language that compiles to Lua Bytecode". This post deals with languages that compile to Lua source code instead.