Thread Tools Display Modes
08-25-18, 08:03 AM   #1
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Added support for Generic Types with my OOP library

I'm trying to make a better error detection framework for my addons so I made an OOP framework to support strong typing. This has proven to be really useful for me as it has simplified all my debugging.

It has ensured that I am not passing in nil values into functions that expect certain values and means I no longer need to add tons of if-statements to check for nils, or for certain value types.

I have just added generic type classes. It's not one of the most useful features but I have used it and I think it's a cool addition to have. For example, I use it for creating lists that should only contain Frames/Buttons/instances of classes, etc...

This is the syntax:

Lua Code:
  1. local KeyValuePair = TestPackage:CreateClass("KeyValuePair<K, V>");
  2.  
  3. TestPackage:DefineParams("K", "V");
  4. function KeyValuePair:Add(data, key, value)
  5.     -- do stuff
  6. end
  7.  
  8. local myInstance = KeyValuePair:Of("string", "number")();
  9. myInstance:Add("testKey", 123);

Basically, this creates an instance of "KeyValuePair" and specifies that the K generic type must be a string, and V must be a number. If you call the "Add" method with anything else then it will throw an error.

You can also use "any" to specify any value that is not null, and "?string" to specify an optional string.

What are your thoughts on this?

The full project is here:
http://www.wowinterface.com/download...ObjectLua.html

EDIT: Also, "data" is a special table for holding private instance data. It doesn't take part in the validation and is given to the function after it has been called (you don't pass it manually during the function call).

Last edited by Mayron : 08-25-18 at 08:09 AM.
  Reply With Quote
08-25-18, 02:10 PM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Unnecessary if you're not a muppet. Read this thread for points against over abstraction.

Plus, the game already has multiple OOP systems in place, why add another one?
__________________

Last edited by MunkDev : 08-25-18 at 02:13 PM.
  Reply With Quote
08-25-18, 04:34 PM   #3
Mayron
A Frostmaul Preserver
 
Mayron's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 275
Originally Posted by MunkDev View Post
Unnecessary if you're not a muppet. Read this thread for points against over abstraction.

Plus, the game already has multiple OOP systems in place, why add another one?
I wanted a very basic OOP system that was quick in performance. Originally I created some basic functions to create metatables in a more user-friendly way for inheritance, like using the __call metamethod to instantiate a new table that inherits from the called table. Pretty basic stuff. I don't think helper libraries like this are pointless at all.

Then I wanted some strongly typed parameters and return values which is the main point of the framework. It means I don't need to write so many checks for variables holding the correct values. Also, I can protect code belonging to objects from outside interference so I can eliminate debugging problems. This is the main reasoning behind "why add another one".

It has definitely helped me significantly to develop code faster so it's not pointless at all. The performance so far seems very good unlike the stuff you linked in that post (not run proper tests but I get the impression that the library/framework in that post is heavy in performance/memory costs though I could be wrong...).

I agree that this new feature (generic types) is a bit over the top but it's just an added extra that might rarely be used. It was more of a "this would be a cool experiment to play around with" concept, but the basic features that come with the library are far more useful.

In addition to the implicit debugging support, the OOP features in this library are closer related to Java, C#, etc... than other systems I have seen which, to me at least, feels nicer to use.

Last edited by Mayron : 08-26-18 at 02:45 AM.
  Reply With Quote
08-28-18, 07:57 AM   #4
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
Just keep up, addons are first created to meet our own requirement, then we share if anybody need them. For me, the biggest gain from the WOW is my OOP lib which I already used for my web servers.

The code in Scorpio in not heavy, it'll bind the function directly to the system event, hook and etc. And about the __Async__, it used a heavy coroutine schedule system, but that system could keep the full fps for all addons based on the Scorpio.

Like your validation system, the PLoop(the heavy part that they mentioned) also provide a full type definition and validation like:
Lua Code:
  1. __Arguments__{ Number, String/nil } -- number require, string optional
  2. function Test(num, str)
  3. end

The cost is tiny since I used several techniques to avoid the if-check, and the validation can be easily turn off with one setting so there is no need to change the code.

There are several company started to use the PLoop on their Unity3D games, the type valiation and other features help them fixed thousands errors, like wrong type, spell error and other problems, like you said, without a fine validation system, an error can be spread to somewhere hard to trace back.

The type validation may not be much powerful for addons, since they are tiny projects. But there are more features can be provided with a fine OOP system. The blz's mixin system is just a basic one.

Last edited by kurapica.igas : 08-28-18 at 08:14 PM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Added support for Generic Types with my OOP library

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off