- Home /
Static Typing for JS function?
Script Optimization indicates that variables should be explicitly typed whenever possible, but ist his also true for variables that are actually functions? http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html
Basically, is it better to do:
`var functionvar;`
or
`var functionvar:boolean;`
for
`function incrementX(){ x++; return true; }`
`functionvar = incrementX();`
Answer by Fattie · Nov 11, 2012 at 09:08 AM
you should type functions - that is, you should give the return type
function something braces semicolon return type
function incrementX( something:Vector3 ):boolean
{
blah;
if ( blah ) return false;
blah
return true;
}
be careful to return null where relevant
function FindUnderHereNamed(name:String):Transform
{
var ttt = transform.GetComponentsInChildren(Transform);
for (var t : Transform in ttt)
{
if (t.name == name) return t;
}
return null;
}
You then ask "what about when you assign a function to a variable? i.e. `var transformFred:Transform = FindUnderHereNamed("Fred")` or just `var transformFred = FindUnderHereNamed("Fred")`"
You're thinking about the confusing and beautiful
"Function" data type.
Once you being to learn about and use data types like this you move to very interesting programming.
var x:Function;
function Awake()
{
x = teste;
}
function teste()
{
Debug.Log("one teste");
}
function testeB():int
{
return 8;
}
function testeC():Vector3
{
return Vector3.up * 14.0;
}
function Start()
{
teste();
x();
x = testeB
var score:int = 27;
score = score + x();
x = testeC;
var heading:Vector3 = Vector3( 2.3,2.7,1.3 );
heading = heading * 0.75 + x();
}
By the way, your examples are simply wrong. In both cases "= FindUnderHereNamed("Fred")" returns the RESULT of that function, because you included the () braces. Makes sense?
When using Function type variables, you must explicitly type them in the declaration. (ie, var x:Function in my example.) (In fact, you should always explicitly type every variable you use. Never fail to do it. it's not 1990 and this is not Perl.)
OK? That fully explains Function type variables right?
what about when you assign a function to a variable? i.e. `var transformFred:Transform = FindUnderHereNamed("Fred")` or just `var transformFred = FindUnderHereNamed("Fred")`
Okay, one more ... what if the function returned a value, would the function variable be declared to be whatever data type the function returns? See example in OP above about incrementX () always returning true, should it be `var functionvar:boolean = incrementX()` or `var functionvar:Function = incrementX()`
also in your examples, it seems you have to declare `function teste():Function` explicitly
One side note, from what I have read lately on another similar thread (actually the exact same), explicitly indicating the return type will not do anything more than making sure you do not return anything else than the specified type. It does not optimize the function at the IL level. You could do the same program twice with and without the return type, you would not see any difference. On the other hand, if you explicitly provide a return type of integer and return a float, you will get a warning (I would guess). Returning a Vector3 for an integer and you get an error at compilation. Without the return type, you would get the error at runtime. To be confirmed.
@faf ... in relation to using Function type variables. Just get my code above, copy, and paste in to any project you have going. experiment with implciit return types for testeB and testeC and you can see what sort of errors or warnings you get.
regarding 'optimization". the whole issue is just sort of whacky. it is utterly inconceivable one would use "Function" type variables, in any sort of performance code.
if one, truly, had a question like "in this specific complex example, would it be higher performance to do la or la". what you would do is:
try it with, and without, the 6 ascii characters in question, and see which is higher performance. it would be absolutely impossible to generalize such as obscure, arcane situation, and it would depend on compiler builds etc etc
Again this is all bizarre because: ALWAYS EXPLICITLY TYPE VARIABLES AND FUNCTION RETURNS
it's just that simple.
Additionally, as seen above, Ina was confused about using "Function" type variables, and thought you have to additionally give the return type of whatever function you are using at the time, or whatever - no. It's "just like a macro".
the one and only take away here is ALWAYS EXPLICITLY TYPE VARIABLES AND FUNCTION RETURNS
Answer by Eric5h5 · Nov 11, 2012 at 05:42 PM
To answer the actual question (sorry, Fattie!): it does not matter what value a variable contains, the rules are always the same. Variables should always be typed, either explicitly or by specifying a value (both of which are functionally identical).
var x; // wrong
var x = Foo; // fine
var x : Function = Foo; // fine
function Foo () {
Debug.Log ("!");
}
the actual question is:
"Is it optimized (faster) to explicitly type Function-type variables"
the answer is "No"
the actual phrasing of the question:
"Script Optimization indicates that variables should be explicitly typed whenever possible, but is this also true for variables that are actually functions?"
The first assumption before the comma is completely wrong, and the question after the comma is answered by "No"
Your answer
![](https://koobas.hobune.stream/wayback/20220613083659im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use functions between two scripts 2 Answers
How to call a function from a script in another scene 5 Answers
If String is in List Then... 2 Answers
Raycasts don't go the right direction unless i'm really far away (javascript) 1 Answer
Underwater effect function causing screen flickering 0 Answers