View Single Post
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