• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Multiple return statements

Status
Not open for further replies.
Level 2
Joined
Aug 24, 2004
Messages
11
What is the purpose of back to back return statements in a function? I'll use an example from fugly's Create Aura script to show you what I'm talking about:

Code:
function H2G takes handle h returns group  
   return h 
   return null 
endfunction

I have seen quite a few people do this in their code, but I don't understand the purpose. I can only assume that if the first return statement fails Warcraft will attempt the next command, kind of like a try/catch clause in C++. But I NEVER see anybody test for the error condition. So what is the point of having a second return statement if your script will only crash again because of a null pointer error in the function that called this function?
 
Level 6
Joined
Jun 16, 2004
Messages
237
It's a so called "return bug." Only the last return statement must comply to return type. Now if you look at the code, you see that the script actually returns a handle, when it should return a group. ("return null" is never executed; it's there only for the compiler.) Thus, you can use that function to convert a handle to a group type. Hence the name H2G.

For what purpose such a function is used, that I don't know. Perhaps it has something to do with the fact that "group" is a subtype of "handle".

Cheers!
 
Level 2
Joined
Aug 24, 2004
Messages
11
Exploiting a bug isn't a very good practice because who knows when Blizzard will decide to fix the bug and not tell us. It could easily break tens or hundreds of scripts that I've seen on this site. Have any attempts been made to convince Blizzard to introduce a type-casting system?

On a side note, it's generally frowned upon to use magicode. Magicode is a segment of code that magically does something which is not immediately apparent. At the very least add a comment above the function describing how it works. I would personally like to see many more comments in the JASS snipetts submitted because it makes learning JASS a little easier. A general description of the snippet as a whole is not enough. It should be broken down function by function, and if needed, line by line. DOWN WITH MAGICODE! :D

Adding useful comments in the snippets would really cut down on the number of users having problems when they attempt to customize the code for their map.
 
Level 3
Joined
Mar 27, 2004
Messages
70
People call it "The return bug" because that is what the person who discovered it used to call it.
Blizzard has stated in their forums that the bug will not be fixed, and Bliz even use the "bug" themselves.

You better get used to these kind of functions, they are really common and really useful. Besides, no one have ever experienced any side effects by using them.
 
Status
Not open for further replies.
Top