Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The terminology seems to be tripping you up (and I agree these terms aren't super clear). I think it's easiest to understand this through examples. Say you wanted to create a specialized function in pure Lua:

    function make_adder_func(n)
      return function(x) return n + x end
    end

    local add3_func = make_adder_func(3)
    local result = add3_func(6)

    -- Prints "9"
    print(result)
This totally works: because Lua supports both closures and first-class functions, the parameter "3" is captured in the environment of the inner function, so we have a specialized function for adding 3 to any other number.

Now the way Lua will execute this isn't super optimized. It treats "n" as a local variable which could theoretically be modified (it's not here, but proving that takes more of an optimizer than most dynamic languages possess). Also Lua doesn't even statically know the type of "n", so the "+" operation could be string concatenation, or even a user-defined function for the __add metamethod if "n" was a custom type. So even though it looks like the add3_func() is pretty specialized, it's actually not specialized at all in practice.

(For the record, LuaJIT is smarter about this and does have a trace compiler + optimizer that makes this extremely efficient).

But what Terra gives us is the ability to generate fully specialized and optimized functions at runtime. In Terra we could write:

    function make_adder_func(n)
      return terra(x : int) return n + x end
    end

    local add3_func = make_adder_func(3)
    local result = add3_func(6)

    -- Prints "9"
    print(result)
Unlike the Lua function, the Terra function is strongly typed and does not contain a closure over its environment. The inner Terra function isn't referring to the Lua variable "n", it is taking the value of n and making it a constant inside the Terra function. This allows the Terra function to be more thoroughly optimized (more like what LuaJIT would do).

So getting back to your question, the phrase "typed Lua is being generated" is a little confusing. What it means is that these Terra functions are strongly typed, and we're using Lua code to programmatically generate Terra functions, like in make_adder_func() above.



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

Search: