News for hackers. Stuff that matters.
Re:“Swift will be open source later this year” by alkonaut on Monday June 08, 2015 @05:32PM (#9682603)
> If i dont specify a type for my variable i should be able to change my variable's value to whatever type i want.

You mean you want to assign a string to a variable and then re-assign it to something else entirely such as a number?

Just reassigning at all is dubious in my opinion, and seeing a reassignment with a completely different type would make me flip the table in review.

If you ever want a "catch all" variable you can then explicitly type it as a base type like "object", and assign what you want. But that should be very rare, so it's perfectly natural that type inference assigns a static type to the field.

The only time you get bad inference is if you have subtypes and the first assignment is too narrow, e.g

Apple GetApple() { ... } var myFruit = GetApple(); // type is Apple if (cond) myFruit = GetOrange(); // can't assign orange

In this scenario you have to declare explicitly as Fruit. But again: this is rare, and all reassignment should be rare! Always have good reason before mutating anything.

There are no comments on this comment yet.