GOOP - OOP Library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

GOOP - OOP Library

Post by baconhawka7x »

This is my first time ever sharing some source and making a library, so if anyone has any feedback it would be very appreciated. Just please be nice :')

GOOP
Gage's Object Oriented Programming
An OOP library for lua that enables all of the oop features you'd want - Along with optional type checked arguments for instantiation.

I'm working on a somewhat large game right now that utilizes a decent amount of OOP practices, and I got tired of all of the boilerplate required for me to create a new class.

Also, as the codebase gets larger and larger, I've ran into instances where I may pass incorrect arguments when instantiating classes, or will miss arguments that the class absolutely needs to function.

To solve this I made GOOP.

Quick Start
Goop's only function is to define a class.
For this example lets make a Person class.

Code: Select all

local Person = Goop.Class({
    static = {
        species = "Homo sapiens"
    },
    dynamic = {
        name = "",
        age = 0
    },
    arguments = { "name", "age" }
})

local bob = Person("Bob") -- Missing arguments!
local bob = Person("Bob", 26) -- All good.
In order to inherit from this class, all we have to do is define the extends value.

Code: Select all

local Bob = Goop.Class({
  extends = Person,
  dynamic = {
    occupation = "construction",
    name = "Bob",
    age = 26
  }
})
Type-Checking
Type checking arguments can be done easily, and at an individual level. All you have to do is define the argument as a table containing two strings - One for the string key, and one for the expected type.

Code: Select all

local Bob = Goop.Class({
  extends = Person,
  dynamic = {
    occupation = "construction",
    name = "Bob",
    age = 26
  },
  arguments = {
    {"age", "number"}, -- MUST be a number
    {"height", "number"}, -- MUST be a number
    "weight" -- Will not be type checked.
  }
})

local bobOne = Bob(26, "170cm", "60kg") -- Invalid type arg #2 - Expected number. Received string.
local bobTwo = Bob(26, 170, "60kg") -- All good
local bobTwo = Bob(26, 170, 60) -- Also good.
GITHUB for more info:
https://github.com/quangogage/GOOP
Attachments
GOOP.lua
(5.64 KiB) Downloaded 222 times
Post Reply

Who is online

Users browsing this forum: darkfrei and 55 guests