MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 17 Jan 2014 06:30 PM |
Just made something you might find interesting. It is a ModuleScript with a library containing an easy to use class function meant to clean up any code that creates and uses custom made classes through newproxy. It also has support for inheritance and classes can inherit from multiple classes (unintended, but gonna keep it because it can be useful). It's usage looks like this:
lib = require(moduleScript) class = lib.class class 'className' (optionalExtendedClass) { property = "$type" --$ is optional and indicates readonly values } object = className() --and if you overwrite the constructor variable className = lib.constructor("className")
Anyone have any possible uses, comments, or criticism on it?
Link: http://www.roblox.com/OO-Class-Library-item?id=142480881 |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Jan 2014 10:08 AM |
This isn't so much of an OOP library as it is a just an easier way to make your own libraries. Regardless, nice job. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 06:31 PM |
| Okay, and any flaws that you can think of with it, like using '$' to indicate read-only values (if it is a flaw that is)? |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2014 06:42 PM |
Don't use "$". I would recommend doing some haxy stuff to get custom datatypes, then you could simply do something like: readonly myvar = "hi" |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 06:53 PM |
Okay, I will look into some methods I can do so without too ugly of a syntax. Wait... I could possibly make readonly a function that behind the scenes and you just stick the function call in brackets if you wish to do so with that. That is the type of approach used for inheritance anyways. Like: class 'randomClass' { [readonly 'myvar'] = 'hi' } That is the best way I can think of right now at least. |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2014 06:54 PM |
Use sandboxing with the __index function that checks a separate table with the members and checks if it's read only.
If it's read only, error.
Keyboard not found. Press F1 to continue. |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2014 06:55 PM |
__index metamethod*
Keyboard not found. Press F1 to continue. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 07:00 PM |
| I already did so. I didn't include the erroring though, because I just thought it was not really necessary. I am also going to set up the code so that creating a class with a name that is already in use won't error since I am trying to create an easy to use function that runs as smoothly as possible. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 07:08 PM |
| What symbol should I use to indicate read-only properties within the index? I want to use a symbol that makes sense, but does not serve any purpose in pattern matching as that would interfere with the current system. The readonly function I will be creating will use that symbol. For now, variable for configuration and customization it is. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 08:23 PM |
Just updated it so it returns a function that unloads the functions to the environment for you, and added a readonly function that adds a symbol set in the ModuleScript to the beginning of the index of a property to enable read-only behavior. New syntax:
require(ModuleScript)() class 'className' (optionalClass) { property = "type", [readonly 'property'] = "type" } newInstance = className() |
|
|
| Report Abuse |
|
|
suremark
|
  |
| Joined: 13 Nov 2007 |
| Total Posts: 6315 |
|
|
| 18 Jan 2014 08:32 PM |
An interesting prospect, but for RBX.Lua I think readonly properties is a little extravagant-- it's not like you're going to have any security issues. The only thing you really need is inheritance.
The best way to imitate classes in Lua is using separate function environments-- just give each instance its own environment with a constructor. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 18 Jan 2014 10:25 PM |
| Each function indexed is wrapped so it gives the instance and a table of properties, but I am going to change that, so it self detects usage of the ':' operator (or 'obj.method(obj)') and replaces it with the property table only so read-only properties can be written to with no trouble from within methods. I will likely have to wrap that too so the symbol internally used by the 'class' function will be able to be omitted from the index. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 19 Jan 2014 12:45 AM |
Just added static properties and applied the changes with methods so the properties table always replaces the object if the object is passed as the first argument to a method. Syntax for static variables: [static readonly 'property2'] = "type" Syntax for static and read-only variables without usage of the 'static' and 'readonly' functions: ['$readOnlyProperty'] = value, ['#readOnlyProperty'] = value I also added configuration variables for both inside the script, and added a small if-check to make the code more portable (when the return at the bottom is removed at least) so newproxy and getfenv are set with pseudo functions if they don't exist, in case the script is used in a Lua 5.2 engine, or another engine that might not use them. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 19 Jan 2014 12:46 AM |
Woops. ['#staticProperty'] = value That is what it should be. |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 21 Jan 2014 05:12 PM |
Sorry for the bump, just had to do this for anyone who might happen to come across this thread in the future. Static and read-only properties have been updated a few days ago. class 'newClass' (optionalExtendedClass) { [static 'propertyOne'] = "type", [readonly 'propertyTwo'] = "type" } Nuff bumping from me unless discussion by chance continues (only bumped cause I linked and wanted up to date info on here). |
|
|
| Report Abuse |
|
|
notfruit
|
  |
| Joined: 21 Sep 2012 |
| Total Posts: 1386 |
|
|
| 21 Jan 2014 06:59 PM |
| You should try to port 30log to roblox. It would be really useful. |
|
|
| Report Abuse |
|
|