Luan / Documentation reactionary software by fschmidt

How Luan differs from Lua

This document explains how Luan differs from Lua as described in the Lua 5.3 Reference Manual.


Contents


Introduction

contents

Lua is one of the simplest languages available, but Luan is even simpler. This means Luan removes more than it adds. Most of what is added is added in the library, not in the language itself.

Luan is implemented in Java and is tightly integrated with Java. This makes it an excellent scripting language for Java.

Basic Concepts

contents

Values and Types

contents

Luan adds a binary type that Lua doesn't have. This is because Lua strings can represent binary while Luan strings cannot.

The Luan Nil type is implemented as the Java null. The Luan Boolean type is implemented as the Java Boolean type. The Luan Number type is implemented as the Java Number type. The Luan String type is implemented as the Java String type. Actual numbers may be any subclass of the Java Number class.

Luan functions may be written in Luan or may be wrappers around native Java methods. Any Java method may be called as a Luan function.

The Luan java type is a replacement for Lua's userdata. A Luan java value is nothing more than a Java object that doesn't fall into one of the other recognized types.

The Luan binary type is the Java byte[ ] type which is an array of bytes.

The Luan table type is just like its Lua equivalent, but implemented in Java.

Luan does not have the Lua thread type which aren't actually threads but in fact are coroutines. Luan has real threads. This is particularly valuable for web serving where each request is handled by a thread. But thread synchronization is too complicated for application programmers. So Luan makes mutable objects immutable when they become accessible by multiple threads. This eliminates the need for thread synchronization. If there is a need to share mutable state across threads, there are special functions for this.

Environments

contents

Luan has no global environment at all, no _G. By default, Luan doesn't define _ENV either, but if you define it as a local table in a chunk, then it acts like it does in Lua. When _ENV isn't defined, there are no global variables and an unrecognized variable name produces a compile error.

Every module is initialized with one local function: require. The module then uses this function to get access to whatever else it needs.

Error Handling

contents

Luan has the function error but does not have pcall or xpcall. Luan adds the try statement instead. Luan errors are implemented as an error table, not as a message object.

Metatables and Metamethods

contents

Luan only has metatable for tables, not for other types.

Luan does not support the call metamethod. There is nothing that one can do with the call metamethod that can't be done more cleanly with closures, so this was left out.

Garbage Collection

contents

Luan uses Java garbage collection. Luan has no special garbage collection methods.

Luan does not yet have weak tables but this will be added.

Coroutines

contents

Luan does not have coroutines. Coroutines is a complex concept that isn't needed in a simple language, so it was left out.

The Language

contents

Lexical Conventions

contents

Unlike Lua, Luan considers the end of a line to be the end of a statement. This catches errors and encourages readability. If you want to continue a statement on another line, you can use a backslash followed by a newline which will be treated as white space.

Luan has a similar set of keywords to Lua and has the same other lexical conventions.

Variables

contents

By default, there are no global variables and an undefined variable produces a compile error. To enable global variables, one must define _ENV. Avoiding global variables makes it much easier to catch errors at compile time.

Statements

contents

Luan adds the block terminators end_do, end_for, end_function, end_if, end_try, and end_while. These can be used to end the appropriate block type, but end can also be used to end any block.

Most statements in Luan are the same as Lua. Only those statements that differ will be listed here.

Control Structures

contents

The Luan if, while, and repeat statement are the same as in Lua except that the condition expression must return a boolean value. Any other value type will produce an error. This helps catch errors and makes code more readable.

Luan adds the continue statement which is used inside loops.

Luan does not have a goto statement.

For Statement

contents

Luan has no numeric for statement. Luan only has generic for statement. Instead of the numeric for statement, Luan uses the range function in a generic for statement like this:

for i in range(from,to,step) do block end

The Luan generic for statement is simpler than the Lua version because Luan only uses an expression, not an explist. So a for statement like:

for var_1, ···, var_n in exp do block end

is equivalent to the code:

do local f = exp while true do local var_1, ···, var_n = f() if var_1 == nil then break end block end end

Try Statement

contents

Unlike Lua, Luan has a try statement. See Try Statement in the Luan Reference Manual. This also eliminates the need for Lua's pcall function which Luan doesn't have.

Logical Statements

contents

Unlike Lua, Luan allows or and and expressions to be stand-alone statements. This is useful in cases like this:

x==5 or error "x should be 5"

Template Statements

contents

Template statements are a Luan addition that don't exist in Lua. See Template Statements in the Luan Reference Manual.

Expressions

contents

Coercions and Conversions

contents

Unlike Lua, Luan does not do automatic conversions of strings to numbers.

Bitwise Operators

contents

Bitwise operators appear to be a new addition to Lua 5.3 and didn't exist in Lua 5.2. Luan does not support bitwise operators, but these can be added if there is a need.

Logical Operators

contents

The only change in Luan is that not must take a boolean argument. This helps catch errors and makes code more readable.

Concatenation

contents

Unlike Lua, Luan converts all concatenation operands to strings.

Table Constructors

contents

Unlike Lua, Luan considers an end_of_line to be a field separator in a table constructor.

Function Calls

contents

Unlike Lua, Luan does not allow extra non-nil arguments to be passed to a function. In Luan, this causes an error. This change helps find coding mistakes that would be very hard to detect otherwise.

Luan does not support Lua's v:name(args) style object-oriented function call. Object oriented programming is done in Luan using closures, so this feature is not needed.

Luan doesn't support proper tail calls. Because Java doesn't support this cleanly, this was left out.

Backticks

contents

Backtick expressions are a Luan addition that don't exist in Lua. See Backticks in the Luan Reference Manual.