|
I've made some crazy implementations of classes in Lua, but nothing like I'm planning to make. Below is some sample code. This will be ran in Lua and callable in Lua. Let me know what you think about the syntax, and how I should improve it. Yes, it does comply with Lua's syntax.
Here is some features that could be included:
-Type checking
-Classes
-Inheritance
-Interfaces
-Abstract classes
-Access modifiers
-Polymorphism
Here is the sample code. You might want to put it into studio and pace it out.
---------------------------------------------------
require(Workspace["Phalanx Lua"])
public:interface "Accounts"{
public:method "addAccount" ();
override.public:method "addAccount" (int "accountID");
public:method "removeAccount" (int "accountID");
};
public:class "Bank" impliments "Accounts"{
private:property "accounts" {};
private:method "changeBallance" (int "accountID", int "amount"){
variable "account" (accounts[accountID]);
account:equal (nil) {
exit "There is no account with that id.";
};
(account - amount):less_than (0) {
exit "There is not enough money in that account.";
};
accounts[accountID]:set (amount);
};
public:method "withdraw" (int "accountID", int "amount"){
exit (changeBallance(accountID, -amount))
};
public:method "deposite" (int "accountID", int "amount"){
exit (changeBallance(accountID, amount))
};
public:method "addAccount" (){
variable "accountID" (#accounts + 1);
accounts[accountID]:set (0);
exit (accountID);
};
override.public:method "addAccount" (int "accountID"){
(accounts[accountID]):not_equal (nil){
exit "There is already an account with that id."
};
accounts[accountID]:set (0);
exit (accountID);
};
public:method "removeAccount" (int "accountID"){
variable "account" (accounts[accountID]);
account:equal (nil) {
exit "There is no account with that id.";
};
accounts[accountID]:set (nil);
exit (accountID);
};
};
local bank = new "Bank"
local accountID = bank:addAccount()
bank:deposite(accountID, 999999999) --I'm rich! |
|
AlyteJoin Date: 2011-10-24 Post Count: 10090 |
NO
NO
YUCK
YOU'RE OBVIOUSLY TRYING TO RUN AWAY FROM LUA!!!! |
|
AlyteJoin Date: 2011-10-24 Post Count: 10090 |
COME BACK HERE CHILD
LET ME SPANK YOUR BOTTOM |
|
|
Haha, I'm guessing you don't like C++ either. |
|
|
"phalanx"
I just couldn't stop thinking of programmers wearing hoplite armour carrying 6-metre long spears and forming a phalanx with their shields having a lua logo painted on them |
|
|
Yeah. I named it that because of the type checking that's included. It takes the difficulty out of defensive programming. |
|
Charl3s7Join Date: 2007-12-07 Post Count: 4146 |
that looks incredibly messy |
|
|
If you want to program like you do in C++ with Lua, that means you're doing it wrong. That is a horrible pollution of the global environment, and you're trying to introduce strong and static typing to weak and dynamic typing. This shows that as a programmer, you haven't fully matured.
Use prototypes, not classes. That's more of "The Lua Way". Prototypes are easy-to-do and are a more traditional Lua method for OO. It keeps it small, it keeps it minimal, and in fact, prototypes are more suited for weak/dynamic/duck typing than classes.
Heil prototypes.
~LuaWeaver; Programmer, gamer, developer. |
|
AlyteJoin Date: 2011-10-24 Post Count: 10090 |
Basically, Lua is a scripting language, so don't try to turn it into C++. You'll only end up making your code look horrible and your execution time will be maybe 3x slower, especially with those statically typed variable attempt... |
|
AlyteJoin Date: 2011-10-24 Post Count: 10090 |
However, it's great that you're making a more complicated OO library than most of the others out there.
Just embrace the fact that Lua is a scripting language, and don't over-do it, k? |
|
|
inb4alldynamiclanguagesturnstatic |
|
MettaurSpJoin Date: 2010-03-20 Post Count: 3179 |
Embrace dem Luas: http://www.roblox.com/Forum/ShowPost.aspx?PostID=123360204
Doesn't stray too far and over complicate things too much. The code I made for the setup is messy itself, but it cleans it up in the end. All the method calls you want to do make it look way too messy. Speaking of my OO thingy, Ima go make some fixes to it that I made in the past day. |
|
|
>However, it's great that you're making a more complicated OO library than most of the others out there.
I once made a really messy thing with newproxy stuff, but it turned into a mess. You can still find it if you know where to look.
~LuaWeaver; Programmer, gamer, developer. |
|
MettaurSpJoin Date: 2010-03-20 Post Count: 3179 |
cleans up the rest of the code in the end* o3o Lack of specifics lol |
|
|
I will admit right off the bat that I am a total newbie at programming in general.
But from what I read, it just looks like you're trying to make Lua more like C++.
Surely, as Lua is meant to be a scripting language, this is a bad idea?
I will also admit I have no idea what "defensive programming" is. |
|
Charl3s7Join Date: 2007-12-07 Post Count: 4146 |
"Defensive programming" is used to describe practices where you assume that every bit of code you write can be hacked, making you write tons of error catching. |
|
|
When I saw "The Lua Way"
I instantly thought of the gray beards yelling code at you and making do weird things. |
|