Sunday, October 25, 2009

Jasoop Redesign

I have been typing for the documentation of Jasoop - which is what I called my style of javascript oop I talked about two days ago (technically yesterday, but not in reality).

And now I have already decided that I am going to change it all already. Here's the story.

I had thought of a way to create static functions, however, I had not realized that it would fail if the function was later redefined. Of course, this does not really matter for functions, but it did mean that I could not use the same technique for static variables. So I was once again posed the same question: how to design static in Jasoop.
I did come up with an answer: if each static property would be an object with one property (say static) I could write some code to make sure these variables were static, while keeping the syntax rather clean. Alright you would now have to type "varName.static", but that's hardly a loss. Then it hit me, if I reversed the syntax there, the code to make the variable static would be (ironically) static, rather than depend on the static variables - and I would get a nicer syntax to boot. Say it yourself: "static.varName". Isn't that much better?
My next issue was deciding whether to use the same syntax for functions or not (I mauled this all over in my head while cooking my dinner). My first impulse was not to do so, because I thought not having to write that static there would just feel much more natural. In the end, the uniformity for variables and functions did win - partly due to an argument that is really nonsense, but I only realize that while writing this. Oh, and don't worry, currently I have more than enough reason to keep it the way it is. The door was now ajar.
Next I thought of a way to create protected variables. I did think of a way (giving the class a return value if it was not called with the new operator), but that did leave me with two challenges: how to differentiate them from private variables and how to pass them through a single return value. Both questions had a single answer, I could simply use the syntax I just made up for static variables. "protected.varName" makes sense, right? Suddenly the door was swinging wide open.
And then opportunity presented itself. I could get rid of all those differences between the variables with different access levels, while getting rid of that ugly _this variable I had to introduce. Man, it cleaned up the syntax, it sure did. I was standing in front of an open door and the other side was much more beautiful than this one - needless to say I went through.

So now, inside the class we have "public.publicVar", "protected.protectedVar", "private.privateVar" and "static.staticVar". One little thing to note is that all static variables are public, but as of yet I do not see any way I can change that (unless static functions are defined outside the "class definition" they can only be accessed once the first object of the class has been created, and outside the class definition you cannot reach protected or private variables).

From the outside you can reach "object.static.varName" and public variables in some way, but I am not sure yet whether that will be "object.varName" or "object.public.varName". Perhaps it will make itself clear once I have found a way to make static variables with different access levels.

Anyway, Jasoop is coming to life and I am pretty sure the result will be nice and it will probably even include features not yet seen in javascript (protected variables, to name one thing).

No comments: