l122.posts.articles.learn_lua about code posts

l122.posts.articles.learn_lua.preface l122.posts.articles.learn_lua.preface.the_c_of_scripting_languages l122.posts.articles.learn_lua.preface.why_lua l122.posts.articles.learn_lua.variables l122.posts.articles.learn_lua.variables.types l122.posts.articles.learn_lua.preface

* this introduction is optional, feel free to skip to the next section

* if you come looking for a language overview, skip to the last section

* if youre a Lua expert reading this and find any errors, please email me!

Lua is a beautiful programming language often described by the things it lacks usually in the context of being a design flaw, especially compared to languages around it which achieve similar goals

Python especially gets the biggest comparison to Lua, they have similar object models, theyre both embeddable^(Python is technically embeddable, nearly any application which has Python configuration does it, but most pretend Python is more than it is, frankly) dynamically typed interpreted scripting languages, and its code can often be literally translated in place without many semantic changes, but whereas Python is a language whose language features are sloppily built up over decades, whose primary issue is trying to do everything at the same time and doing none of them particularly well, Lua does takes the exact opposite: do *one* thing, and do it very well

the reason Lua is a beautiful language is not because it is terse, easy to read, or has a lot of features, but that the features it does have are elegant in use and implementation, they all flow together in one direction, not against eachother

██████████████████

l122.posts.articles.learn_lua.preface.the_c_of_scripting_languages

Lua finds itself in a very strange position in that its design is unmistakably high level, the language semantics hide things and theyre nowhere close to bare metal, however the operations you do in Lua are *strange* for a high level language, you do a lot more work to do seemingly simpler things than you would in other languages *like* Python by operating directly on the data youre given

Luas objects are not abstractions more than they are implementations of the languages features, its strings are tables with metamethods which access and manipulate userdata

every thing you learn about the language points you more towards how the language itself works, ever wondered how the : delimiter on methods works? why self is not a keyword? try passing an object to a function!

local Point = {x = 0, y = 0} function Point:incrementX (self) self.x = self.x + 1 end local p = Point; p:incrementX() print(p.x) --> 1

what if i had another object i wanted to use this function with who doesnt *own* the incrementX function? is it just permanently attached to Point?

local p2 = {x = 0} p.incrementX(p2) print(p2) --> 1

as it turns out, this is completely possible, and this may be immediately obvious why to some people, the first argument of a method is *always* the object of the caller, makingx:y() a shorthand for x.y(x), and this assertion is completely correct

l122.posts.articles.learn_lua.preface.why_lua

awawa

l122.posts.articles.learn_lua.variables

Lua is a dynamically typed programming language, in practice this means that its variables are more values attached to names, than names attached to values

variables are simply assigned using the = operator using this pattern

local s = "awawa" | | | |_ value | | |___ assignment operator | |_____ name |___________ scope? print(s) --> awawa

note the use of local before the variable name is completely optional, without it variables are *global* by default

x = 1

however its more valuable to be explicit with your scoping, as globals which are not marked can be percieved as passed in by the environment, or just mixed up for value reassignment

_G. x = 1

admittedly a bit ugly, but this is generally preferred for global declarations, it should remind you to consider if your use of globals is warranted,^(my lawyer wants me to reiterate that unless you *really* know what youre doing or are very sure theres no other way or it makes it significantly easier to do something, you should always use locals, hell even make a function which registers locals to a file you require in other places, usage of `require` is always cached so dont feel bad about overusing it! i use globals for my project-level class system but drop it in place of just returning the class constructor for a library which uses the same format, always evaluate the possibility of name clashing before using global variables) if you always explicitly define your globals, you can always tell whether an assignment creates a new value, assigns to one that was defined earlier, and whether that value is newly created, passed in, and where it should be accessible! its a good time for everyone :)^(if you wanted to know, using `_G. x = y` creates a new field within `_G` named `x` with the value `y`, which is an interface table that allows programmatic access to global variables, you can test this by trying to print `_G["x"]`, which will return the value of `y`!)

l122.posts.articles.learn_lua.variables.types

there are only 5 core types in Lua, with others related to the Lua C API or unimportant for most Lua programmers, those are

* string^(immutable table string object)

* number^(double precision float and special values `inf` and `-inf`)

* table^(1-indexed array + any:any hashmap + object behavior "metatable")

* boolean^(`true` or `false` expression value)

* function^(callable reference)

strings are very deeply integrated into Lua, theyre an object which represents 0 or more characters in it^(lua uses these all over the place as the expected data type for a lot of operations, rather than a number like in other languages), you can use them in place of any other primitive value as you would normally, that may sound weird but it makes more sense when you hear about how other variable types are used :) Lua treats strings as first class citizens

numbers are IEEE 64 bit floats,^(within the positive range of 2^53 and -(2^53)-1 as an integer) Lua does not distinguish between numbers of different sign, floating point, or sizes, and neither can they be constrained to any particular size, which may sound like a limitation but it only really comes up when ensuring array safety

tables are a heterogenous^(having no restrictive type) dynamic^(whose overall length is not fixed) array representing its array component, a hashmap^(a structure assigning values to other values) of any:any pairs^(one key to one value), and a metatable which defines the behavior of the table under specific conditions... three parts! tables are so complicated because they are the only data structure available and comprise every object and complex compound data structure or high level concept you could think of, including but not limited to: enums, dictionaries, pointers, jump tables objects, classes, namespaces, constants, and iterators

booleans are a truthy or falsy value using true and false keywords as the result of a conditional expression^(as opposed to an arithmetic expression such as `(1 + 1)`, a conditional expression may be like `(1 == 1)` instead) or as a value on its own, they function as you would expect, with additional interaction with the not operator, flipping its value from true to false and back

functions are actually values too though they are only callable, functions are the value of a Lua chunk^(compiled Lua bytecode as a value on the stack) once compiled, and since theyre values they can be returned and passed around by value, named or anonymous

the type of a value does not constrain its variable to that value, making variable types mutable since they are not attached to the value they represent, a variable can have its type changed for example:

local x -- declaring a variable with no value x = "awawa" -- string x = 1 -- number x = {} -- table

this concept applies to every other instance of a variable like a function argument, its type is not constrained in any way and any value can be given regardless of type:

local function add (a, b) return (a + b) -- intended to use number type end add("one", "two") -- will error, cannot add strings

as such its the responsibility of the caller and the function to make sure the input is correct, you can do this by error checking or by dispatching, using the variable typing to your advantage:

-- dispatch table local __trim_type = { left = function(s) -- ... return s end, right = function(s) -- ... return s end, } -- interface function local function trim (s, type) __trim_type[type](s) end print(trim(" awawa", "left")) --> awawa

this is a way to implement function overloading in Lua!


uploaded 26.04.13 updated 26.11.21, 1795290198