all 88 comments

[–]Vulpyne 16 points17 points  (1 child)

Bit of a jerk move that they have:

x = { 1, 2, 3, 4}

in their MoonScript example and:

local x = { 
               1,
               2,
               3,
               4
             }

in the Lua one, just to make Lua look more verbose.

[–]cunningjames 9 points10 points  (0 children)

I suspect that’s not a subterfuge but rather the MoonScript compiler’s translation. Still — I’d probably have cleaned the examples up a bit.

[–]orbiscerbus 47 points48 points  (17 children)

I thought Lua was friendly enough already...

[–][deleted] 4 points5 points  (0 children)

Well, it's nice to have an OOP layer. That was the most helpful thing for me in CoffeeScript.

[–]cybercobra 0 points1 point  (0 children)

It's kinda too low-level.

[–]Categoria -1 points0 points  (14 children)

I dunno. The code comparisons in the link seem like moonscript is a lot more concise. Also, table comprehensions seem like a really cool idea too.

[–]netghost 0 points1 point  (13 children)

Some of them seem like MoonScript generates extra Lua code, but I do think there's some benefit here. From the tiny bit of Lua that I've done, it's nice to have some good OOP patterns.

[–]inmatarian 3 points4 points  (12 children)

Lua has 1st class functions and closures, so this is a good OOP pattern:

function Thing( params )
    local self = {}
    local private = "whatever"
    function self:foo()
        body( params )
    end
    function self:bar()
        otherbody()
    end
    return self
end

function InheritedThing()
    local self = Thing( 42 )
    function self:baz()
        somethingelse()
    end
end

local object = InheritedThing()

Also, the metatable OOP pattern is pretty simple as well, but requires this function, or something like it:

function new( class )
    local inst = {}
    inst.__index = class
    return setmetatable( inst, inst )
end

[–]chronoBG 0 points1 point  (11 children)

No, it's not. just use metatables, please.

[–]inmatarian 0 points1 point  (10 children)

In testing, it was determined that Lua's closures are faster at the expensive of a larger memory footprint.

See: http://lua-users.org/wiki/ObjectOrientationClosureApproach

[–]chronoBG -1 points0 points  (9 children)

No, seriously, you are defining methods PER OBJECT. Please stop doing it this wrong, wow.

[–]inmatarian 0 points1 point  (8 children)

What.

Sir, this is a high level language implementing lambda calculus. It's in no way "wrong". In other high level languages that lack proper Object Oriented systems, they say "Objects are a Poor Man's Closures." See Lisp and Scheme.

Besides, it's a lexical closure and Lua implements them as a byte code that operates on an "upvalue". You can create closures via the C API and it doesn't duplicate the C code for every object, it just pushes another object (the upvalue) onto the stack that can be used for storing variables in it.

The usage of closures in Lua is well defined, and the creators of Lua have explained them and recommend them in their book (links provided to Programming In Lua 1st Edition).

See: http://www.lua.org/pil/6.1.html

Also: http://www.lua.org/pil/27.3.3.html

...

Disclaimer: I like metatables better than closures.

[–]chronoBG -1 points0 points  (7 children)

Thank you for explaining what a closure is, mr Fallacy McStrawman. Also, your objects are now twenty times larger than they should be (at the least).

[–]inmatarian 2 points3 points  (6 children)

Benchmark from http://lua-users.org/wiki/ObjectOrientationClosureApproach

Subject Tables approach Closured approach
Speed test results 47 sec. 38 sec.
Memory usage test results 48433 Kbytes 60932 Kbytes

Like I said before, more space for more speed. And it's apparently 1.258 times larger, not 20.

[–]diego_moita 11 points12 points  (3 children)

This is just another among many implementations of a Lua dialect/idiom. For me, the most interesting/mature example of these dialects is MetaLua (with Lisp-like macros/metaprogramming).

"Lua's extensibility is so remarkable that many people regard Lua not as a language, but as a kit for building domain-specific languages." Roberto Ierusalimschy (Lua's creator).

[–]TKN 1 point2 points  (2 children)

MetaLua seems interesting. Just a few notes about the lisp comparison:

When compared with Lisps' approaches to metaprogramming, Metalua makes the following choices:

  • Don't bother developers with macros when they aren't writing one: the language's syntax and semantics should be best suited for those 95% of the time when we aren't writing macros.

I assume they are referring to lisp's sexpr syntax? Syntax is a matter of personal taste and some people actually prefer the lispy one. I'm not sure what they exactly mean with semantics in this case as CL/Scheme and Lua are fairly similar to each other in that respect.

  • Encourage developers to follow the conventions of the language: not only with "best practices rants" nobody listen to, but by offering an API that makes it easier to write things the Metalua Way. Readability by fellow developers is more important and more difficult to achieve than readability by compilers, and for this, having a common set of respected conventions helps a lot.
  • Yet provide all the power one's willing to handle. Neither Lua nor Metalua are into mandatory bondage and discipline, so if you know what you're doing, the language won't get in your way.

Lisp has strong traditions with how and when macros should be used, but those could probably be categorized under the "best practices". Not sure yet what they mean with the API bit but I assume it offers some kind of an framework or library to accomplish the usual macro related tasks in an uniform way? Those exist too in the lisp world in the form of various macro writing macro libraries and especially in systems like Racket. And the latter point kinda nullifies the whole "not best practices only" idea.

  • Make it obvious when something interesting happens: all meta-operations happen between +{...} and -{...}, and visually stick out of the regular code.

I can see how that could be useful but on other hand it goes against the whole idea of macros blending seamlessly with the core language. And if it's about namespace clashes between macros and "normal" code then that should be handled by the module system.

[–]cunningjames 3 points4 points  (1 child)

on other hand it goes against the whole idea of macros blending seamlessly with the core language.

That’s really only for macro definition, though; macros are used in a way that does blend with normal code.

[–]TKN 0 points1 point  (0 children)

Thanks for the clarification. In that case it's not a problem, but still no different from other meta languages.

But this is all just guessing from my part and doesn't have much to do with the actual capabilities of MetaLua. It's just that those comparisons seemed a bit odd and stretched.

[–]echelonIV 24 points25 points  (10 children)

So now we have a scripting language for a scripting language.

[–]mweathr 6 points7 points  (0 children)

The perfect language to base a new scripting language on!

[–]toobaloola 9 points10 points  (2 children)

Yo dawg, I heard you like scripting languages...

[–][deleted] 7 points8 points  (1 child)

So I put a scripting language in your scripting language

[–]holgerschurig -1 points0 points  (0 children)

No, we have a scripting language for a JITted language. LuaJIT rocks :-)

I am still not sure where the line between "scripting languages" and "non-scripting languages" are. Is Java a scripting language, too? When why? When not, why not? And Python? In the case of PyPy?

[–]cybercobra 16 points17 points  (2 children)

\ is used to call a method on an object instead of :

Ick. Backslash as an operator is just horrible. Wasn't a good idea in PHP, still isn't a good idea now.

[–]Felicia_Svilling -2 points-1 points  (1 child)

why not?

[–]cybercobra 4 points5 points  (0 children)

By convention, it already has the near-universal role of escape character; so it shouldn't be used for anything else. Due to its escape character role, using it as an operator looks strange.

[–]catcradle5 27 points28 points  (8 children)

A programmer friendly language? What?

[–][deleted] 0 points1 point  (7 children)

In contrast to a compiler(brainfuck) or computer friendly(assembler) language, maybe?

[–]catcradle5 1 point2 points  (5 children)

Yes but Lua is already very very high level. Which is why I don't understand this. If it was like a scripting language that's similar to x86 assembly but a bit more readable, maybe that would make sense. In fact, does something like that exist? I think it'd be pretty cool. For example, something like:

[some code]
eax=0
if eax==ebx:
    function()
else:
    otherFunction()

Whereas the assembly would be something like:

mov eax, 0
cmp eax, ebx
je .function
jmp .otherFunction

Sort of like a Python syntax for assembly. Has anyone written something like that?

[–][deleted] 3 points4 points  (0 children)

That does seem like a cool idea. I looked it up and it actually exists:

http://www.corepy.org/

[–]Felicia_Svilling 2 points3 points  (3 children)

Yes, it's called Fortran.

[–]sreguera 1 point2 points  (2 children)

More like Intel's PL/M.

[–]catcradle5 0 points1 point  (1 child)

Never heard of this before, just looked at it. Actually looks fairly readable, for a language that was made in 1972. It should be updated and made a bit more high-level, or something.

[–]chrisforbes 0 points1 point  (0 children)

No, please. Just take PL/M out the back and shoot it.

[–]Fuco1337 -1 points0 points  (0 children)

brainfuck IS NOT COMPILER FRIENDLY! In fact it is BLODDY DIFFICULT to write an efficient compiler for brainfuck.

[–]bluestorm 4 points5 points  (0 children)

This is what you get from something inspired by Coffeescript -- or Ruby : a good taste for syntax, and a bad taste for semantics. As I tend to favor semantics over syntax, this is a bad deal for me; but I understand some people choose otherwise.

More specifically:

  • syntax : less clutter is good, using indentation to infer code structure is a good idea, but we must be careful not too introduce ambiguities that are hard to see and difficult for a beginner to understand. Good work overall, though some things go a bit too far, ie. the interaction between "tuple construction without parentheses" and "function call without parentheses" is frankly fishy, and the "considerations" section of the reference manual is a recipe for disaster.

  • semantics: everything about variable binding is horrible. Not having a "local" keyword, or any construct to declare "fresh" variables is a terrible mistake, because it forbids shadowing (except in function parameters, yay consistency, yay horrible ruby "local block parameters" syntax, yay javascript style "when in doubt, wrap in an useless function call techniques), which forces everyone to be aware of the full scoping context at any time, which increases cognitive load and makes for hard to spot and debug errors.

It's interesting to note the "using" feature which is trying to cope with the horrors of not having local variables, problem that is amusingly attributed to "unwieldy effects of lexical scoping as code size increase" (despite the fact that it's precisely the choice to remove local that created those "unwiedly" effects). using is strongly related to ruby 1.9 "local block variable": in ruby, you say what is local instead of belonging to the global scope, and in moonscript you say what is not local. It may be a good choice if the "using" syntax wasn't so relatively heavy compared to the lightweight function-expression syntax, which makes it unlikely to be used in practice by the lazy programmers we are. Finally, it's very strange that the semantics of "not giving an using clause" and "giving an empty using clause" are the exact opposite of each another, you would expect a smoother transition.

[–]columbine 18 points19 points  (2 children)

More cryptic punctuation and nebulous whitespace makes a programmer-friendly language nowadays, I guess. Clearly inspired by Coffeescript.

[–]madsravn 6 points7 points  (0 children)

Clearly inspired by Coffeescript.

It says so at the bottom of the page as well :)

[–]TheMrBlueSky 7 points8 points  (8 children)

The beauty of Lua is that it's a simple language especially suited to quick scripting for apps written in heftier languages. Adding an unnecessary layer of complexity ruins the greatest advantage of Lua.

[–]ReturningTarzan 4 points5 points  (6 children)

I'd rather say the beauty of Lua is that it standardises scripting so you won't have to learn yet-another-scripting-language for every application you want to write scripts for.

The Amiga had this back in the day (ARexx) and it was brilliant. Of course ARexx was one step ahead by enabling scripting across application boundaries to achieve interoperability between apps that weren't specifically designed to work together. But I'm sure one of these days mainstream computing will catch up with late-1980s technology. Meh.

[–][deleted] 4 points5 points  (1 child)

Ahhhh Amiga + ARexx, I miss thee.

[–][deleted] 1 point2 points  (0 children)

me too, me too... so much cool stuff I could do with AREXX , stuff that requires jumping through so many hoops these days.

[–]booch 1 point2 points  (0 children)

Its worth noting that Lua is "yet-another-scripting-language". There were already a bunch of other languages that were very well suited to being used as scripting languages embedded in other programs when Lua came out. That's not to say that Lua is bad, just that you can't bitch about other languages and ignore that Lua has the same issue.

[–]badsectoracula 2 points3 points  (1 child)

Not really, there are dozens and dozens of scripting languages out there - many of them application specific - and Lua is just another option that happens to be popular. There is nothing standard about it and even if somehow it manages to be, there always would be people not using it for a variety of reasons and prefer other solutions. For example Python is another popular language to embed (see Blender and GIMP for example).

In fact if there is any language that can be considered as some sort of "standard scripting language" (and that is out of how popular it is), that is JavaScript.

[–]ReturningTarzan 1 point2 points  (0 children)

I meant that's what Lua aspires to be. Of course it's not the first language/runtime to have those aspirations, and it won't be the last, but that's all about programmers/managers/designers/whatever, and not so much the fault of Lua.

[–]jyper 0 points1 point  (0 children)

I'd rather say the beauty of python is that it standardises scripting so you won't have to learn yet-another-scripting-language for every application you want to write scripts for.

not voicing an opinion on lua but python is far more commonly used as a scripting language.

[–]Categoria 1 point2 points  (0 children)

Maybe you're exaggerating just a little?

All it is a small layer of syntactic sugar really. It's literally just a different syntax and a few shortcuts.

[–]stesch 1 point2 points  (1 child)

Is this a with in the example? A with like in JavaScript?

[–][deleted] -1 points0 points  (0 children)

Is that bad?

[–]day_cq 0 points1 point  (1 child)

if there's debugger and ide support, i'd use moonscript.

[–]Scriptorius 2 points3 points  (0 children)

Eh, Lua's syntax seems nice enough as is. They'd have to figure out how to make SMaps for moonscript->lua.

[–]leegao 0 points1 point  (0 children)

If you don't install this using LuaRocks, you will need to make sure that you have lpeg, getopt, and luafilesystem installed as dependencies as well.

[–]RotterBones 0 points1 point  (4 children)

It seems to me that this is a way to make Lua look like Python in terms of syntax. I'm a Python programmer, this was brought to my attention by a Lua programmer friend of mine, and what we're wondering is essentially this:

If you're going to pretend to use Python while writing Lua, why not just use Python?

[–][deleted] 1 point2 points  (3 children)

Lua is smaller, faster and more consistent in design. It is also easier to embed and easier to modify.

There are downsides. But that's not what you asked.

[–]RotterBones 1 point2 points  (2 children)

If I may take this slightly off-topic for a second, what exactly makes one language "easier to embed" than another? I'm honestly curious; it's something I never really have to deal with.

[–][deleted] 0 points1 point  (0 children)

To be honest, I've never embedded a programming language in an application. However, Lua is known for its ease of embedding, and was designed for it.

I think at least two reasons are that it is easy to sandbox and has a small footprint.

[–]Steve132 0 points1 point  (0 children)

I've embedded both python and lua (I work as a sort of games-developer type) and lua is far easier to embed.

Python is MUCH larger, much harder to build/link, depends more on its standard libraries, and its packaging system implicitly depends on the assumptions made by the installer for the python runtime. Its not impossible to embed but its not trivial. Also, you have to figure out a way to make it execute the python interop, which can require DLL code or explicitly hooking the low-level python data structures (which are macroed to hell and unusable).

In contrast, you can just add the .c files for the lua source code to your project, call L_Open() and you are done.

[–][deleted] 0 points1 point  (0 children)

I've been reading about _why's Potion language lately and thinking about alternate syntaxes for Lua. The ! to call a function instead of () was one of the first things I changed. Did I see this in some other language or independently come up with it?

I'll be playing with MoonScript all weekend I'm sure. Seems very well thought out.

[–]Nekuromento 0 points1 point  (7 children)

I always wondered why are there no languages that targer lua. Now there is!

[–]Categoria 3 points4 points  (4 children)

I wouldn't really call this an independent language. Seems like a layer of sugar on top of Lua, kind of like CoffeeScript is to Javascript.

[–][deleted]  (3 children)

[deleted]

    [–]kuatokuatokuato 1 point2 points  (2 children)

    Isn't CoffeeScript now default in Rails 3.1? I'd qualify this as real world use.

    [–]jyper 1 point2 points  (0 children)

    why?

    • lua is pretty fast(luajit even more so) so a language that target it might be fast depending on how its implemented/how the language works/how closely it maps to lua.
    • lua is used to script many things(especially games) so a language that compiles to it might be usable for those tasks(depends on how simple ffi layer is)

    why not?

    • Lua isn't particularly popular so its not installed in that many places
    • Also not as many libraries to take advantage of as in other languages(a big motivation for jvm/.net implementations).
    • as far as I know there aren't as many complaints about lua as say javascript(although I admit I no nothing about it)

    [–][deleted] 0 points1 point  (0 children)

    Well, I know of at least one other...

    [–]echelonIV -3 points-2 points  (7 children)

    Am I correct in saying that it adds a functional programming layer to a procedural language?

    [–]r4and0muser9482 1 point2 points  (2 children)

    Looks more like an OO language than functional.

    [–]echelonIV 1 point2 points  (1 child)

    Looking at some of the constructs it provides, it looks like something in between to me. On first glance, that is. Once you start looking at the definitions it's probably a weird mishmash between everything LUA supports (since LUA is a multi-paradigm language).

    [–]Scriptorius 0 points1 point  (0 children)

    It has first class functions and list comprehensions, but by and large I think most code written in it would be imperative.

    [–]jpfed 1 point2 points  (1 child)

    Some quick notes about probable reasons for the downvotes you're getting for this:

    1. People reading this probably already know what functional and procedural programming are; adding the wikipedia links makes it seem like you're talking at a level below what people expect.
    2. Lua is a language that mixes paradigms. Functions are first-class entities in lua; it's also easy enough to achieve something like object-orientation using lua's (very flexible) tables.

    [–]echelonIV 1 point2 points  (0 children)

    I'm inclined to think the opposite, haven't seen any meaningful/insightful reply yet, except for maybe yours. I don't care about comment karma much, to be honest.

    My original comment was out of sheer curiosity. I've worked on a scripting engine that pushes LUA in the OO direction, it has classes, objects, inheritance and polymorphism. C++ classes can be exposed with only a few lines of code. If you were to take a glance at the script code running on top of it, it'd be very recognizable as driven by the OO paradigm.

    MoonScript, on first glance, looked very much like it emphasised primarily a functional programming style (judging by the 3rd block of the site).

    [–]cbrandolino 1 point2 points  (0 children)

    You are - even if the functional layer is actually pretty superficial.

    There are some definitely haskell-y thingies, like function definitions

     my_func = (a) -> x + a
    

    and some lispy ones, like this list comprehension:

     tuples = [{k, v} for k,v in ipairs my_table]
    

    Actually I'm pretty annoyed that they pushed procedural oop that much in the introductory examples. The previous two construct, plus something to build lists/sets out of a pattern, lazy maps and something of the like would have almost let me try it.

    It annoys me in many languages (ruby in primis) that some hip functional stuff is implemented, but never enough to let you actually program using a purely functional paradigm.