Page 1 of 1
[Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 11:43 am
by TheZombieKiller
https://github.com/DaZombieKiller/classy
Classy provides a C++ esque class and struct implementation for Lua, offering the following syntax:
Code: Select all
class "ExampleClass"
{
-- Class constructor
ExampleClass = function(self)
print "Hello, world!"
end;
-- Sample method
MyMethod = function(self)
print "ExampleClass MyMethod"
end;
}
struct "ExampleStruct"
{
-- Structs do not require constructors, but classes do.
-- If a struct inherits from a class, it will inherit its constructor.
x = 0;
y = 0;
z = 0;
}
See the included example file for a more detailed look at Classy.
This was actually a library I originally made around a year ago, but today I decided to re-write it from the ground up, which proved to be quite beneficial.
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 12:43 pm
by marco.lizza
NIce effort... but, quite frankly, what's the point? This only resemble C++ syntax for the presence of the class/struct "keyword" and the usage of the outermost brackets. In my opinion it distracts/confuses more than what helps.
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 12:49 pm
by Tanner
I didn't know that you could use semi-colons to separate table values in Lua. That combined with your use of function chaining to take additional arguments without using parentheses makes this into an (I think) compelling idiom. Clever!
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 1:00 pm
by TheZombieKiller
marco.lizza wrote:In my opinion it distracts/confuses more than what helps.
Fair enough, I can see where you're coming from. My primary drive for creating this library was that I simply preferred the way classes are done in C/C++ esque languages, and wanted to be able to do it in Lua. It's heavily a matter of opinion I guess.
Tanner wrote:That combined with your use of function chaining to take additional arguments without using parentheses makes this into an (I think) compelling idiom. Clever!
Thanks!
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 1:45 pm
by marco.lizza
TheZombieKiller wrote:
My primary drive for creating this library was that I simply preferred the way classes are done in C/C++ esque languages, and wanted to be able to do it in Lua.
I see.
This is quite a debate. Since the very beginning I always read that almost everybody dislike Lua syntax. I do not, although I admit it's a bit redundant.
Have you checked out Squirrel language, by the way?
TheZombieKiller wrote:
It's heavily a matter of opinion I guess.
Once again, I agree with you.
Just to be clear, I really don't mean to say that your library is useless or something like that. It really serves well it's purpose... but I simply prefer to go "vanilla" and use Lua as it is and/or by implementing classes in the most light and plain way.
I really don't like to put abstraction, redefinition and and overhead layers on top of the language itself. For example, I admire MoonScript as a projects, but I won't be using it no matter what. If I need a more abstract language, I go for another without tweaking the existing one.
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 1:57 pm
by TheZombieKiller
I've taken a brief look at Squirrel, although other than Lua, I've only ever embedded AngelScript into my projects.
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Wed Mar 30, 2016 2:37 pm
by airstruck
This must be great news for anyone who was about to
switch to C++ for the sake of convenience. I guess namespaces will be the next logical step?
Re: [Lib] Classy - a C++ esque class and struct implementation
Posted: Fri Apr 01, 2016 5:43 am
by TheZombieKiller
Overhauled the inheritance functionality, it is now used like this:
Code: Select all
class "ExampleClass"
{
-- Class constructor
ExampleClass = function(self)
print "Hello, world!"
end;
-- Sample method
MyMethod = function(self)
print "ExampleClass MyMethod"
end;
}
class "InheritanceExample" : ExampleClass
{
-- Inherits all methods (including constructor) from ExampleClass
MyMethod = function(self)
print "InheritanceExample MyMethod"
end;
}