stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 09 Feb 2012 09:38 PM |
Take a look at the class system here: http://codepad.org/nuB49Df4 And the example usage here: http://codepad.org/CxWB3kh8 And a unit test of the features if anyone cares: http://codepad.org/PcFGNYqg
General run-down of the non-obvious features: -The main usage is that you pass a function accepting a "class def" object to the class function, and the class function runs this function right away, so really this function is just to provide an elegant logical block for the class-def. In the function body you populate the class-def with the class-data.
-Here are several sub-objects in the classdef which you use to define various things. 1) Just defining a function right on the class-def: "function def:Foo()" will add this function to the public API of the class, so that it can be used on instances of the class.
2) "function def.get.MemberName()" and "function def.set.MemberName(value)" are used to define custom getters and setters for properties of the object.
3) "def.get.MemberName()", "def.set.MemberName()" and "def.getset.MemberName()", add default getters and setters for the given property.
4) "def.static[ anything ] = value" adds a piece of data to the static storage for the class, accessed with "ClassName[ anything ]"
5) "function def.private:Foo()" can be used to create a non-public utility function, which can be called from within the class' member functions, but not from the public API.
Now, from looking at the example code, you may notice I set a member in the setter function itself, which seems like it should cause an infinite loop. That may seem odd but I chose to design the API this way. You cannot call public API getters and setters from within methods. When you set or get a piece of data in the class, it directly gets or sets it, bypassing any getters and setters there may be.
Also, one more thing, the "Create" method is special, it is called when a new object is created, sop you can do set-up of the object in it.
----------------------------------------------
This is by no means the final from of the thing of course. That's why I'm making this post, I want to hear any problems / suggestions / suggested different naming for parts of it. |
|
|
| Report Abuse |
|
|
| |
|
Ozzypig
|
  |
| Joined: 27 Mar 2008 |
| Total Posts: 4906 |
|
|
| 09 Feb 2012 10:27 PM |
| @above http://www.roblox.com/item.aspx?setItemId=8196336&avID=165625478 |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2012 10:28 PM |
| I'm asking a legit question.. |
|
|
| Report Abuse |
|
|
| |
|
Ozzypig
|
  |
| Joined: 27 Mar 2008 |
| Total Posts: 4906 |
|
|
| 09 Feb 2012 10:30 PM |
| I think it's a great system. I believe there's some great inspiration to be found over at LOVE2D's wiki/forums. I know many people have made class systems there that work fantastic. I believe MiddleClass is a good one for class implementations for Lua. It comes with an add on called Stateful. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 12:15 AM |
Lurking...
I just had to point this out:
>Admin posts Links >We Post Links >We get banned by the admin that posted the links in the first place
:D |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 12:23 AM |
@Darkfireyice
There's nothing wrong with them being able to and not us. Links aren't necessarily inappropriate, it's just that they'd need to be moderated, which would require way too much time.
It's just like how only admins can create hats and gear, or upload sound. These are perfectly normal. Well, it's the same for posting links. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 10 Feb 2012 12:24 AM |
| I like the class system, but if you could; make it more C styled pl0x. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
| |
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 10 Feb 2012 12:34 AM |
I suppose the wiki has syntax Highlighting for Lua code now so I really should be using that, but I like codepad's Lua syntax hilighting better.
Anyways, I'm just looking here for any annoyances people would have with that current thing I have being published. It looks okay to me, but I'd rather get a community opinion.
Also, I find it interesting how the Love2D class libraries and mine sort of converge on the same most-elegant syntax solution for some things. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 10 Feb 2012 12:41 AM |
"but if you could; make it more C++ styled pl0x."
Any suggestions for syntax? The main thing is the question of inheritance. I decided not to add inheritance, because it adds a fair cost to the implementation, and it also makes the API a lot harder to understand.
If I want to have inheritance then I also have to do stuff like have an elegant way to access base-class things which have been over-written, and how to have virtual methods, and how to handle construction (whether to have a strict tree of inheritance, or to allow cyclic parts in the inheritance graph EG Cat isa Animal, Dog isa Animal, do I allow ScienceExperiment isa Dog anda Cat? They both have a similar Animal base so it needs to be handled somehow)
Basically, inheritance makes things much more complicated for a user trying learn how to use the class system, and drastically increases the code complexity to implement it.
Personally I decided that it's not worth the extra cost. In Lua we have duck-typing anyways, so we don't need inheritance in all that many cases. You can add your own inheritance-like properties if you really need them by giving your classes which really need it an "IsA" method.
If you can make the case to me of why inheritance is really necessary then I can restructure the thing to include it. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 10 Feb 2012 12:43 AM |
What I mean by C++ Styled; meaning you can state if a part of the class is protected, private, or public.
Inheritance is overrated, so it doesn't matter. |
|
|
| Report Abuse |
|
|
aboy5643
|
  |
| Joined: 08 Oct 2010 |
| Total Posts: 5458 |
|
|
| 10 Feb 2012 12:44 AM |
| Yay Stravant :D Very elegant design and looks fairly useful :) |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 10 Feb 2012 12:45 AM |
| Well; since you won't add inheritance, might as well not add protected. Just private and public. (Default for each member is public would make much sense.) |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 10 Feb 2012 12:53 AM |
Really it already does support private and public, in that if you store something in the object, unless you add a getter/setter for it, either with getset.Mem() or adding more complex getters and setters, the user will just get back an error message that it's not a member. As for methods, I already have the 'def.private' construct to make methods private.
If you have a more elegant way of putting this then I could add it. I considered the syntax: def.private.get.Mem() / etc..
But really they'd just be stubs which do nothing at all, because you can already throw whatever properties you want into the internal object. If you think this would be beneficial to readability then I could add it, but I don't think this is the case. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 10 Feb 2012 01:40 AM |
| But rather doing it like that, for beginners wouldn't it be much easier for them to explicitly type "private: " |
|
|
| Report Abuse |
|
|
NXTBoy
|
  |
| Joined: 25 Aug 2008 |
| Total Posts: 4533 |
|
|
| 10 Feb 2012 01:58 AM |
Instead of `CreateMyClass`, I think the constructor would look nicer as one of:
MyClass.new() -- Consistent with the Roblox objects MyClass() -- C++ and Python style new "MyClass"(...) -- Java and C++ style
Also, how does your system cope with static access of member functions? Sometimes, something like this is useful:
MyClass.Method(anInstanceOfMyClass)
Such as for functional programming like this:
function apply(func, iterable) for _, i in ipairs(iterable) do func(i) end end
apply(MyClass.Method, {anInstanceOfMyClass, anotherInstanceOfMyClass}) |
|
|
| Report Abuse |
|
|
zTopz
|
  |
| Joined: 08 Feb 2012 |
| Total Posts: 17 |
|
|
| 10 Feb 2012 02:06 AM |
"What is this post for" I thought it was pretty obvious... |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 10 Feb 2012 08:09 AM |
"But rather doing it like that, for beginners wouldn't it be much easier for them to explicitly type "private: "" Unfortunately I can't do it exactly like C++ with the Lua syntax, otherwise I would.
"Also, how does your system cope with static access of member functions? Sometimes, something like this is useful:" That's a good point, and easy to implement too, I will add it. The code already checks that members are actually being called on a valid instance of the class they belong with, so even functionally it isn't an issue.
"Instead of `CreateMyClass`, I think the constructor would look nicer as one of:" I mainly did this to follow the convention set by the functions in the RbxGui library, but I'm considering changing it now.
My preferred syntax is `new'MyClass'()`, but the problem with that is that it's somewhat namespace polluting. If I made this automatically included into the global scope when you tell the library to import itself into your scope then I'm sure at the very least one script somewhere will break, and others will have unexpected behavior since they use new as a name themselves which would over-write my new for constructors.
The second option is to use the Roblox convention of MyClass.new(), while this is good because people already know it, it's also it's downside. People expect MyClass.new'd objects to be immutable, while the stuff I'm making with this is decidedly not immutable.
What do you think about the pros/cons of each? |
|
|
| Report Abuse |
|
|
coplox
|
  |
| Joined: 07 Jun 2008 |
| Total Posts: 3252 |
|
|
| 10 Feb 2012 09:20 AM |
The fact that there are no parenthesis after "class" surrounding the first argument hurts my eyes. Will it work if I just move around the parenthesis a bit?
class("Part", function() end)
|
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 10 Feb 2012 10:11 AM |
"but the problem with that is that it's somewhat namespace polluting"
You could say the exact same thing about your class function. If there is any conflict, it's easy enough to fix even if it's a large script. I would prefer the new'Name'() syntax because it's consistent with the class'Name'() syntax. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 12:25 PM |
| stoked on this work Stravant, I'll try and test it out soon and let you know what I think. |
|
|
| Report Abuse |
|
|
|
| 10 Feb 2012 05:57 PM |
| I really like the syntax. It reminds me of LOVE2D's syntax |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 10 Feb 2012 06:03 PM |
@Legand Fair enough. I assume that if the user is using my class system they aren't also using another one thing, and it seems a lot more unlikely that "class" would be used as a variable name than "new". Although both are fairly unlikely. I am reconsidering it now though, I'll ask around a bit more. It is fair enough to say that if I'm using the class syntax new would not me much more of a problem.
"The fact that there are no parenthesis after "class" surrounding the first argument hurts my eyes. Will it work if I just move around the parenthesis a bit?" That syntax is just syntactic sugar in Lua, if you want to you can use: class('MyClass')(function() end) And it will work identically. |
|
|
| Report Abuse |
|
|