HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

A label operator

07-13-2008, 03:26 AM#1
Vexorian
I refrained from calling the title "A postfix operator" or "A suffix operator" cause I really have no idea what the correct one would be.

The ability to declare this sort of operator for structs, like a backwards [] could be very useful, if and only if it is implemented correctly.

Current ideas:

Collapse A:
struct life
    static method operator {} takes unit u returns real
        return GetWidgetLife(u)
    endmethod
    static method operator {}= takes unit u, real v returns nothing
        call SetWidgetLife(u,v)
    endmethod
endstruct

function sam takes unit u returns nothing
    set u{life} = u{life} + 3.0
endfunction

Collapse B:
struct life
    static method operator -> takes unit u returns real
        return GetWidgetLife(u)
    endmethod
    static method operator ->= takes unit u, real v returns nothing
        call SetWidgetLife(u,v)
    endmethod
endstruct

function sam takes unit u returns nothing
    set u->life = u->life + 3.0
endfunction


I don't feel like using . because people would confuse it with what . really does which is actually the opposite order, it would also be ambiguous. If someone could bring better ideas?
07-13-2008, 03:38 AM#2
Alevice
I liek the idea, but I am not certain I like the implementation via structs. What happened to the wrapper syntax? Seemed a little clearer.
07-13-2008, 03:40 AM#3
Vexorian
wrapper? Ah, I am just using Life as an example, most of the times it wouldn't be about the unit's life but about something else, and not always about units, it wouldn't have that much to do with natives. Wrapper will allow . if/when I implement it.
07-13-2008, 03:47 AM#4
TheDamien
I like this idea, but I can't picture it being more than a convenient shorthand.
07-13-2008, 03:52 AM#5
Vexorian
Yeah, it would join the team conformed by [], []=, <, .name and .name= provided I can find a suitable symbol for it though.
07-13-2008, 04:17 AM#6
grim001
if you feel like inventing brand new syntax you could use

Collapse JASS:
set u:life = u:life + 3.0

it's like the . syntax, but different enough to not be ambiguous.
07-13-2008, 11:27 PM#7
Vexorian
set u::life = u::life + 3.0 I was going to use . to namespaces if/when I added them anyway, though scopes do the work well enough and namespaces would confuse the heck of people that got used to scopes.

Edit: though single : makes more sense.

--
Edit; I think I am going to end up adding ":"

Collapse JASS:
struct life
    static method operator : takes unit u returns real
        return GetWidgetLife(u)
    endmethod
    static method operator := takes unit u, real v returns nothing
        call SetWidgetLife(u,v)
    endmethod
endstruct

function sam takes unit u returns nothing
    set u:life = u:life + 3.0
endfunction

Of course, unless someone rushes to convince me otherwise in the time between this decision and the time I actually add it (which may take a good while, btw)
07-13-2008, 11:58 PM#8
Anitarf
Why are the parameters (unit, real) still defined as parameters (the "takes" and "returns" syntax)? I know it's a bit late to change that since [] and []= work the same way, but those are defined on a struct and this doesn't really even work as a struct, I mean is there any case where you'd want to use []= and := on the same thing since they seem unrelated? Shouldn't this be defined as something other than a struct anyway?

Edit: Unrelated to the syntax, in your example, the method operators should take a widget, not a unit.
07-14-2008, 01:10 AM#9
grim001
Shouldn't it work more like this? Instead of using structs, create a couple new keywords, even though you hate doing that. Really the point of this is to be able to create a bunch of "labels" for the native handle types, correct?

Collapse JASS:
getlabel unit:life takes unit u, returns real
    return GetWidgetLife(u)
endlabel
setlabel unit:life takes unit u, real v returns nothing
    call SetWidgetLife(u, v)
endlabel

setlabel unit:x takes unit u, returns real
    call SetUnitX(u, v)
endlabel
getlabel unit:x takes unit u, real v returns nothing
    return GetUnitX(u)
endlabel

//this way should allow each handle type to have one declaration of a label
//with the same name. for example, both units and items can have a
//label named x. It is required that the first parameter is equal to the
//type declared in the label name or you get a syntax error.
setlabel item:x takes item i returns real
    return GetItemX(i)
endlabel
getlabel item:x takes item i, real v returns nothing
    call SetItemX(i, v)
endlabel

//works with structs too.
setlabel MyStruct:data takes MyStruct m, integer data returns nothing
    call m.setData(data)
endlabel
getlabel MyStruct:data takes MyStruct m returns real
    return m.datavalue
endlabel


function TEST takes unit u, item i, MyStruct m returns nothing
    set u:life = u:life + 3.0
    set u:x = u:x - 5.0
    set i:x = i:x + 10.0
    set m:data = m:data - 100.0
endfunction
07-14-2008, 03:29 AM#10
Vexorian
Quote:
create a bunch of "labels" for the native handle types, correct?
Not really. What you are posting is what I've been planning for wrapper, which shows a little easier syntax.

Collapse JASS:
wrapper unit extends handle

    static method create takes player p, integer utype, real x, real y, real f returns unit
       return CreateUnit(p,utype,x,y,f)
    endmethod

//should be made inline friendly, really:
    method operator life takes nothing returns real
        return GetWidgetLife(this)
    endmethod
endwrapper

Though what you posted is not bad either, could be a little more flexible.
Edit: Or like the original idea: http://www.wc3campaigns.net/showpost...72&postcount=2

--
I think that instead of doing this the way I put, I will just create the : operator:
Collapse JASS:
set u:x = u:x + 3

becomes:
Collapse JASS:
set x[u] = x[u] + 3

Should work with anything that previously allowed [], anyone agrees?
Quote:
in your example, the method operators should take a widget, not a unit.
It could take a widget, yes.
07-14-2008, 11:31 AM#11
Anitarf
The original looks nice, though I'm liking the : syntax more than the . one.

So, we could do stuff like this?
Collapse JASS:
wrapper real
    method round takes nothing returns real calls I2R(R2I(this+0.5))
    method trunc takes nothing returns real calls I2R(R2I(this))
    method pow takes real p returns real calls Pow(this, p)
    method sqrt takes nothing returns real calls SquareRoot(this)
    //obviously, stuff like add and multiply would look uglier this way, so it was not included
endwrapper

function foo takes real r returns nothing
    set r = r:round-r:trunc+r:pow(2)-r:sqrt
endfunction
Well, it looks somewhat preetier, but doesn't seem all that useful, at least for native types. I suppose properties on handles might be somewhat more worth it.
07-14-2008, 02:14 PM#12
MaD[Lion]
i like -> since its more like pointer kinda thing.... and : is like static methods hmm