Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialReed Carson
8,306 PointsWhy does he say we need to write 255.0 instead of jus 255 when the type has already explicitly been declared as a float?
why is he asking us to add the .0 when the types are already floats?
2 Answers
Nathan Tallack
22,160 PointsLeft over from legacy languages that did not like you trying to pass what looked like an int into a float or double.
Swift of course has no such problems. It is happy enough to automatically typecast an int into a float. :)
Nathan Tallack
22,160 PointsWell, in this instance I am guessing they declare Float so that they don't get people passing 55.0 and have it defaulting to a Double when type inference guess it as a double.
That could likely be the reason. Because if you just said let myFloat = 255.0 you would end up with a double. So they are explicitly declaring the type as a Float and seeing as you are doing that you can be lazy and pass in 255 and leave off the .0 if you wish. :)
Reed Carson
8,306 PointsReed Carson
8,306 Pointsso there is typecasting occurring under the hood in that instance?