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?

Basically, is it better to do:

var functionvar;


var functionvar:boolean;


function incrementX(){ x++; return true; }

functionvar = incrementX();

you should type functions - that is, you should give the return type

function something braces semicolon return type

function incrementX( something:Vector3 ):boolean
	if ( blah ) return false;
	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 ( == 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()
      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?

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 ("!");