Lua Unofficial FAQ (u. FAQ)Maintainer: Steve Donovan (steve j donovan at gmail com), 2. Creative Commons License; Reproduction in any form is permitted as long as this attribution is kept. Language. 1. 1 Where to start? The official lua. FAQ is here, which is the place to go for essential information like Lua availability and licensing issues. There is also an incomplete Lua Wiki FAQ; this unofficial FAQ aims to fill in the holes and answer as many questions as possible, in a useful way. ![]() Lua is a modern dynamic language with conventional syntax: function sqr(x). This simplicity accounts for a lot of Lua's speed and compactness compared with languages of equivalent power. The Lua User's Wiki is full of useful example source and thoughtful discussion. This makes it a good choice for embedding as a scripting language in larger applications, but also is suitable for introducing programming concepts. Functions are first- class values and may be anonymous, so that functional styles can be learned. Proper closures and tail- call recursion are supported. It runs interactively so exploring the language and its libraries is easy. The instructions on this page are intended for advanced users or system administrators. General Information. The PATH is the system variable that your operating.Some comparisons with other languages can be found on the Lua Wiki. Suitability as a second programming language? One scenario is that you already have big C/C++/Java/C# (etc) applications and wish to let your users script those applications. Lua will only add about 1. K, which gets you a modern extension language which understands your special needs (like sand boxing) and can be easily integrated with your code. The other is that you have a lot of components which you use in your work flow and you are looking for a glue language to easily bind them together quickly without messy recompilation. The version that ships with Lua for Windows can also debug Lua code. In addition, it is scriptable using its own embedded Lua interpreter. There is a Lua plugin for Intelli. J IDEA. 1. 5 Lua seems very verbose. Why isn't it like C? TheINQUIRER publishes daily news, reviews on the latest gadgets and devices, and INQdepth articles for tech buffs and hobbyists. In Windows 7, when I start the Command prompt, is there any command to display the contents of an environment variable (such as the JAVA? What JexePack is: JexePack is a command line tool (great for automated scripting) that allows you to package your Java application (class files), optionally along. The compactness of C (and the Unix environment in which it grew up) comes from the technical limitations of very clunky teleprinters. It was designed for engineers who were not professional programmers and more likely to be used to FORTRAN. You can make your arrays start with zero, but the standard library functions (such as table. The first and fails and we pick up b anyway. Java applications need more platform information. One of the key design facets of Java Technology is that java programs execute within a virtual machine. Editor, IDE, Compiler; Java-Version; Installation des Java SE JDK 6 unter Windows; Installation des J2SE SDK unter SuSE Linux; Java-Programm compilieren und. Do you have the latest version of the Java Platform, Standard Edition Development Kit installed? Get it here. If you did download Java you should see a folder. Oracle Technology Network is the ultimate, complete, and authoritative source of technical information and learning about Java. Multiple values are understood: > print (false and 5 or 1. People will often write a convenience function: function choose(cond,a,b). This evaluates both values first, so it is suitable only for simple values, but it is bulletproof. How can undefined variable access be caught in Lua? By default, an unknown variable is looked up in the global or module table and its value will be nil. The easiest is to check dynamically whether a given global variable has been initialised explicitly or not; that is, even if it has been set to nil somewhere (see strict. Lua distribution.)The tests/globals. This provides semantic highlighting for Lua, so that for instance local variables are marked and can be visited, and unknown globals are flagged in red. Does Lua support Unicode? Yes and No. Lua strings can contain any characters (they are not NUL- terminated) so Unicode strings can be passed around without problems, as can any binary data. However, the existing string libraries assume single- byte characters, so a special library is needed; see slnunicode or ICU4. Lua. 1. 8 Optimization tips? The first question is, do you actually have a problem? Is the program not fast enough? Remember the three basic requirements of a system: Correct, Robust and Efficient, and the engineering rule of thumb that you may have to pick only two. Donald Knuth is often quoted about optimisation: . There is a definite cost in optimising that program, both in programmer time and in code readability. This can also give more space- efficient representation for arrays of C types, compared to using tables. When do I need to worry about memory? The short answer is: only when you have to worry. But Lua interns strings, so that there is really only one copy of each distinct string. So this code uses less memory than you would think: local t = . Here is the wrong way to read a file into a string: local t = . The idea is that you can use them efficiently as resize- able arrays with integer indices, but can also index them with any other value as a hash map. Just because 0 is a numerical index does not mean it is an array index! By definition, these go from 1 to #t. These are equivalent ways to access the array elements: for i,v in ipairs(t) do f(i,v) end. If you wish to keep track of the size of a sparse array, then it is best to keep and increment a counter: t? The reason is that arrays having array parts and hash parts is an implementation detail, not part of the specification. But concatenation is not addition, and it is useful to keep the concepts separate, In Lua, strings can convert into numbers when appropriate (e. For instance, a List class can be created where . Look at the standard libraries section of the manual. In Lua 5. 1, all strings have a metatable pointing to the string table, so that s: find('HELLO') is equivalent to string. HELLO'). 1. 1. 3 Why doesn't 'for k,v in t do' work anymore? This is one of the few big changes between Lua 5. In the simplest case, it must be a function that returns one or more values each time it is called, and must return nil when the iteration is finished. It is very straightforward to write iterators. This is a simple iterator over all elements of an array which works just like ipairs: function iter(t). There are two ways to get around this. One approach is a sparse array with an explicit size; the iterator must return the index and the value: local sa = ! These issues are discussed further on the wiki. How can I dump out a table? This is one of the things where a nice semi- official library would help. Here is what a typical result looks like. Why does print 'hello' work, and not print 4. The confusion arises because some languages (like Python 2. Lua (as it is n Python 3). However, there are two cases where you can leave out the parentheses when calling a Lua function: you may pass a single value of either string or table type. This little bit of 'syntactic sugar' comes from Lua's heritage as a data description language. What is the difference between a. If the result is 'callable' then you can call it. The method is found, but the self parameter is not set, and the method crashes, complaining that the first parameter passed is not the object it was expecting. How are variables scoped? By default, variables are global, and are only local if they are function arguments or explicitly declared as local. G. local b = y . G. Local variables are 'lexically scoped', and you may declare any variables as local within nested blocks without affecting the enclosing scope. There is one more scope possibility. See the wiki page. Difference between require and dofile? Lua files. The first difference is that you pass the module name to require and an actual file path to a Lua file to dofile. So what search path does require use? This is contained in the value of package. Unix system this would look like this (note that semicolons are used to separate items): $> lua - e . This shows you explicitly how Lua is trying to find the module by matching the path entries: $> lua - lalice. In a similar fashion, package. DLLs) which export an initialisation function. Lua will also use the environment variables LUA? This matches any Lua module in the current directory, which is useful for testing. Why is there the pattern '/usr/local/share/lua/5. So if there was a directory 'mylib' on the Lua path, then require 'mylib' would load 'mylib/init. What if the module name contains dots, such as require 'socket. So on a Unix machine, it replaces '?' in '/usr/local/lib/lua/5. How to explicitly load a binary module? A binary module is a shared library that must have one entry point with a special name. For instance, if I have a binary module fred then its initialisation function must be called luaopen. When writing extensions in C++, it is important to export the entry point as extern . On Windows, you have to ensure that at least this entry point is exported via . Normally, the environment for a function is the global table, but this can be changed. Say we need a function which can operate on one or many numbers: function overload(num). The usual operators can be overloaded using metamethods. If an object (either a Lua table or C userdata) has a metatable then we can control the meaning of the arithmetic operators (like + - * / ^). Overriding () allows for 'function objects' or functors, which can be used wherever Lua expects something that is callable. Note a restriction on overloading operators like ==: both arguments must be of the same type. You would have to create an instance of Super. Number called Zero and use that in your comparisons. You can control the meaning of a. But if Set a method 'set', then s. This is restriction has been removed for Lua 5. How to make functions receive a variable number of arguments? Lua functions are very tolerant of extra arguments; you could take a function of no arguments and pass it arguments, and they will be simply discarded. The syntax of functions taking an indefinite number of arguments ('variadic functions') is the same as in C: function vararg. It is shorthand for all the unnamed arguments. So function I(..) return .. In this case, . The assignment is equivalent to the multiple Lua assignmentlocal a,b = 3. If your function constructs an array- like table, then it can return the array values as multiple values with unpack. Another way for a function to return values is by modifying a table argument: function table. You pass a table and exploit the fact that it is not necessary to use extra parentheses in the case of a function passed a single table argument: function named(t). The usual convention is that if the function returns nil or false then the second value returned is the error message.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |