HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Type name stuff for structs

07-17-2008, 02:48 PM#1
Vexorian
Something I am beginning to use a lot is this:

Collapse 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:


Collapse 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;


Collapse JASS:
static method create takes nothing returns foo
 local foo r = foo.allocate()
   set r.x=200
   set r.y=200
 return r
endmethod
With adopt it gets a little better:
Collapse 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:

Collapse 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.

Collapse 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
d07.RiV
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
Vexorian
Quote:
I think adopting syntax of a widely used language is a much better idea.

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
darkwulfv
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
moyack
Quote:
Originally Posted by darkwulfv
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.
It reduces the number of letters you write, but in a justified way.


Collapse 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
darkwulfv
I do like the "return this" though. Sounds logical :D
07-18-2008, 06:45 AM#7
Jazradel
I like:
Collapse JASS:
static method create takes nothing returns foo
 adopt foo.allocate()
    set .x=2
    set .y=234
 return this
endmethod
Are there any cases were people haven't used .allocate()? It might be easier to generate it automatically. Not sure how you'd use create with extended structs though.
07-18-2008, 07:25 AM#8
Ammorth
you use allocate with the arguments the parent struct uses in it's create.

Collapse 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
grim001
Make sure adopt works in functions and not just static methods.
07-18-2008, 12:54 PM#10
Vexorian
Quote:
Are there any cases were people haven't used .allocate()?
Yes. I have done that. This allows people to make their custom allocator, and sometimes that's a good idea.

Quote:
Make sure adopt works in functions and not just static methods.
How would that work?
07-18-2008, 03:52 PM#11
Alevice
Why not use the "with" keyword? Seems far more suitable for such needs.
07-18-2008, 04:35 PM#12
Vexorian
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
Vexorian
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.

Collapse 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:
What does that look like? I tried it once before when I needed an array with a number of indices that could only be determined after map init, but couldn't get it working and couldn't find any examples here or any info in the manual.
I don't understand too well what you wanted to do.