~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.
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.
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="" 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="Test" call BJDebugMsg( x[1]) call BJDebugMsg( x[0]+x[3]) set x[1] = "." 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="" 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< takes operatortest b returns boolean return StringLength(this.str) < StringLength(b.str) endmethod endstruct function test takes nothing returns nothing local operatortest x=operatortest.create() local operatortest y=operatortest.create() set x.str="Test..." set y.str=".Test" if (x<y) then call BJDebugMsg("Less than") endif if (x>y) then call BJDebugMsg("Greater than") 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 < 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>=to) set j=i+1 loop exitwhen (j>to) if (a[j]<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>
....