[vJass] Operator

~GaLs~

† Ғσſ ŧħə ѕαĸε Φƒ ~Ğ䣚~ †
Reaction score
180
I wishes to learn how operators works, but the damn manual is making me much and much more confused.

Anyone out there willing to teach me of how operators works?

*Ps: People who don't know about what operator is please don't lay a finger on this thread, I need someone who teach me, not someone who examine me. Thank you so much. :D

JassHelper Manual said:
Operator making
Jasshelper allows you to declare custom operators for your structs, these operators would then be converted to method calls, vJass currently allows operators for <, > , array set and array get.

The official name for this process (In wikipedia and books) is operator overloading. In the case of vJass, An overloaded operator is a method, but it gets operator as name, after the operator keyword you specify the operator being overloaded.

An example is worth 1000 words:
JASS:
    struct operatortest
        string str=&quot;&quot;

        method operator [] takes integer i returns string
            return SubString(.str,i,i+1)
        endmethod

        method operator[]= takes integer i, string ch returns nothing
            set .str=SubString(.str,0,i)+ch+SubString(.str,i+1,StringLength(.str)-i)
        endmethod

    endstruct


    function test takes nothing returns nothing
     local operatortest x=operatortest.create()
        set x.str=&quot;Test&quot;
        call BJDebugMsg( x[1])
        call BJDebugMsg( x[0]+x[3])

        set x[1] = &quot;.&quot;
        call BJDebugMsg( x.str)
    endfunction

By this example we are overloading the [] operator and giving it a new function for the objects of type operatortest. The operator [] specifies the replacement for array get and []= is the replacement for array get.

After inspecting the code you may notice that we are making the string function as an array of strings (or actually characters)

The [] operator requires 1 argument (index), the []= operator requires 2 arguments (index and value to assign). [] must return a value.

[] and []= operators can also be declared as static. This might have some uses, the struct name is going to be allowed to use index operators.

There is a lot of criticism towards operator overloading since it allows programmers to make code that does not make sense, please use this feature with responsibility.

You can also overload < and > , notice that there is only syntax to declare < by declaring it you are forcefully determining an order relation for structs of that type. So it automatically makes > based on your < declaration.
JASS:
struct operatortest
        string str=&quot;&quot;

        method operator [] takes integer i returns string
            return SubString(.str,i,i+1)
        endmethod

        method operator[]= takes integer i, string ch returns nothing
            set .str=SubString(.str,0,i)+ch+SubString(.str,i+1,StringLength(.str)-i)
        endmethod

        method operator&lt; takes  operatortest b returns boolean
            return StringLength(this.str) &lt; StringLength(b.str)
        endmethod

    endstruct


    function test takes nothing returns nothing
     local operatortest x=operatortest.create()
     local operatortest y=operatortest.create()

        set x.str=&quot;Test...&quot;
        set y.str=&quot;.Test&quot;

        if (x&lt;y) then
            call BJDebugMsg(&quot;Less than&quot;)
        endif
        if (x&gt;y) then
            call BJDebugMsg(&quot;Greater than&quot;)
        endif
    endfunction

In the example, an object of type operatortest is considered greater than another object of that type if the length of its str member is greater than the length of the other object's str member.

operator< must return a boolean value and take an argument of the same type as the struct.

Operators are interface friendly meaning that an interface may declare operators, there is a catch and it is that the operator< must be declared without signature in an interface. Also when using > or < to compare interface objects both instances must have the same type, otherwise the function would halt before performing the comparisson, if debug mode was enabled when compiling, it will also show a warning message.
JASS:
    interface ordered
        method operator &lt;
    endinterface

    interface indexed
        method operator [] takes integer index returns ordered
        method operator []= takes integer index, ordered v returns nothing
    endinterface

    function sort takes indexed a, integer from, integer to returns nothing
     local integer i
     local integer j
     local ordered aux

        set i=from
        loop
            exitwhen (i&gt;=to)
            set j=i+1
            loop
                exitwhen (j&gt;to)
                if (a[j]&lt;a<i>) then
                    set aux = a<i>
                    set a<i> = a[j]
                    set a[j] = aux
                endif
                set j=j+1
            endloop

            set i=i+1
        endloop
    endfunction
</i></i></i>

....
 

Flare

Stops copies me!
Reaction score
662
I haven't tried overloading the > and <, so I'll just deal with [] and []=

Basically, it just allows you to replace the normal parameter for the array index (i.e. an integer) with your own custom one.

[] would correspond to referencing the array (e.g. call Func (Array[x]), while []= would be setting it (e.g. set Array[x] = value)

If you take PUI for example,
JASS:
    static method operator[] takes unit whichUnit returns integer
        local integer pui = GetUnitIndex(whichUnit)
        if .pui_data[pui] != 0 then
            if .pui_unit[pui] != whichUnit then
                // recycled handle detected
                call .destroy(.pui_data[pui])
                set .pui_unit[pui] = null
                set .pui_data[pui] = 0            
            endif
        endif
        return .pui_data[pui]
    endmethod

It takes a unit for the parameter, so you could do
JASS:
set Data[myUnit] = someValue
set someValue = Data[myUnit]
//Data corresponding to the structs&#039;s name

which would be equivalent to (well, if you disregard the null checks in PUI)
JASS:
set DataArray[GetUnitIndex (myUnit)] = someValue
set someValue = DataArray[GetUnitIndex (myUnit)]
//DataArray just being some global



Also, could be used with gamecache e.g.
JASS:
struct Cache //obviously, a gamecache would need to be initialized somewhere
//this is lame freehand version of Table, I don&#039;t really feel like searching for the system to get the exact code
method operator [] takes handle h returns integer
  return GetStoredInteger (myCache, I2S (this), I2S (H2I (h)))
endmethod

method operator []= takes handle h, integer data returns nothing
  call StoreInteger (myCache, I2S (this), I2S (H2I (h)), data)
endmethod
endstruct

Then,
JASS:
globals
Cache StructCache
endglobals

function ... takes ... returns ...
local StructData d = StructCache[GetExpiredTimer ()] //for something like timer attachment
endfunction

function Init takes ... returns ...
  set StructCache = Cache.create ()
endfunction
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top