Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AlecZ

macrumors 65816
Original poster
Sep 11, 2014
1,173
123
Berkeley, CA
Sorry in advance if my terminology is off. I'm learning Swift just by converting old code from ObjC to Swift, and it bothers me that we declare a variable like this

Code:
var foo = Array<AnyObject>()
without declaring the type, as we would in something like Java like this:
Code:
Array<AnyObject> foo = Array<AnyObject>() // Not valid in Swift.
The way Swift does it makes it not very apparent what kind of data this variable refers to! Of course, there's the option of doing
Code:
var foo:Array<AnyObject> = Array<AnyObject>() // Does same thing as first example, right?
but I find that pretty annoying to type and messy in comparison. Why did they change how we declare variables in such a way? Is there something having to do with how Swift works that I'm missing?
 
Last edited:
Sorry in advance if my terminology is off. I'm learning Swift just by converting old code from ObjC to Swift, and it bothers me that we declare a variable like this

Code:
var foo = Array<AnyObject>()
without declaring the type, as we would in something like Java like this:
Code:
Array<AnyObject> foo = Array<AnyObject>() // Not valid in Swift.
The way Swift does it makes it not very apparent what kind of data this variable refers to!
Well, that's a feature of the language (type inference) which is considered by many to be helpful in writing more focused, concise, less redundant code. It also makes your code a bit more resilient to changes in dependencies, e.g. when a class you depend on is renamed, your code might not even care. On the other hand, you can always option-click on the variable name if you're interested in the concrete type.
Of course, there's the option of doing
Code:
var foo:Array<AnyObject> = Array<AnyObject>() // Does same thing as first example, right?
but I find that pretty annoying to type and messy in comparison.
But isn't that just personal preference based on prior experience? You need to have the var / let information in place, so all you could do is swap name and type. I come from a Java background myself, but actually find that this syntax better matches how I would formulate the statement in natural language: "I want a variable named foo of type bar, initialized with the value of x"
 
Well, that's a feature of the language (type inference) which is considered by many to be helpful in writing more focused, concise, less redundant code. It also makes your code a bit more resilient to changes in dependencies, e.g. when a class you depend on is renamed, your code might not even care. On the other hand, you can always option-click on the variable name if you're interested in the concrete type.
Ah, so it is just syntax and not about how Swift handles variables. Yeah, it's better in a way to just type "var", and I didn't think about code resiliency, but you're right about that. There's still the downside of not making it clear what kind of variable it is. This is mostly a problem when it's referring to a number, and you have to specify CGFloat, Float, Int, etc. I'm fine with it anyway.

But isn't that just personal preference based on prior experience? You need to have the var / let information in place, so all you could do is swap name and type. I come from a Java background myself, but actually find that this syntax better matches how I would formulate the statement in natural language: "I want a variable named foo of type bar, initialized with the value of x"

Well, there's an extra "var" in there now, and there's a colon. Maybe it's just me, but I don't like typing or seeing colons in code. Maybe it's just because I'm not used to it, but then again, most people aren't unless they've used Scala (which is very similar to Swift in syntax). Edit: I'll add that C++ has way too many darn colons.
 
Last edited:
Well, there's an extra "var" in there now, and there's a colon. Maybe it's just me, but I don't like typing or seeing colons in code. Maybe it's just because I'm not used to it, but then again, most people aren't unless they've used Scala (which is very similar to Swift in syntax).

Not sure what I can do to make you like colons :), but the extra "var" actually serves a purpose, as it declares a variable as opposed to a constant ("let"). You have the same distinction in Java with "final" denoting a constant, only that the non-final default case has no separate keyword. At least to me, it feels better to have both cases explicitly stated, not just one of them.
 
Well, there's an extra "var" in there now, and there's a colon. Maybe it's just me, but I don't like typing or seeing colons in code. Maybe it's just because I'm not used to it, but then again, most people aren't unless they've used Scala (which is very similar to Swift in syntax). Edit: I'll add that C++ has way too many darn colons.

Objective-C is rife with colons. Not in variable declarations, which are the same as plain C, but in method declarations and invocations (message sends) they're ubiquitous.
 
Objective-C is rife with colons. Not in variable declarations, which are the same as plain C, but in method declarations and invocations (message sends) they're ubiquitous.

Yeah, but they're not in annoying places, and they're auto-completed :)
 
In Python colons are pretty common... You use them to start blocks of code and also for dictionary literals.

You're right, and I didn't even realize it despite using it a lot this year. The colons only seem out of place where I'm declaring variables. Again, not a real issue, but it seemed strange and looked ugly to me, so I was wondering if Swift was doing something different under the hood to warrant it that I wasn't aware of.
 
C++ does essentially the same thing as Swift with its 'auto' keyword. And the colon in declaring the type of the expression is as old as programming languages themselves. It was adopted by Church in 1940 for typed lambda calculus and I would guess that its use predates even that.

Swift takes its roots in algebraic programming languages such as ML and its friends, so its only natural that it adopts the same elegant mathematical notation.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.