Learning Swift right now I am up to Enums and realise that Syntactical-Sugar (simplification) was always going to be something that Apple would like to do, as they did with scrollbars.
While removing type names may be cool, and it is readable, it’s seems like it makes it harder to understand, but then again I am a beginner. It would be nice to have a toggle (as the compiler knows what is going on) to switch either way: to remove all of the type names, or add them in. The idea being that for the first few passes over somebody else’s code you may prefer to read all of the detail, until becoming familiar, when you then toggle it off yourself, at your convenience.
I would like to know more advanced coders views on this, or if I will just get used to it?
Thanks!
— This isn't really about enums, is it?
— What you're talking about can be an issue (or not) out of context. For example:
let numberOfThings = 5isn't improved by being explicit about the type:
let numberOfThings: Int = 5In fact, it's arguably worsened, since the type is easy to see. OTOH, with something like this:
let age = complicatedThingIAlreadyCalculatedyou have no hope of knowing whether the age is an Int in years, months, days or seconds, or a floating point value of some kind.
In practice, though, so long as you're reading a passage of connected code, and variables are named reasonably and consistently, knowing the type isn't likely to be a problem.
— You should also consider the origins of Swift in reaction to Objective-C. Let's say you have something like this in Swift:
let myObject = MyObject ()This falls under the case already mentioned, where it's pretty obvious what the type is. Back in Obj-C-land, we have to type:
MyObject* myObject = [MyObject new]; // "new" for purposes of illustration onlyIt got really old really quickly to have to type the class twice in every declaration, when once would have been enough. This tedious duplication of effort in Obj-C was one of the things Swift was designed to avoid — hence the type inference that avoids the explicit type annotation.
— If you're within Xcode, you can always find out the type of a variable you don't know: you can Option-Click on it to find out.
So, yes, some developers have their anxiety levels raised by the lack of explicit typing, but most(?) developers who are used to the language find it a desirable boon for the fingertips. 🙂