[Lib] Classy - a C++ esque class and struct implementation

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
TheZombieKiller
Prole
Posts: 8
Joined: Sun Dec 13, 2015 11:53 am
Location: Gold Coast, Australia
Contact:

[Lib] Classy - a C++ esque class and struct implementation

Post 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.
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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.
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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!
User avatar
TheZombieKiller
Prole
Posts: 8
Joined: Sun Dec 13, 2015 11:53 am
Location: Gold Coast, Australia
Contact:

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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!
marco.lizza
Citizen
Posts: 52
Joined: Wed Dec 23, 2015 4:03 pm

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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.
User avatar
TheZombieKiller
Prole
Posts: 8
Joined: Sun Dec 13, 2015 11:53 am
Location: Gold Coast, Australia
Contact:

Re: [Lib] Classy - a C++ esque class and struct implementation

Post by TheZombieKiller »

I've taken a brief look at Squirrel, although other than Lua, I've only ever embedded AngelScript into my projects.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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?
User avatar
TheZombieKiller
Prole
Posts: 8
Joined: Sun Dec 13, 2015 11:53 am
Location: Gold Coast, Australia
Contact:

Re: [Lib] Classy - a C++ esque class and struct implementation

Post 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;
}
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests