Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · Nov 11, 2012 at 05:56 AM · javascriptfunction

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();`

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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?

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Nov 11, 2012 at 09:38 AM 0
Share

what about when you assign a function to a variable? i.e. `var transformFred:Transform = FindUnderHereNamed("Fred")` or just `var transformFred = FindUnderHereNamed("Fred")`

avatar image ina · Nov 11, 2012 at 12:16 PM 0
Share

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()`

avatar image ina · Nov 11, 2012 at 12:28 PM 0
Share

also in your examples, it seems you have to declare `function teste():Function` explicitly

avatar image fafase · Nov 11, 2012 at 01:08 PM 2
Share

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.

avatar image Fattie · Nov 11, 2012 at 01:18 PM 0
Share

@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

Show more comments
avatar image
0

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 ("!");
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Nov 11, 2012 at 08:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges