分享

Josh Haberman: Static Lua

 quasiceo 2014-01-15

Static Lua

Following up on my last post, here's what I'd like to do someday (read: in the future. not now).

I'd like to create a language that is as much like Lua as possible, but adds just enough to make it static and type-safe. Specifically:


  1. all variables must be defined before use.

  2. all variables and function parameters must have a type specified. though it would support "auto" that can infer the type from the initializer. there would be typenames for all of lua's built-in types, and classes would have names too.

  3. there would be a "class" construct. just enough to let you specify the class name, parent class, maybe abstract interfaces you implement, and then a class definition inside curlies.

  4. inside a class definition, you would define your instance methods with the same syntax that you declare faux instance methods in Lua today.

  5. inside a class definition you can also put instance variables.

  6. class methods and instance variables can be public, private, protected, etc.

  7. objects would show up as a "userdata" in Lua, which would let you use them just like regular Lua faux classes, except that referencing nonexistent fields would throw an error.



The benefits would be:

  1. Static Lua and Lua could operate seamlessly.

  2. Static Lua would have much better performance because (1) type checks would be performed at compile time instead of runtime, (2) property accesses could be array offsets instead of hash table lookups, and (3) all types are known at compile time so the compiler could generate much more optimized code.

  3. Static Lua would have all the benefits of static languages, and Lua would have all the benefits of dynamic languages. But they would both be runtime-loadable, and could both be JITted using LLVM.



And to Java people who would say that I'm re-inventing the JVM, I would like to remind you that I happen to actually care about memory footprint, startup time (scroll all the way to the bottom to find Groovy), and having a lightweight (small, easy-to-distribute) runtime embeds nicely and plays well with others. Maybe the right implementation of Java would be what I'm looking for, but I'm not that interested in a joining a language community that has demonstrated for 15 years that it has no regard for such things.

I'm a die-hard minimalist, and will never be satisfied building on top of runtimes that do not have the same appreciation for minimalism. There's a reason zlib (for example) is absolutely everywhere you look today. It's the tiniest and most unobtrusive C library you could possibly imagine for doing compression. There is absolutely nothing about it that can make anybody say "it has great functionality, but [I only have 64k of RAM]/[I need to compress 1TB streams with much less than 1TB of RAM]/[I need to have control over all memory allocation]/[I need to be able to impose hard limits on its resource usage].". No matter what you're doing, zlib is there for you, because it imposes the absolutely minimal amount of requirements on whatever is embedding it.

I want the same to be true for Gazelle, my protobuf implementation, and pretty any software I'm writing for the ages.

7 comments:

  1. I'd like to see more languages outside of the "purely" functional space (Haskell, OCaml) using type inference. Typed, but no types to type. Boo and hAxe are the only imperative/oop language I know of that do this.

    Reply
  2. I second this. Explicit type declaration should be optional, not mandatory. Type inference isn't exactly new technology.

    Reply
  3. I wonder how much of your "static lua" could be implemented as a simple compiler or preprocessor that outputs Lua, sort of like Objective-J to JavaScript:
    http:///discuss/2008/12/08/on-leaky-abstractions-and-objective-j/

    It wouldn't help with optimization, but you could at least get some added compile-time safety without sacrificing run-time performance.

    Reply
  4. Hmm, how about Javascript? [Ducking...]

    Reply
  5. Alex ChachanashviliFebruary 21, 2009 at 3:31 AM

    I can appreciate your quest for a language that lies somewhere between lua and java. I was on a similar quest few years ago trying to embed a scripting language inside my app server, after much research I found lua to be the easiest to embed at a cost to programming style, however after using lua for a bit as an extension (an embedded script language) I found it to be just fine, since the function extensions I wrote almost forced structure since the data was already defined in C and merely exposed via lua. Since then I have grown to really like lua (but not as a standalone language).

    Java on the other hand, while not too difficult to embed, consumed almost 3x the entire memory footprint while executing a simple HelloWorld class. I come from an 8-bit world of 64KB with 8KB used for video, that kind of bloat offends me (not to mention WebSphere using almost 1GB of ram just to start empty).

    Java had so much potential in the 90s (when it was introduced), but when all those large corps started weighing in with their JSRs and 'enhancements' it quickly became the poster child for 'designed by committee'...bloatware.

    Anyhow, good read, glad there are other people out there who take memory usage seriously.

    Reply
  6. Tim Sweeney (Epic) says that type inference doesn't scale (to many types) in reference to Haskell on the last slide:
    http:///28kf2w

    Still, it seems like a nice idea for static typing.

    Reply
  7. It looks like someone implemented my exact suggestion from comment #3 above:

    http://www./objective-lua/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多