Why don't we declare types in Swift?

Discussion in 'iOS Programming' started by AlecZ, Mar 15, 2015.

  1. AlecZ, Mar 15, 2015
    Last edited: Mar 15, 2015

    AlecZ macrumors 65816

    AlecZ

    Joined:
    Sep 11, 2014
    Location:
    Berkeley, CA
    #1
    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?
     
  2. cipo macrumors member

    Joined:
    Nov 23, 2010
    Location:
    Stuttgart, Germany
    #2
    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.
    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"
     
  3. AlecZ, Mar 16, 2015
    Last edited: Mar 16, 2015

    AlecZ thread starter macrumors 65816

    AlecZ

    Joined:
    Sep 11, 2014
    Location:
    Berkeley, CA
    #3
    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.

    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.
     
  4. cipo macrumors member

    Joined:
    Nov 23, 2010
    Location:
    Stuttgart, Germany
    #4
    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.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. AlecZ thread starter macrumors 65816

    AlecZ

    Joined:
    Sep 11, 2014
    Location:
    Berkeley, CA
    #6
    Yeah, but they're not in annoying places, and they're auto-completed :)
     
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    In Python colons are pretty common... You use them to start blocks of code and also for dictionary literals.
     
  8. AlecZ thread starter macrumors 65816

    AlecZ

    Joined:
    Sep 11, 2014
    Location:
    Berkeley, CA
    #8
    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.
     
  9. leman macrumors 604

    Joined:
    Oct 14, 2008
    #9
    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.
     

Share This Page