[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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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