Classes, exceptions and enums

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Classes, exceptions and enums

Kurieita
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.

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.
Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Rob Kendrick-2
On Wed, May 29, 2019 at 05:47:01AM -0400, Kurieita wrote:
> 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.

Lua is about providing the tools to do what you want, not mandate.  I do
not use most of the features you describe here in Lua, and nor would I.
Adding them to the core to save a few lines of code seems like bloat,
not to mention the various tastes people have.

B.

Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Frank Kastenholz-2
In reply to this post by Kurieita


> On May 29, 2019, at 5:47 AM, Kurieita <[hidden email]> wrote:
>
> Exceptions(try, catch, finally, [maybe even "else"]) can be done the same way honestly but using callback functions.

Pcall?

> That is one thing that Lua does not support which makes it impractical for real world applications unless you're doing embedding applications.

Um, isn’t “embedding applications” what Lua is designed for? (And I know of some very real world applications, that are much more than “embedding applications”, in Lua that work quite well without classes.)

Frank Kastenholz




Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Kurieita
Pcall is a protected call. Not exceptions, yes its similar but not the same thing.
And yes embedded applications was what it was designed for but that doesn't mean it can not includes some useful features that many other languages have.

On Wed, May 29, 2019 at 7:11 AM Frank Kastenholz <[hidden email]> wrote:


> On May 29, 2019, at 5:47 AM, Kurieita <[hidden email]> wrote:
>
> Exceptions(try, catch, finally, [maybe even "else"]) can be done the same way honestly but using callback functions.

Pcall?

> That is one thing that Lua does not support which makes it impractical for real world applications unless you're doing embedding applications.

Um, isn’t “embedding applications” what Lua is designed for? (And I know of some very real world applications, that are much more than “embedding applications”, in Lua that work quite well without classes.)

Frank Kastenholz




Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

David Favro
In reply to this post by Kurieita
On 5/29/19 5:47 AM, 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.
[...]
> 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.

Having used Lua as a general-purpose language for ~8 years (or attempted
anyhow), I more-or-less agree with your general points, but I view saying
"enum is missing" as a convoluted way to say that you want constants and
static typing (or type annotations), and probably a little syntax to define
the enum.  Lua has neither constants nor static types/annotations (both of
which would be welcome by me), so the building blocks are not there to
create a good enumerated type concept.

Without these required components, an inferior but workable concept of enum
can be implemented with existing Lua facilities (as you noted) e.g. as a
table with some arbitrary values assigned to named keys [1], or something a
little more sophisticated via considerable metatable complexity [2].

I confess that I am ignorant of the difficulty in implementing constants and
static types, but my impression is that constants is the less difficult of
the two, and adds less 'bloat' to the runtime [3], but also less value to
the language.

-- David

[1]: I am not intimately familiar with Python but my understanding is that
it does not enforce type-checking on enums even at runtime, so it really
doesn't offer much more than Lua already has.  I.e., in Python the result of
an equality comparison operator between two values of differing enum types
is not considered an error, it just evaluates to false (please correct me if
I'm wrong).  In my opinion, unless the programmer explicitly indicates
otherwise, this almost always represents a bug, and should not be permitted
(for best results via 'static analysis' AKA 'at compile time', but even a
runtime error is better than no error).

[2]: A metatable implementation could attempt run-time type-checking, but
this is undermined by the == operator's refusal to call __eq() on differing
types [pre-5.3 they must have *exactly* the same __eq metamethod; even in
5.3, although the behavior improved, __eq() still will not be called on a
primitive type other than table or userdata].

[3]: I haven't been following 5.4 developments too closely but there was
some list discussion that constants might be coming, or perhaps not.


Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Kurieita
I was reading the source for 5.4 and there are basic implementation for const variable which I like because I would've asked for const too since its something that would be useful to.
The enums problem is a thing that I didn't really think about, thanks for pointing that out. I do disagree tho that you said "const variable bring less value to the language" if I understood that correctly.
Even as an embedded language added enums might be troublesome but added basic try/catch syntax wouldn't really be bloat or troublesome. There are plently of basic Lua implementation for it but not the actual syntax.



On Wed, May 29, 2019 at 9:00 PM David Favro <[hidden email]> wrote:
On 5/29/19 5:47 AM, 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.
[...]
> 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.

Having used Lua as a general-purpose language for ~8 years (or attempted
anyhow), I more-or-less agree with your general points, but I view saying
"enum is missing" as a convoluted way to say that you want constants and
static typing (or type annotations), and probably a little syntax to define
the enum.  Lua has neither constants nor static types/annotations (both of
which would be welcome by me), so the building blocks are not there to
create a good enumerated type concept.

Without these required components, an inferior but workable concept of enum
can be implemented with existing Lua facilities (as you noted) e.g. as a
table with some arbitrary values assigned to named keys [1], or something a
little more sophisticated via considerable metatable complexity [2].

I confess that I am ignorant of the difficulty in implementing constants and
static types, but my impression is that constants is the less difficult of
the two, and adds less 'bloat' to the runtime [3], but also less value to
the language.

-- David

[1]: I am not intimately familiar with Python but my understanding is that
it does not enforce type-checking on enums even at runtime, so it really
doesn't offer much more than Lua already has.  I.e., in Python the result of
an equality comparison operator between two values of differing enum types
is not considered an error, it just evaluates to false (please correct me if
I'm wrong).  In my opinion, unless the programmer explicitly indicates
otherwise, this almost always represents a bug, and should not be permitted
(for best results via 'static analysis' AKA 'at compile time', but even a
runtime error is better than no error).

[2]: A metatable implementation could attempt run-time type-checking, but
this is undermined by the == operator's refusal to call __eq() on differing
types [pre-5.3 they must have *exactly* the same __eq metamethod; even in
5.3, although the behavior improved, __eq() still will not be called on a
primitive type other than table or userdata].

[3]: I haven't been following 5.4 developments too closely but there was
some list discussion that constants might be coming, or perhaps not.


Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Lorenzo Donati-3
In reply to this post by Kurieita
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



Reply | Threaded
Open this post in threaded view
|

Re: Classes, exceptions and enums

Andrew Starks-2
In reply to this post by Kurieita
Top posting because that’s how this thread is:

Another thing, be careful about assuming what is hard and what isn’t. If parity with the C API is not important, you’re probably right, exceptions wouldn’t be that hard. C does not have exceptions, so implementing them in Lua, while maintaining parity with the C API (you can build up anything in the C API that you can in the language) isn’t as straightforward as you’re making it out to be. 

Andrew Starks

From: [hidden email] on behalf of Kurieita <[hidden email]>
Sent: Thursday, May 30, 2019 06:11
To: Lua mailing list
Subject: Re: Classes, exceptions and enums
 
I was reading the source for 5.4 and there are basic implementation for const variable which I like because I would've asked for const too since its something that would be useful to.
The enums problem is a thing that I didn't really think about, thanks for pointing that out. I do disagree tho that you said "const variable bring less value to the language" if I understood that correctly.
Even as an embedded language added enums might be troublesome but added basic try/catch syntax wouldn't really be bloat or troublesome. There are plently of basic Lua implementation for it but not the actual syntax.



On Wed, May 29, 2019 at 9:00 PM David Favro <[hidden email]> wrote:
On 5/29/19 5:47 AM, 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.
[...]
> 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.

Having used Lua as a general-purpose language for ~8 years (or attempted
anyhow), I more-or-less agree with your general points, but I view saying
"enum is missing" as a convoluted way to say that you want constants and
static typing (or type annotations), and probably a little syntax to define
the enum.  Lua has neither constants nor static types/annotations (both of
which would be welcome by me), so the building blocks are not there to
create a good enumerated type concept.

Without these required components, an inferior but workable concept of enum
can be implemented with existing Lua facilities (as you noted) e.g. as a
table with some arbitrary values assigned to named keys [1], or something a
little more sophisticated via considerable metatable complexity [2].

I confess that I am ignorant of the difficulty in implementing constants and
static types, but my impression is that constants is the less difficult of
the two, and adds less 'bloat' to the runtime [3], but also less value to
the language.

-- David

[1]: I am not intimately familiar with Python but my understanding is that
it does not enforce type-checking on enums even at runtime, so it really
doesn't offer much more than Lua already has.  I.e., in Python the result of
an equality comparison operator between two values of differing enum types
is not considered an error, it just evaluates to false (please correct me if
I'm wrong).  In my opinion, unless the programmer explicitly indicates
otherwise, this almost always represents a bug, and should not be permitted
(for best results via 'static analysis' AKA 'at compile time', but even a
runtime error is better than no error).

[2]: A metatable implementation could attempt run-time type-checking, but
this is undermined by the == operator's refusal to call __eq() on differing
types [pre-5.3 they must have *exactly* the same __eq metamethod; even in
5.3, although the behavior improved, __eq() still will not be called on a
primitive type other than table or userdata].

[3]: I haven't been following 5.4 developments too closely but there was
some list discussion that constants might be coming, or perhaps not.