- Home /
What is Javascript dynamic typing and type inference on initialization?
Hey Unitarians! (Specifically Android Javascript programmers..)
I'm in a bit of a spot of trouble. I made a game with a ton of javascripts that were initially going to be developed for desktop. I changed my mind and switched to Android. I got a bazillion compile errors, and after much google hunting, Unity told me what I did wrong: http://docs.unity3d.com/Manual/android-GettingStarted.html. At near the end of this URL, it says,
For performance reasons, dynamic typing in JavaScript is always turned off in Unity Android, as if #pragma strict were applied automatically to all scripts. This is important to know if you start with a project originally developed for the desktop platforms since you may find you get unexpected compile errors when switching to Android; dynamic typing is the first thing to investigate. These errors are usually easy to fix if you make sure all variables are explicitly typed or use type inference on initialization.
So I know that I should have made it in C Sharp, like most of my other Android projects. However, I have no idea how to convert Javascripts to C Sharp and I imagine that even if I did it would take a lot of time that I don't have.
They show two solutions at the end of their paragraph. They say that I can "make sure all variables are explicitly typed" I imagine this would take a lot of unnessesary time, and either way I'm not 100% sure what they mean on that. They also say something called "use type inference on initialization". As my last resort, I'm interested in this. Can anyone tell me what it is and what to do? Or if you could clear up dynamic typing in general as well, that'd be great since I believe I only have a on-the-surface idea of what's happening here.
Your awesome Indie developer, Aman Jha
Answer by meat5000 · Jun 22, 2014 at 05:14 PM
My answer disappeared again... I'll re-write it.
Unity is telling you to tell it what Type your variables are as you declare them rather than letting it work that out based on the data that's put in.
so,
var myFloat : float = 0.0;
or
var myFloat = 0.0;
Rather than
var myFloat;
myFloat = 0.0;
:O It's that simple?? Thanks so much! I'm going to check it out right now after I finish wrestling with my non-cooperative GUI elements.
@meat5000: That's actually wrong, or to be more precise, both of your examples are actually right ;)
In the first one you explicitly type your variable manually. In your second example you use type inference to type the variable which is also ok.
What doesn't work is this:
var myFloat; // dynamic type
// ...
myFloat = 5; // dynamic... int
myFloat = "Hello world!"; // dynamic... string
myFloat = 0.0; // dynamic... float
In this case myFloat doesn't have a type and would be a "dynamic type" which isn't supported on mobile and should even be avoided on any platform.
Cheers for clearing that up. I got some errors using the second one a long time ago and I've avoided it like the plague ever since :D
I'll update my answer.
Better? :D
$$anonymous$$y primary language is actually AS$$anonymous$$ ^ ^
@meat5000: Yes, better :D But i bet you don't use AS$$anonymous$$ in Unity, or do you code in IL? x)
I used x86 assembler for quite a long time as well as PIC assembler during my PIC time.
$$anonymous$$y primary languages are now: C#, LUA, C++, Java, Delphi. I never use UnityScript ;)
lol I'd have to be Autistic to pull that one off :D
PICs are awesome :P Got a few 877A propping up my desk :D I was halfway through a lil USB project when I got distracted by Unity.
Java was my first OO. Got heavily in to C, Elec-Eng stylee. $$anonymous$$y skills are fading though. I can feel my brain cells committing suicide :D
Your answer
Follow this Question
Related Questions
5.4 No autocompletion in unity or MonoDevelop, how to compile the code JS & C#? 0 Answers
About ListItems in Unity3D 1 Answer
Unity editor style camera script 1 Answer
Flying AI Planes 0 Answers