2

I've done some research about Lua programming but I'm still confused about under which paradigm it can work.

In some walkthroughs, I've found that Lua is not made for Object-Oriented Programming. But they are other people that says it can work too for OOP. So, I'm looking in which programming paradigms it can work the best.

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Ortharios
  • 610
  • 1
  • 5
  • 10

2 Answers2

9

Lua is a "do what you want to" programming language. It doesn't pick paradigms; it is a bag of useful features that give you the freedom to use whatever paradigm that you need. It doesn't have functional language features, but it does feature functions as first-class objects and proper lexical scoping. So you can use it functionally if you so desire. It doesn't have "classes" or other such prototypes, but it does feature ways to encapsulate data and special syntax for calling a function with a "this" object. So you can use it to build objects.

Lua does not dictate what you do with it; that's up to you. It provides low-level tools that allow you to easily build whatever paradigm you wish.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
5

Lua is an imperative language; so it is not ideal for functional programming.

Left by itself, Lua is a procedural language. However, given the simplicity of its data structures (it just has one: the table) it's very easy to add a "layer" on top of it and make it an object oriented language. The most basic inheritance rule can be achieved in as little as 10 lines of code. There are several libraries out there that provide a more refined experience though. My library, middleclass, is 140 LOC in total.

Another great way of using Lua is as an scripting language. It's small, fast, uses only standard C stuff, and it's standard lib is tiny. On the other hand, it doesn't come with "batteries included" like java does.

Finally, I find it very useful as a data notation language; you can express raw data in a format very similar to JSON.

In general, I think that Lua feels very close to javascript.

kikito
  • 51,734
  • 32
  • 149
  • 189
  • 1
    +1, with the note that since functions are first-class values and can be anonymous, it can be used in a very functional way. The notations are a little wordy, so I'd agree that it isn't ideal in that role. – RBerteig Nov 02 '11 at 01:54
  • @kikito I know this is an old post, but maybe you can help me: Can you explain that point where you say that there is a format very similar to JSON? I started to get to know Lua but I have not been able to make out a format similar to it to express raw data – Rüdiger Jul 14 '18 at 20:38
  • @Rüdiger Lua tables can be written so that they look almost like json; just replace ':' by '='. There's also the fact that Lua can be made "scoped". You can declare a bunch of global variables in a `.lua` file and then load it so that all globals in the file are scoped inside a `config` var. – kikito Jul 16 '18 at 12:27
  • @kikito alright, thank you! I have found [this](https://www.lua.org/pil/10.1.html) example, I guess that is basically what u meant? So there is simply a table created and this is not a "native" constructor especially for data sets? – Rüdiger Jul 16 '18 at 12:41
  • I don't know of any difference between "a table created" and "a native constructor especially for data sets". – kikito Jul 16 '18 at 16:58