The true use of 'constant'?

tooltiperror

Super Moderator
Reaction score
231
Why should I make a function constant? What will it do?
 

Nestharus

o-o
Reaction score
84
From my guide-
Constant functions may not call non-constant functions and they may not manipulate any element that cannot be a constant (arrays and code).

Both regular functions and constant functions are inlined if they follow these rules:
* The function is a one-liner

* If the function is called by call the function's contents must begin with a variable assignment (=) or a function call or be a return of a single function.

* If the inlined function is an assigment, it should not assign one of its arguments (input).

* Every argument must be evaluated once and only once by the function, in the same order as they appear in the arguments list.

* If the function contains function calls, they should all be evaluated after the arguments UNLESS the function is marked as non-state changing, at the moment some very few native functions and also return bug exploiters are considered as non-state changing.

So really, constant functions are pretty deprecated.

Constants that aren't inlined are exactly the same speed as their non-constant cousins
 

Weep

Godspeed to the sound of the pounding
Reaction score
400
From my guide-
So, which parts apply to vanilla JASS? (If you hadn't noticed, ToolTipError is on a Mac and doesn't care to use vJASS.)

Constants that aren't inlined are exactly the same speed as their non-constant cousins
AFAIK, there's no inlining with the stock WE compiler, so does that mean there is no difference made by declaring a constant function?
 

weaaddar

New Member
Reaction score
6
The constant keyword is meant to imply something about usage, but because the constant natives are terrible and stateful, it doesn't really mean anything.
 

Nestharus

o-o
Reaction score
84
Precisely, and there's absolutely no speed difference between constant functions and regular functions with plain old JASS.

->Constants that aren't inlined are exactly the same speed as their non-constant cousins
 

Jesus4Lyf

Good Idea™
Reaction score
397
If you write a constant function, it should tell the user that running that function over something does not change the state of anything. IE, getting a widget's life does not change that widget's life or edit any variables, it has no "side effects". :)

So constant functions only return values, calculated or not.
 

weaaddar

New Member
Reaction score
6
Except that some constant natives have side effects or inconsistent returns. ergo its useless.
 

Anachron

New Member
Reaction score
53
I never used constant functions.

Also there is one word, static, that does something like constant with if constructs.
JASS:
static if LIBRARY_TABLE then
    call BJDebugMsg("You have the library table in the map.")
endif


This will print the text if you have a library called table. The static part means its checked for parsing. If you don't have the library, it gets commented out.
 
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