lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 29/05/2019 11:47, Kurieita wrote:
I been using Lua for years now and one thing that I hate is that is
does not support native classes, exceptions or enums as many other
languages does. Even Python support the three. I understand that it
is very easy to write Lua code to emulate the syntax or code as you
might see it but this isn't the same thing as having direct syntax
for the languages.

Classes can easily be done with function scope or metatables
Exceptions(try, catch, finally, [maybe even "else"]) can be done the
same way honestly but using callback functions. Enums can be done
simply with a table or a much more powerful metatables enum system

To implement them is 1) time consuming . 2) defeats the whole purpose
of the DRY (Do not repeat yourself) rule. 3) and make the code harder
to read since they are not native and someone have to read the
implementation for them.



I think that you may suffer from something that Lua "newcomers" have
always suffered: the "my favorite language has it" syndrome (yes, I had
it too!).

BTW I've been using Lua as my main language (albeit not professionally)
for more than 10 years.

At the beginning I struggled, coming from long years of (professional)
Java programming, without native class support. Then I slowly realized
(REALLY realized) that Lua was a different language, with a different
philosophy ("never fight the language" as the old adage says).

Lua is minimalistic by design. It doesn't implement direct support for
any programming paradigm (as C++ maybe tries to do), it gives you
mechanism to build that support in a fairly decent ways, if you really
so desires. Class-based OO is possible, prototype-based OO is possible,
functional programming is possible. You "just" need to provide the right
framework using the facilities Lua provides. Of course they won't be
"standard", so interoperability could be an issue, but Lua is not
designed to be used as that (although huge projects do use Lua as an
embedded language in which most codebase is written).

By writing things in Lua I even came to the conclusion that for many
tasks full OO is overkill and overvalued. In my personal projects I
rarely use inheritance now. I use most often composition and, frankly,
simple container objects, where OO techniques are only used for
encapsulation and data hiding. No more complex hierarchies or the like.
It took quite a while to have that "aha!" moment in which I realized I
could switch paradigm with ease, without shoehorning everything in an
object, with a ton of abstraction layers above. YMMV of course.

As for exceptions, the problem is the same: error management is hard.
Anyhow. There is no magic bullet and "exceptions" are not the exception
(pun intended!:)
You just have to adapt to a different error management mechanism.
If you want to build a robust system your error management strategy will
be complex and multi-faceted anyway, so the fact that in Lua exceptions
are not available is not the showstopper. Again, you have to learn a
different approach.

My questions/concerns is: will Lua ever have actual real support
for any of these? Mainly classes. That is one thing that Lua does not
support which makes it impractical for real world applications unless
you're doing embedding applications.


So, to answer your questions, that from time to time pop-up in this
list, the answer is: most probably no. Lua will never have classes and
will never have Java/C++-like exceptions, at least as Lua team see Lua now.

Some of the use cases of those facilities are being addressed in the
forthcoming Lua 5.4 release, in particular RAII (which in C++ is based
on classes and exceptions/stack unwinding). I didn't follow Lua 5.4.
development too much, but the "toclose" variables seem to have been
introduced just to have deterministic resource management.

Lua 5.4. will have also "const" variables (but not "const" object, as
far as I understand, but objects non-constness is an issue only for
tables and userdata, since other values are not objects or are immutable).


Cheers!

-- Lorenzo