What is difference between 1 and 2?
-
var A = 0.0;
-
var A : float = 0.0;
What is difference between 1 and 2?
var A = 0.0;
var A : float = 0.0;
There is no difference. (Well, aside from line 1 being 12 characters long and line 2 being 20 characters long.)
–Eric
whisper its just there to make you feel good that you can write that extra line of code.
Actually the second is just good programming practice.
It’s always nice to explicitly tell the variable what type it is instead of letting the compiler make assumptions. It also helps you later on if your program gets bigger or you need to come back to the project after a break… lets you know by looking at it what type it is instead of reading through and figuring it out.
Basically they are the same, the first is the compiler determining the type by the value given and the second you telling the compiler what to expect.
They will both work (generally), but they aren’t the same.
http://www.unifycommunity.com/wiki/index.php?title=JavaScript_Type_Inference#Dynamic_Type_Checking
The main reason to statically type your variables is for performance reasons, and the cases in which Unity is unable to accurately guess what type you meant.
FYI, may be somewhat unrelated, but if you did that in C#, the compiler will assume that the number is a double, not a float.
As novashot said, you’re far better off getting in the habit of explicitly typing your variables. Once you start you’ll never go back.
Incorrect. They are exactly the same.
Both lines posted by the OP are statically typed. Type inference is done at compile time and isn’t related in any way to dynamic typing.
Which is one of the annoying things about C#, since you’re far more likely to be using floats rather than doubles. With Javascript you don’t have to stick “f” in floats.
Can’t really agree with that:
var someVar = Vector3(1.5, 2.0, 3.3);
var someVar : Vector3 = Vector3(1.5, 2.0, 3.3);
With the first line, it’s already 100% clear that “someVar” is a Vector3; the second line only adds redundancy, and makes the code unnecessarily cluttered. I find code that’s as simple and clear as possible makes it easier when you have to read through large amounts of it.
–Eric
QFT.
I even have to waste time hitting the shift key because lowercase f doesn’t have syntax highlighting. :roll:
I respectfully disagree: C# is beautifully type-safe. If you define your code as:
float myFloat = 0.0;
double myDouble = 0.0;
It will properly handle both cases. In cases of ambiguity, it will default to double (or int if there’s no decimal). Whatever their decision was to choose double over float, it doesn’t matter. It’s only really an issue if you use the keyword “var” instead:var myVariable = 0.0; which would produce a double. But to be honest, you should never use “var” unless its truly necessary (anonymous types, LINQ, etc.)
What you are “far more likely” is irrelevant; it depends on the application. At my work, we usually use doubles for accuracy in our particular programs. In Unity it’s usually floats. But it doesn’t actually matter because you should always, always, always explicitly define your types.
I respectfully disagree: C# is beautifully type-safe. If you define your code as:
float myFloat = 0.0; double myDouble = 0.0;It will properly handle both cases.
I’m sorry, but it doesn’t. If it’s a float it simply will not compile without the “f”. It’s not a matter of agreeing or disagreeing, that’s just a fact.
What you are “far more likely” is irrelevant; it depends on the application.
Obviously, the application is Unity. I don’t use C# in anything else so I don’t care about any other use cases.
–Eric
C# and Mono is standardized. There’s no acceptable reason for Unity to alter up those standardized compilation and syntax rules because floats are used more often in this medium. That would mess up a lot more .NET/Mono developers than it would to be a slight pain for Unity scripters.
You’re right though, declaring a float without the “f” won’t work. You can see how often I do that, though it became such a habit from years ago that I would always append my floats with “f” that I never thought twice of it and in the .NET world I assumed that it’d convert fine (though I see why it would not). Doubles work at least. ![]()
C# and Mono is standardized. There’s no acceptable reason for Unity to alter up those standardized compilation and syntax rules because floats are used more often in this medium. That would mess up a lot more .NET/Mono developers than it would to be a slight pain for Unity scripters.
I wasn’t suggesting that they change the standard, I was suggesting that it’s annoying (with the context being the usage in Unity since this is a Unity forum after all
).
–Eric
I wasn’t suggesting that they change the standard
Then I’ll do it. If we can have our own version of JavaScript, then I call for a less annoying version of C# as well. U#?
Eh if I am going to spend all this time scripting in Unity, I’d prefer it adhere to industry standards. That way my work isn’t only useful in the Unity universe but applicable in other projects as well.
But to be honest, you should never use “var” unless its truly necessary (anonymous types, LINQ, etc.)
What you are “far more likely” is irrelevant; it depends on the application. At my work, we usually use doubles for accuracy in our particular programs. In Unity it’s usually floats. But it doesn’t actually matter because you should always, always, always explicitly define your types.
Uses and misuses of implicit typing
Then I’ll do it. If we can have our own version of JavaScript, then I call for a less annoying version of C# as well. U#?
If we had a less annoying version of C#, then we couldn’t use the less annoying IDE ― Visual Studio.