I noticed today that I missed this thread, so sorry for not replying earlier. I appreciate all the interest in Terra and am grateful for the Lua community for developing a simple and well-designed dynamic language! I think there were a few unanswered questions from the earlier thread:
Wesley Smith asked:
+ functions and operators can be overloaded and dispatched based on type (yes!!)
The operator overloading is documented a bit in our api reference (http://terralang.org/api.html#structs
). But we're still iterating on the design, so we don't have details. Function overload works similar to C, just define a Terra function twice:
terra add(a : int, b : int)
return a + b
terra add(a : int, b : int, c : int)
return a + b + c
add:printpretty() -- should list 2 definitions for "add"
+ how do you free memory allocated in Terra objects? It seems there's
a free() method??? I only found mention in passing in the paper but
don't see any example usage
Terra doesn't have any built-in library functions (yet). So for memory management, we use libc's malloc and free:
C = terralib.includec("stdio.h")
terra foo(N : int)
var a = [&int](C.malloc(N*sizeof(int)))
Grahm Wakefield asked:
I wonder if they have considered allowing type declarations to be optional (derived types); it seems like it wouldn't be too hard to add (and simply throw compile-time errors if the derivation doesn't succeed), since the coercion rules are evidently already present.
We do type propagation so not all types have to be annotated, just parameter types, and (for recursive functions) return types. We've considered doing something more sophisticated but there are a few complications. For instance, certain metamethods on "struct" types can be user-defined, which basically injects turing complete code into the type-checker. In the spirit of Lua, we also wanted to keep the implementation simple.
Also I wonder whether there can be multiple independent 'instances' of terra running, or if it is possible to remove/replace a function? This could make it rather attractive for interactive/live coding.
This would be cool! But we haven't done anything towards that goal yet since some of it is tricky to implement. For instance, replacing a function means having to track all the places where that function was inlined and forcing those functions to recompile as well. Eventually it would be great to incorporate live coding, since it would allow for more powerful IDE features such as code completion by examining the running Lua state.
If I a question, just ping me with it again and I'll do my best to answer it.