| 07-17-2008, 02:48 PM | #1 |
Something I am beginning to use a lot is this: JASS:struct foo real x real y static method expire takes nothing returns nothing local foo this = foo( GetTimerData(GetExpiredTimer()) ) set .x = .x+2.0 set .y = .y-2.0 endmethod method doo takes nothing returns nothing local timer t=NewTimer() call TimerStart(t,0.02, true, function foo.expire) call SetTimerData(t, this ) endmethod Calling the variable 'this' allows me to freely use .x and .y in a static method, I know, having made the compiler gives me an advantage. Anyway, I think this is wrong yet useful, this should be a keyword and calling a variable 'this' is probably confusing. I have been calling this adot-a-instance. A way in which static methods can mimic instance methods once they have acquired a instance from some indirect way. So, I've been thinking: JASS:struct foo real x real y static method expire takes nothing returns nothing adopt foo( GetTimerData(GetExpiredTimer()) ) set .x = .x+2.0 set .y = .y-2.0 endmethod method doo takes nothing returns nothing local timer t=NewTimer() call TimerStart(t,0.02, true, function foo.expire) call SetTimerData(t, this ) endmethod ?? This madness has a little advantage, .create() has a horrible pitfall and it is that it allows you to mistype stuff since you have to type the struct name thrice; JASS:static method create takes nothing returns foo local foo r = foo.allocate() set r.x=200 set r.y=200 return r endmethod JASS:static method create takes nothing returns foo adopt foo.allocate() set .x=2 set .y=234 return this endmethod Well, anyway the whole thing could be fixed in another way, by static locals inside methods do not need a type name, and if no type name is used, the struct's type is assumed: JASS:static method create takes nothing returns foo local r=foo.allocate() set r.x=2 set r.y=234 return r endmethod Of course, this doesn't allow you to use .x directly. The ultimate thing would be to allow type inference for locals. JASS:function extender takes nothing returns nothing local u = GetTriggerUnit() //GetTriggerUnit returns unit, so the compiler can figure out u needs to be of type unit local handle h = GetTriggerUnit() //since we want the type to be handle we need to type it local unit w //since the assigment is not in the declaration, we can't do it set w=GetTriggerUnit() endfunction static method create takes nothing returns foo local r=.allocate() //allocate is said to return foo so... set r.x=2 set r.y=234 return r endmethod So??? |
| 07-17-2008, 03:51 PM | #2 |
I doubt it would change much apart from causing additional confusion, I think adopting syntax of a widely used language is a much better idea. |
| 07-17-2008, 04:06 PM | #3 | |
Quote:
The last ideas come from widely used languages. I really don't see the point in copying things that are widely using just for the sake of them being widely used. |
| 07-17-2008, 04:42 PM | #4 |
Is there some added functionality I'm missing? It looks okay for better organization, but otherwise it just looks like a way to get out of typing the struct name 3 times. Unless I'm missing something. |
| 07-17-2008, 05:58 PM | #5 | |
Quote:
JASS:static method create takes nothing returns foo adopt foo.allocate() set .x=2 set .y=234 return this endmethod I love this idea, because it's very common repeat the same procedure at the create stage. this is a nice keyword. |
| 07-17-2008, 08:12 PM | #6 |
I do like the "return this" though. Sounds logical :D |
| 07-18-2008, 06:45 AM | #7 |
I like: JASS:static method create takes nothing returns foo adopt foo.allocate() set .x=2 set .y=234 return this endmethod |
| 07-18-2008, 07:25 AM | #8 |
you use allocate with the arguments the parent struct uses in it's create. JASS:struct A static method create takes unit u, integer i returns A ... endmethod endstruct struct B extends A static method create takes unit u, integer i, real r returns B local B b = B.allocate(u, i) ... endmethod endstruct |
| 07-18-2008, 10:01 AM | #9 |
Make sure adopt works in functions and not just static methods. |
| 07-18-2008, 12:54 PM | #10 | ||
Quote:
Quote:
|
| 07-18-2008, 03:52 PM | #11 |
Why not use the "with" keyword? Seems far more suitable for such needs. |
| 07-18-2008, 04:35 PM | #12 |
The plan is to make adopt only work on static methods and around the position of the locals declaration to avoid all the bad things with introduces, and with requires endwith which is too much code. |
| 07-18-2008, 05:48 PM | #13 | |
The inherent thing for custom allocators is that you are not able to use .destroy() . I should consider being able to override it or something. There's also the little problem of not being able to make your own default values. But all in one, if you can make an allocator for something outside structs, you can make the same thing inside a struct using static members. JASS:struct dada static dada array stack static integer N=0 static integer id=0 static method create takes nothing returns dada if(.N==0) then set .id=.id+1 return dada(.id) endif set N=N-1 return .stack[N] endmethod method recycle takes nothing returns nothing set .stack[.N]=this set .N=.N+1 endmethod endstruct Anyway this is a struct with a custom allocator: http://www.wc3campaigns.net/showthre...+value+structs Quote:
|
