Hacker News new | past | comments | ask | show | jobs | submit login

I'm not sure there is an easy answer for the question of why one language became more popular than the other but there some very interesting parallels that can be drawn between Lua and TCL. One thing that both have in common is that both languages were purposefully designed to be able to be embedded with other languages, such as C. But they do this in very different ways.

The problem of embedding one language inside another is how do you make code written in one language call functions and objects defined in the other language? For example, when an object is transferred from the scripting language to the host language, how does the garbage collector keep track of it? This isn't easy to do and in some languages it is quite cumbersome. For instance, in Python the C programmer has to manually increment and decrement the reference counts for the python objects that they interact with.

TCL solves this problem by making everything a string, taking the "everything is a string" mantra farther than any other programming language. Strings are easy to pass from one language to the other.

Lua went the opposite direction. Lua exposes a rich API that lets other languages interact with Lua. The API lets other languages create Lua objects and call Lua functions, as well as send C objects and functions to Lua. Lua is entirely designed around this API and even the core standard library is built using it, ensuring that every Lua feature is accessible to C code as well. One interesting example of this is exception handling. In most languages this is done using try-catch blocks. But that feature isn't easy to use when embedding one language inside another. How would you write try-catch blocks in C? Because of this, Lua has a different way to do exception handling. Instead of try-catch blocks, there is a "pcall" function that invokes a function and returns whether there was an exception or not. Function calls and if-then-else statements are things that are also available to other languages via the Lua API...

    local ok, err = pcall(function()
        something_that_may_throw_an_exception()
    end)
    if not ok then
        -- handle exception
    end



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: